Skip to content
v2.273.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 serverlessCache: elasticache.ServerlessCache;

// The 5 minutes average of the total number of successful read-only key lookups in the cache.
const cacheHits = serverlessCache.metricCacheHitCount();

// The 5 minutes average of the total number of bytes used by the data stored in the cache.
const bytesUsedForCache = serverlessCache.metricDataStored();

// The 5 minutes average of the total number of ElastiCacheProcessingUnits (ECPUs) consumed by the requests executed on the cache.
const elastiCacheProcessingUnits = serverlessCache.metricProcessingUnitsConsumed();

// Create an alarm for ECPUs.
elastiCacheProcessingUnits.createAlarm(this, 'ElastiCacheProcessingUnitsAlarm', {
  threshold: 50,
  evaluationPeriods: 1,
});
python
# serverless_cache: elasticache.ServerlessCache


# The 5 minutes average of the total number of successful read-only key lookups in the cache.
cache_hits = serverless_cache.metric_cache_hit_count()

# The 5 minutes average of the total number of bytes used by the data stored in the cache.
bytes_used_for_cache = serverless_cache.metric_data_stored()

# The 5 minutes average of the total number of ElastiCacheProcessingUnits (ECPUs) consumed by the requests executed on the cache.
elasti_cache_processing_units = serverless_cache.metric_processing_units_consumed()

# Create an alarm for ECPUs.
elasti_cache_processing_units.create_alarm(self, "ElastiCacheProcessingUnitsAlarm",
    threshold=50,
    evaluation_periods=1
)
java
ServerlessCache serverlessCache;


// The 5 minutes average of the total number of successful read-only key lookups in the cache.
Metric cacheHits = serverlessCache.metricCacheHitCount();

// The 5 minutes average of the total number of bytes used by the data stored in the cache.
Metric bytesUsedForCache = serverlessCache.metricDataStored();

// The 5 minutes average of the total number of ElastiCacheProcessingUnits (ECPUs) consumed by the requests executed on the cache.
Metric elastiCacheProcessingUnits = serverlessCache.metricProcessingUnitsConsumed();

// Create an alarm for ECPUs.
elastiCacheProcessingUnits.createAlarm(this, "ElastiCacheProcessingUnitsAlarm", CreateAlarmOptions.builder()
        .threshold(50)
        .evaluationPeriods(1)
        .build());
csharp
// Install: dotnet add package Amazon.CDK.Lib
ServerlessCache serverlessCache;


// The 5 minutes average of the total number of successful read-only key lookups in the cache.
var cacheHits = serverlessCache.MetricCacheHitCount();

// The 5 minutes average of the total number of bytes used by the data stored in the cache.
var bytesUsedForCache = serverlessCache.MetricDataStored();

// The 5 minutes average of the total number of ElastiCacheProcessingUnits (ECPUs) consumed by the requests executed on the cache.
var elastiCacheProcessingUnits = serverlessCache.MetricProcessingUnitsConsumed();

// Create an alarm for ECPUs.
elastiCacheProcessingUnits.CreateAlarm(this, "ElastiCacheProcessingUnitsAlarm", new CreateAlarmOptions {
    Threshold = 50,
    EvaluationPeriods = 1
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var serverlessCache ServerlessCache


// The 5 minutes average of the total number of successful read-only key lookups in the cache.
cacheHits := serverlessCache.MetricCacheHitCount()

// The 5 minutes average of the total number of bytes used by the data stored in the cache.
bytesUsedForCache := serverlessCache.MetricDataStored()

// The 5 minutes average of the total number of ElastiCacheProcessingUnits (ECPUs) consumed by the requests executed on the cache.
elastiCacheProcessingUnits := serverlessCache.MetricProcessingUnitsConsumed()

// Create an alarm for ECPUs.
elastiCacheProcessingUnits.CreateAlarm(this, jsii.String("ElastiCacheProcessingUnitsAlarm"), &CreateAlarmOptions{
	Threshold: jsii.Number(50),
	EvaluationPeriods: jsii.Number(1),
})

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 ​

accountstringaccountstraccountStringAccountstringAccount*string#
Account which this metric comes from.
colorstringcolorstrcolorStringColorstringColor*string#
The hex color code used when this metric is rendered on a graph.
dimensions{ [key: string]: any }dimensionstyping.Mapping[str, Any]dimensionsMap<String, Object>DimensionsIDictionary<string, object>Dimensions*map[string]interface{}#
Dimensions of this metric.
idstringidstridStringIdstringId*string#
Unique identifier for this metric when used in dashboard widgets.
labelstringlabelstrlabelStringLabelstringLabel*string#
Label for this metric when added to a Graph in a Dashboard.
metricNamestringmetric_namestrmetricNameStringMetricNamestringMetricName*stringrequired#
Name of this metric.
namespacestringnamespacestrnamespaceStringNamespacestringNamespace*stringrequired#
Namespace of this metric.
periodDurationperiodDurationperiodDurationPeriodDurationPeriodDurationrequired#
Period of this metric.
regionstringregionstrregionStringRegionstringRegion*string#
Region which this metric comes from.
statisticstringstatisticstrstatisticStringStatisticstringStatistic*stringrequired#
Statistic of this metric.
unitUnitunitUnitunitUnitUnitUnitUnitUnit#
Unit of the metric.
visiblebooleanvisibleboolvisibleBooleanVisibleboolVisible*bool#
Whether this metric should be visible in dashboard graphs.
warningsstring[]warningstyping.List[str]warningsList<String>Warningsstring[]Warnings*[]*stringdeprecated#
- use warningsV2
warningsV2{ [key: string]: string }warnings_v2typing.Mapping[str, str]warningsV2Map<String, String>WarningsV2IDictionary<string, string>WarningsV2*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) MathExpressionstatic#
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) Grantstatic#
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.