Sub topic 3
Namespaces and dimensions: identifying a metric
This chapter assumes you know what a CloudWatch metric and data point are, and that you can run AWS CLI commands in a chosen region. It covers how CloudWatch decides which time series a data point belongs to.
A metric has a full identity
A metric is not identified by its name alone. CloudWatch identifies a metric by four parts together:
- Namespace
- Metric name
- Dimensions (zero or more)
- Region and account
Two data points land in the same metric only if all of these match. Change one dimension value and you get a different metric with its own history.
Namespaces
A namespace is a container that groups related metrics and keeps them apart from others. AWS services publish to namespaces that start with AWS/, such as AWS/EC2 or AWS/RDS. Your own metrics go into a namespace you choose, such as MyApp. You can't use the AWS/ prefix for your own metrics.
Namespaces prevent collisions. CPUUtilization exists in several AWS services, and the namespace tells them apart.
Dimensions
A dimension is a name/value pair that further describes a metric. InstanceId=i-0123456789abcdef0 identifies one EC2 instance. A metric can have up to 30 dimensions.
aws cloudwatch put-metric-data \
--namespace MyApp \
--metric-name PageLoadTime \
--dimensions Service=checkout \
--unit Milliseconds \
--value 230This publishes to the metric identified by MyApp, PageLoadTime, and Service=checkout.
The next diagram shows how one metric name splits into separate metrics.
Exact dimension matching
CloudWatch does not aggregate across dimensions for you. Publishing with Service=checkout and then querying with no dimensions returns nothing, because those are different metrics. Queries must supply the exact set of dimensions that was used at publish time.
aws cloudwatch list-metrics \
--namespace MyApp \
--metric-name PageLoadTimelist-metrics shows every dimension combination that exists. Use it to discover the exact identity before you query or build an alarm.
If you want both per-service values and an overall value, publish both: one data point with the dimension and one without. Alternatively, aggregate at query time with metric math.
Common errors
- Empty results from a correct-looking query. The dimension set differs from the published one. Check for a missing, extra, or misspelled dimension with
list-metrics. - Wrong region. Metrics are stored per region. Query the region where the data was published.
- Unbounded dimension values. A value such as a user ID or request ID creates a new metric for every value. This inflates cost and makes the metrics useless for alarms. Use values from a small, stable set, such as service name or environment.
- Changing dimensions later. Adding a dimension to a running application starts a new metric. Existing alarms and dashboards that use the old combination see no new data.
Exercise
You publish PageLoadTime to namespace MyApp with the dimension Service=checkout. You then run get-metric-statistics for MyApp, PageLoadTime, with no dimensions, and get no data points. Why, and how do you find the right query?
Show answer
A metric with no dimensions is a different metric from one with Service=checkout, and CloudWatch does not aggregate across dimensions. Run aws cloudwatch list-metrics --namespace MyApp --metric-name PageLoadTime to see the existing dimension combinations, then repeat the query with --dimensions Name=Service,Value=checkout.