Metric

class aws_cdk.aws_cloudwatch.Metric(*, metric_name, namespace, account=None, color=None, dimensions=None, label=None, period=None, region=None, statistic=None, unit=None)

Bases: object

A metric emitted by a service.

The metric is a combination of a metric identifier (namespace, name and dimensions) and an aggregation function (statistic, period and unit).

It also contains metadata which is used only in graphs, such as color and label. It makes sense to embed this in here, so that compound constructs can attach that metadata to metrics they expose.

This class does not represent a resource, so hence is not a construct. Instead, Metric is an abstraction that makes it easy to specify metrics for use in both alarms and graphs.

Parameters
  • metric_name (str) – Name of the metric.

  • namespace (str) – Namespace of the metric.

  • account (Optional[str]) – Account which this metric comes from. Default: - Deployment account.

  • color (Optional[str]) – The hex color code, prefixed with ‘#’ (e.g. ‘#00ff00’), to use when this metric is rendered on a graph. The Color class has a set of standard colors that can be used here. Default: - Automatic color

  • dimensions (Optional[Mapping[str, Any]]) – Dimensions of the metric. Default: - No dimensions.

  • label (Optional[str]) – Label for this metric when added to a Graph in a Dashboard. Default: - No label

  • period (Optional[Duration]) – The period over which the specified statistic is applied. Default: Duration.minutes(5)

  • region (Optional[str]) – Region which this metric comes from. Default: - Deployment region.

  • statistic (Optional[str]) – What function to use for aggregating. Can be one of the following: - “Minimum” | “min” - “Maximum” | “max” - “Average” | “avg” - “Sum” | “sum” - “SampleCount | “n” - “pNN.NN” Default: Average

  • unit (Optional[Unit]) – Unit used to filter the metric stream. Only refer to datums emitted to the metric stream with the given unit and ignore all others. Only useful when datums are being emitted to the same metric stream under different units. The default is to use all matric datums in the stream, regardless of unit, which is recommended in nearly all cases. CloudWatch does not honor this property for graphs. Default: - All metric datums in the given metric stream

Methods

attach_to(scope)

Attach the metric object to the given construct scope.

Returns a Metric object that uses the account and region from the Stack the given construct is defined in. If the metric is subsequently used in a Dashboard or Alarm in a different Stack defined in a different account or region, the appropriate ‘region’ and ‘account’ fields will be added to it.

If the scope we attach to is in an environment-agnostic stack, nothing is done and the same Metric object is returned.

Parameters

scope (Construct) –

Return type

Metric

create_alarm(scope, id, *, evaluation_periods, threshold, actions_enabled=None, alarm_description=None, alarm_name=None, comparison_operator=None, datapoints_to_alarm=None, evaluate_low_sample_count_percentile=None, period=None, statistic=None, treat_missing_data=None)

Make a new Alarm for this metric.

Combines both properties that may adjust the metric (aggregation) as well as alarm properties.

Parameters
  • scope (Construct) –

  • id (str) –

  • evaluation_periods (Union[int, float]) – The number of periods over which data is compared to the specified threshold.

  • threshold (Union[int, float]) – The value against which the specified statistic is compared.

  • actions_enabled (Optional[bool]) – Whether the actions for this alarm are enabled. Default: true

  • alarm_description (Optional[str]) – Description for the alarm. Default: No description

  • alarm_name (Optional[str]) – Name of the alarm. Default: Automatically generated name

  • comparison_operator (Optional[ComparisonOperator]) – Comparison to use to check if metric is breaching. Default: GreaterThanOrEqualToThreshold

  • datapoints_to_alarm (Union[int, float, None]) – The number of datapoints that must be breaching to trigger the alarm. This is used only if you are setting an “M out of N” alarm. In that case, this value is the M. For more information, see Evaluating an Alarm in the Amazon CloudWatch User Guide. Default: evaluationPeriods

  • evaluate_low_sample_count_percentile (Optional[str]) – Specifies whether to evaluate the data and potentially change the alarm state if there are too few data points to be statistically significant. Used only for alarms that are based on percentiles. Default: - Not configured.

  • period (Optional[Duration]) – (deprecated) The period over which the specified statistic is applied. Cannot be used with MathExpression objects. Default: - The period from the metric

  • statistic (Optional[str]) – (deprecated) What function to use for aggregating. Can be one of the following: - “Minimum” | “min” - “Maximum” | “max” - “Average” | “avg” - “Sum” | “sum” - “SampleCount | “n” - “pNN.NN” Cannot be used with MathExpression objects. Default: - The statistic from the metric

  • treat_missing_data (Optional[TreatMissingData]) – Sets how this alarm is to handle missing data points. Default: TreatMissingData.Missing

Return type

Alarm

to_alarm_config()

Turn this metric object into an alarm configuration.

Return type

MetricAlarmConfig

to_graph_config()

Turn this metric object into a graph configuration.

Return type

MetricGraphConfig

to_metric_config()

Inspect the details of the metric object.

Return type

MetricConfig

to_string()

Returns a string representation of an object.

Return type

str

with_(*, account=None, color=None, dimensions=None, label=None, period=None, region=None, statistic=None, unit=None)

Return a copy of Metric with properties changed.

All properties except namespace and metricName can be changed.

Parameters
  • account (Optional[str]) – Account which this metric comes from. Default: - Deployment account.

  • color (Optional[str]) – The hex color code, prefixed with ‘#’ (e.g. ‘#00ff00’), to use when this metric is rendered on a graph. The Color class has a set of standard colors that can be used here. Default: - Automatic color

  • dimensions (Optional[Mapping[str, Any]]) – Dimensions of the metric. Default: - No dimensions.

  • label (Optional[str]) – Label for this metric when added to a Graph in a Dashboard. Default: - No label

  • period (Optional[Duration]) – The period over which the specified statistic is applied. Default: Duration.minutes(5)

  • region (Optional[str]) – Region which this metric comes from. Default: - Deployment region.

  • statistic (Optional[str]) – What function to use for aggregating. Can be one of the following: - “Minimum” | “min” - “Maximum” | “max” - “Average” | “avg” - “Sum” | “sum” - “SampleCount | “n” - “pNN.NN” Default: Average

  • unit (Optional[Unit]) – Unit used to filter the metric stream. Only refer to datums emitted to the metric stream with the given unit and ignore all others. Only useful when datums are being emitted to the same metric stream under different units. The default is to use all matric datums in the stream, regardless of unit, which is recommended in nearly all cases. CloudWatch does not honor this property for graphs. Default: - All metric datums in the given metric stream

Return type

Metric

Attributes

account

Account which this metric comes from.

Return type

Optional[str]

color

The hex color code used when this metric is rendered on a graph.

Return type

Optional[str]

dimensions

Dimensions of this metric.

Return type

Optional[Mapping[str, Any]]

label

Label for this metric when added to a Graph in a Dashboard.

Return type

Optional[str]

metric_name

Name of this metric.

Return type

str

namespace

Namespace of this metric.

Return type

str

period

Period of this metric.

Return type

Duration

region

Region which this metric comes from.

Return type

Optional[str]

statistic

Statistic of this metric.

Return type

str

unit

Unit of the metric.

Return type

Optional[Unit]

Static Methods

classmethod grant_put_metric_data(grantee)

Grant permissions to the given identity to write metrics.

Parameters

grantee (IGrantable) – The IAM identity to give permissions to.

Return type

Grant