aws-cdk-lib.aws_cloudwatch.Metric

class Metric

LanguageType name
.NETAmazon.CDK.AWS.CloudWatch.Metric
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awscloudwatch#Metric
Javasoftware.amazon.awscdk.services.cloudwatch.Metric
Pythonaws_cdk.aws_cloudwatch.Metric
TypeScript (source)aws-cdk-lib » aws_cloudwatch » Metric

Implements IMetric

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.

Example

declare const fn: lambda.Function;

const minuteErrorRate = fn.metricErrors({
  statistic: cloudwatch.Stats.AVERAGE,
  period: Duration.minutes(1),
  label: 'Lambda failure rate'
});

Initializer

new Metric(props: MetricProps)

Parameters

  • props MetricProps

Properties

NameTypeDescription
metricNamestringName of this metric.
namespacestringNamespace of this metric.
periodDurationPeriod of this metric.
statisticstringStatistic of this metric.
account?stringAccount which this metric comes from.
color?stringThe hex color code used when this metric is rendered on a graph.
dimensions?{ [string]: any }Dimensions of this metric.
label?stringLabel for this metric when added to a Graph in a Dashboard.
region?stringRegion which this metric comes from.
unit?UnitUnit of the metric.
warnings?string[]Warnings attached to this metric.

metricName

Type: string

Name of this metric.


namespace

Type: string

Namespace of this metric.


period

Type: Duration

Period of this metric.


statistic

Type: string

Statistic of this metric.


account?

Type: string (optional)

Account which this metric comes from.


color?

Type: string (optional)

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


dimensions?

Type: { [string]: any } (optional)

Dimensions of this metric.


label?

Type: string (optional)

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


region?

Type: string (optional)

Region which this metric comes from.


unit?

Type: Unit (optional)

Unit of the metric.


warnings?

Type: string[] (optional)

Warnings attached to this metric.

Methods

NameDescription
attachTo(scope)Attach the metric object to the given construct scope.
createAlarm(scope, id, props)Make a new Alarm for this metric.
toMetricConfig()Inspect the details of the metric object.
toString()Returns a string representation of an object.
with(props)Return a copy of Metric with properties changed.
static grantPutMetricData(grantee)Grant permissions to the given identity to write metrics.

attachTo(scope)

public attachTo(scope: IConstruct): Metric

Parameters

  • scope IConstruct

Returns

  • Metric

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.


createAlarm(scope, id, props)

public createAlarm(scope: Construct, id: string, props: CreateAlarmOptions): Alarm

Parameters

  • scope Construct
  • id string
  • props CreateAlarmOptions

Returns

  • Alarm

Make a new Alarm for this metric.

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


toMetricConfig()

public toMetricConfig(): MetricConfig

Returns

  • MetricConfig

Inspect the details of the metric object.


toString()

public toString(): string

Returns

  • string

Returns a string representation of an object.


with(props)

public with(props: MetricOptions): Metric

Parameters

  • props MetricOptions — The set of properties to change.

Returns

  • Metric

Return a copy of Metric with properties changed.

All properties except namespace and metricName can be changed.


static grantPutMetricData(grantee)

public static grantPutMetricData(grantee: IGrantable): Grant

Parameters

  • grantee IGrantable — The IAM identity to give permissions to.

Returns

  • Grant

Grant permissions to the given identity to write metrics.