How-To Guide

How to Control Metric Cardinality

Keep time-series storage affordable by limiting label combinations, avoiding unbounded dimensions and aggregating high-cardinality data.

On this page

  1. Introduction
  2. What Cardinality Is and Why It Explodes
  3. Finding the Expensive Metrics
  4. Choosing Labels Wisely
  5. Bounding and Dropping Labels
  6. Aggregating Instead of Exploding
  7. Detecting Cardinality Regressions
  8. Conclusion

Introduction

A metric is identified by its name plus its label set. The number of distinct label combinations is the series count, and it is multiplicative: ten methods, five paths, three statuses and four regions is six hundred series from one metric name. Add a user id or a request id as a label and the series count explodes to one series per user, which can take a monitoring backend from megabytes to terabytes overnight and slow every query. Cardinality control is the discipline of keeping label sets small and bounded. This article shows how to find the expensive metrics, choose labels that stay bounded, and aggregate the rest without losing the signal you need.

What Cardinality Is and Why It Explodes

Each distinct combination of label values creates a separate time series, stored and indexed independently. Cardinality is the product of the number of values each label can take. That multiplication is why a small change hurts: adding one new label with a hundred values multiplies storage by a hundred. Unbounded labels such as user_id, session_id, request_id or a raw URL path turn a compact metric into a per-entity record, which defeats the purpose of aggregation.

High cardinality hurts in three ways. Storage and cost grow with series count. Queries slow down because the engine must consider more series. And the backend may drop the whole metric when it hits a limit, so you lose even the aggregated view you wanted.

Finding the Expensive Metrics

Most backends expose the series count per metric. Query the worst offenders by their label combinations, not by sample rate. A metric with a handful of series and a million samples per minute is cheap; a metric with a million series is expensive regardless of sample rate. Exporters such as the Prometheus client libraries also report how many series each metric produced, which makes the offender obvious.

# Prometheus: top metrics by number of active series
topk(20,
  count by (__name__) ({__name__=~".+"})
)

# Series count for one metric
count(http_request_duration_seconds_bucket)

# Find metrics that have grown a new high-cardinality label
count by (__name__) ({job="api"})

Choosing Labels Wisely

A good label is bounded, low-cardinality and useful for grouping. HTTP method, route template, status class, region and version are good: each has a small fixed set of values and you routinely filter or group by it. A route template such as /orders/:id is bounded; the raw path /orders/8f3a is not. User, request, session and trace identifiers are bad labels because they are unique per event; if you need to correlate by them, do it in traces or logs, not in metrics.

The test to apply: can you enumerate the value set in advance and would you ever want to sum across it? If the answer is no, it does not belong in a label. Move the unbounded dimension to an event or a log line and keep the metric aggregate.

Bounding and Dropping Labels

When a library emits a label you cannot change, relabel at the collector. Keep an allowlist of labels and drop everything else, or hash an unbounded value into a bounded bucket. Rewriting a URL path to its route template and collapsing unknown methods to a single value are the two most common rewrites. Do the relabeling at the collector so the change applies to every service without a redeploy.

# OpenTelemetry collector: keep only a bounded label set
processors:
  metricstransform:
    transforms:
      - include: http_requests_total
        action: update
        operations:
          - action: aggregate_labels
            label_set: [method, route, status_class, region]
  transform:
    metric_statements:
      - context: datapoint
        statements:
          # Drop an unbounded label entirely
          - delete_key(attributes, "user_id")
          # Replace unknown methods with a single bucket
          - set(attributes["method"], "OTHER") where attributes["method"] not in ["GET","POST","PUT","DELETE"]

service:
  pipelines:
    metrics:
      receivers: [otlp]
      processors: [metricstransform, transform]
      exporters: [prometheusremotewrite]

Aggregating Instead of Exploding

Often you do not need per-entity detail in metrics at all; you need the distribution. Instead of labelling by user id, record a histogram without that label and answer “how many users had a slow request” from traces when you need the detail. Summarize the tail with percentiles or a histogram of latencies, and keep the sum and count so you can compute an average and a rate. This preserves the aggregate signal that alerting depends on.

If a dimension is genuinely needed for a small, fixed set, label it. If it is needed for a large set, push it to an event stream or a trace attribute and compute the metric on the aggregate. The rule of thumb: metrics answer “how much” broadly, traces answer “which one” specifically.

Detecting Cardinality Regressions

Cardinality grows from code changes, so alert on the series count of your most expensive metrics, not only on their value. A pull request that adds a user id to a label will show up as a step change in series count before it shows up as a bill. Add a check in code review or CI that forbids known-unbounded label names in metric definitions.

Keep a budget: a handful of labels per metric and a bounded value set. When you exceed it, decide explicitly whether the new dimension is worth the cost. This mirrors capacity planning for any resource: measure, set a limit, and treat crossing it as a decision point.

Conclusion

Cardinality control is about keeping label sets bounded so a monitoring backend stays fast and affordable. Measure the series count per metric, keep only the labels you would group by, relabel at the collector to drop or bucket unbounded values, and push per-entity detail into traces instead of metric labels. Alert on series-count growth so a careless label change is caught in review rather than on the bill. The result is metrics that stay queryable at scale.

Sources: Prometheus: cardinality, OpenTelemetry Collector processors, Prometheus: understanding metric cardinality.

FAQ

Why are user ids so bad as metric labels?
Because a label value that is unique per user multiplies the series count by the number of users, turning an aggregate into a per-entity record. That inflates storage, slows queries and can make the backend drop the metric. Put user-level detail in traces or logs instead.
Can I keep high-cardinality data somewhere without hurting metrics?
Yes. Record the aggregate metric without the unbounded label, and send the per-entity detail to a trace or an event stream. Metrics answer how much in aggregate; traces answer which specific request was involved.

Related Examples