Skip to content
v2.278.0

Metric Class ​

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 ​

ts
declare const instance: rds.IDatabaseInstance;

const metrics = rds.DatabaseInstanceMetrics.fromDatabaseInstance(instance);

// How far a read replica lags behind its source (average over 5 minutes)
const replicaLag = metrics.replicaLag();

// The average amount of time taken per disk I/O read operation (average over 1 minute)
const readLatency = metrics.readLatency({ period: Duration.seconds(60) });

// Any other metric, such as an engine-specific one
const binLogDiskUsage = metrics.metric('BinLogDiskUsage', { statistic: 'Maximum' });
python
# instance: rds.IDatabaseInstance


metrics = rds.DatabaseInstanceMetrics.from_database_instance(instance)

# How far a read replica lags behind its source (average over 5 minutes)
replica_lag = metrics.replica_lag()

# The average amount of time taken per disk I/O read operation (average over 1 minute)
read_latency = metrics.read_latency(period=Duration.seconds(60))

# Any other metric, such as an engine-specific one
bin_log_disk_usage = metrics.metric("BinLogDiskUsage", statistic="Maximum")
java
IDatabaseInstance instance;


DatabaseInstanceMetrics metrics = DatabaseInstanceMetrics.fromDatabaseInstance(instance);

// How far a read replica lags behind its source (average over 5 minutes)
Metric replicaLag = metrics.replicaLag();

// The average amount of time taken per disk I/O read operation (average over 1 minute)
Metric readLatency = metrics.readLatency(MetricOptions.builder().period(Duration.seconds(60)).build());

// Any other metric, such as an engine-specific one
Metric binLogDiskUsage = metrics.metric("BinLogDiskUsage", MetricOptions.builder().statistic("Maximum").build());
csharp
// Install: dotnet add package Amazon.CDK.Lib
IDatabaseInstance instance;


var metrics = DatabaseInstanceMetrics.FromDatabaseInstance(instance);

// How far a read replica lags behind its source (average over 5 minutes)
var replicaLag = metrics.ReplicaLag();

// The average amount of time taken per disk I/O read operation (average over 1 minute)
var readLatency = metrics.ReadLatency(new MetricOptions { Period = Duration.Seconds(60) });

// Any other metric, such as an engine-specific one
var binLogDiskUsage = metrics.Metric("BinLogDiskUsage", new MetricOptions { Statistic = "Maximum" });
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var instance IDatabaseInstance


metrics := rds.DatabaseInstanceMetrics_FromDatabaseInstance(instance)

// How far a read replica lags behind its source (average over 5 minutes)
replicaLag := metrics.ReplicaLag()

// The average amount of time taken per disk I/O read operation (average over 1 minute)
readLatency := metrics.ReadLatency(&MetricOptions{
	Period: awscdk.Duration_Seconds(jsii.Number(60)),
})

// Any other metric, such as an engine-specific one
binLogDiskUsage := metrics.Metric(jsii.String("BinLogDiskUsage"), &MetricOptions{
	Statistic: jsii.String("Maximum"),
})

Initializer ​

ts
new Metric(props: MetricProps)
python
Metric(props: MetricProps)
java
new Metric(MetricProps props)
csharp
new Metric(MetricProps props)
go
NewMetric(Props MetricProps)

Properties ​

accountmay be undefinedstringaccountmay be undefinedstraccountmay be undefinedStringAccountmay be undefinedstringAccountmay be undefined*string #
Account which this metric comes from.
colormay be undefinedstringcolormay be undefinedstrcolormay be undefinedStringColormay be undefinedstringColormay be undefined*string #
The hex color code used when this metric is rendered on a graph.
dimensionsmay be undefined{ [key: string]: any }dimensionsmay be undefinedtyping.Mapping[str, Any]dimensionsmay be undefinedMap<String, Object>Dimensionsmay be undefinedIDictionary<string, object>Dimensionsmay be undefined*map[string]interface{} #
Dimensions of this metric.
idmay be undefinedstringidmay be undefinedstridmay be undefinedStringIdmay be undefinedstringIdmay be undefined*string #
Unique identifier for this metric when used in dashboard widgets.
labelmay be undefinedstringlabelmay be undefinedstrlabelmay be undefinedStringLabelmay be undefinedstringLabelmay be undefined*string #
Label for this metric when added to a Graph in a Dashboard.
metricNamestringmetric_namestrmetricNameStringMetricNamestringMetricName*string #
Name of this metric.
namespacestringnamespacestrnamespaceStringNamespacestringNamespace*string #
Namespace of this metric.
periodDurationperiodDurationperiodDurationPeriodDurationPeriodDuration #
Period of this metric.
regionmay be undefinedstringregionmay be undefinedstrregionmay be undefinedStringRegionmay be undefinedstringRegionmay be undefined*string #
Region which this metric comes from.
statisticstringstatisticstrstatisticStringStatisticstringStatistic*string #
Statistic of this metric.
unitmay be undefinedUnitunitmay be undefinedUnitunitmay be undefinedUnitUnitmay be undefinedUnitUnitmay be undefinedUnit #
Unit of the metric.
visiblemay be undefinedbooleanvisiblemay be undefinedboolvisiblemay be undefinedBooleanVisiblemay be undefinedboolVisiblemay be undefined*bool #
Whether this metric should be visible in dashboard graphs.
warningsmay be undefinedstring[]warningsmay be undefinedtyping.List[str]warningsmay be undefinedList<String>Warningsmay be undefinedstring[]Warningsmay be undefined*[]*string deprecated #
- use warningsV2
warningsV2may be undefined{ [key: string]: string }warnings_v2may be undefinedtyping.Mapping[str, str]warningsV2may be undefinedMap<String, String>WarningsV2may be undefinedIDictionary<string, string>WarningsV2may be undefined*map[string]*string #
Warnings attached to this metric.

Methods ​

anomalyDetectionFor(props: AnomalyDetectionMetricOptions): MathExpression@staticmethod def anomaly_detection_for(props: AnomalyDetectionMetricOptions) -> MathExpressionpublic static MathExpression anomalyDetectionFor(AnomalyDetectionMetricOptions props)public static MathExpression AnomalyDetectionFor(AnomalyDetectionMetricOptions props)func AnomalyDetectionFor(Props AnomalyDetectionMetricOptions) MathExpression static #
Creates an anomaly detection metric from the provided metric.
1 parameter · returns

Parameters

  • propspropspropspropsProps — The anomaly detection alarm properties.

Returns: An anomaly detection metric

attachTo(scope: IConstruct): Metric // chainabledef attach_to(self, scope: IConstruct) -> Metricpublic Metric attachTo(IConstruct scope)public Metric AttachTo(IConstruct scope)func AttachTo(Scope IConstruct) Metric #
Attach the metric object to the given construct scope.
notes

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: Construct, id: string, props: CreateAlarmOptions): Alarmdef create_alarm(self, scope: Construct, id: str, props: CreateAlarmOptions) -> Alarmpublic Alarm createAlarm(Construct scope, String id, CreateAlarmOptions props)public Alarm CreateAlarm(Construct scope, string id, CreateAlarmOptions props)func CreateAlarm(Scope Construct, Id *string, Props CreateAlarmOptions) Alarm #
Make a new Alarm for this metric.

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

grantPutMetricData(grantee: IGrantable): Grant@staticmethod def grant_put_metric_data(grantee: IGrantable) -> Grantpublic static Grant grantPutMetricData(IGrantable grantee)public static Grant GrantPutMetricData(IGrantable grantee)func GrantPutMetricData(Grantee IGrantable) Grant static #
Grant permissions to the given identity to write metrics.
1 parameter

Parameters

  • granteegranteegranteegranteeGrantee — The IAM identity to give permissions to.
toMetricConfig(): MetricConfigdef to_metric_config(self, ) -> MetricConfigpublic MetricConfig toMetricConfig()public MetricConfig ToMetricConfig()func ToMetricConfig() MetricConfig #
Inspect the details of the metric object.
toString(): stringdef to_string(self, ) -> strpublic String toString()public string ToString()func ToString() *string #
Returns a string representation of an object.
with(props: MetricOptions): Metric // chainabledef with(self, props: MetricOptions) -> Metricpublic Metric with(MetricOptions props)public Metric With(MetricOptions props)func With(Props MetricOptions) Metric #
Return a copy of Metric `with` properties changed.

All properties except namespace and metricName can be changed.

1 parameter

Parameters

  • propspropspropspropsProps — The set of properties to change.