123DevOps··5 min read

Controlling cardinality explosions in Prometheus and Mimir

What cardinality is, how to find the metrics and labels that generate the most series, how to fix it at the source and at scrape time, protective limits, and governance so the explosion doesn't come back.

Observability

One day Prometheus starts using twice as much memory. Queries get slow. In Mimir (or any managed service), the bill goes up or the per-tenant series limits start rejecting data. Most of the time, the cause has a name: cardinality explosion.

What cardinality is

Each unique combination of metric name and label values is a time series. A metric’s cardinality is the product of those combinations:

http_requests_total{method, route, status, pod}

  5 methods × 50 routes × 10 statuses × 100 pods = 250,000 series

Now someone adds user_id with 100,000 active users. In practice, not every combination exists, but the number of series can jump into the millions, from a single metric.

Every active series takes up memory in Prometheus and disk space and, in managed services, goes straight into the cost. Cardinality is the main cost and stability lever of a metrics environment.

The usual suspects

  • Unique identifiers as labels: user_id, order_id, request_id, session_id.
  • Full URL instead of the route: /users/123/orders/456 instead of /users/{id}/orders/{id}.
  • Error messages as labels: every different text becomes a series.
  • Ephemeral Kubernetes labels: pod, pod_template_hash, container_id on metrics that don’t need them; every deploy creates new series.
  • Histograms with too many buckets: each bucket is a series, multiplied by all the other labels.
  • Metrics nobody uses: exporters collecting hundreds of metrics by default.

Finding the heavy series

In Prometheus

The TSDB Status page (/tsdb-status) shows the metrics with the most series and the labels with the most values. To investigate with PromQL (these are heavy queries, so run them with care):

# the 10 metrics with the most series
topk(10, count by (__name__) ({__name__=~".+"}))

# how many distinct values a label has in a metric
count(count by (route) (http_requests_total))

In Mimir

Mimir exposes per-tenant cardinality analysis APIs (/api/v1/cardinality/label_names and /api/v1/cardinality/label_values), and Grafana has ready-made dashboards for this. mimirtool cross-references the stored metrics with the ones that are actually used in dashboards and rules:

mimirtool analyze grafana --address=https://grafana.example.com --key="$GRAFANA_TOKEN"
mimirtool analyze ruler --address=https://mimir.example.com --id=my-tenant
mimirtool analyze prometheus --address=https://mimir.example.com --id=my-tenant

The result is usually eye-opening: a significant share of the stored series doesn’t appear in any dashboard, alert, or rule.

Fixing it: from source to storage

1. At the source (the best place)

  • Use route templates (http.route in the OpenTelemetry conventions), never the raw URL.
  • Put unique identifiers in traces and logs, not in metrics (details in MTTR with business context).
  • Group values: status_class="5xx" instead of each individual code, when the detail isn’t needed.
  • Review histogram buckets: start with few, aligned with your SLOs. Prometheus native histograms greatly reduce this cost; check support in the version you run.

2. At scrape time, with relabeling

Drop metrics and labels before they’re stored:

scrape_configs:
  - job_name: apps
    kubernetes_sd_configs:
      - role: pod
    sample_limit: 50000          # protects against a target that explodes
    label_limit: 30
    label_value_length_limit: 200
    metric_relabel_configs:
      - source_labels: [__name__]
        regex: "go_gc_duration_seconds.*|process_.*"
        action: drop
      - regex: "pod_template_hash|controller_revision_hash"
        action: labeldrop

With sample_limit, a target that exceeds the limit has its entire scrape rejected (and the up == 0 alert lets you know), which is better than taking down the whole Prometheus.

3. In the OpenTelemetry pipeline

If metrics go through the Collector, remove high-cardinality attributes before exporting, using the transform or attributes processors, as shown in telemetry pipelines.

4. Aggregating with recording rules

Many dashboards don’t need the pod label. Create recording rules that aggregate, and query the aggregated version:

groups:
  - name: aggregations
    rules:
      - record: service:http_requests:rate5m
        expr: sum by (service, route, status_class) (rate(http_requests_total[5m]))

On managed platforms, automatic aggregation features (such as Grafana Cloud’s Adaptive Metrics) do something similar at ingestion, dropping the labels nobody queries.

Limits as a seat belt

In Mimir, per-tenant limits keep one team’s mistake from taking down the cluster for everyone:

overrides:
  payments-team:
    max_global_series_per_user: 2000000
    max_global_series_per_metric: 200000
    max_label_names_per_series: 30

When the limit is reached, new series are rejected and the problem stays confined to whoever caused it. Monitor how close you are to the limits and alert before hitting them.

Governance so it doesn’t come back

  • Metric review in PRs: new labels and new metrics go through a checklist (does the label have bounded values? will anyone use it?).
  • Documented naming and label conventions, aligned with the OpenTelemetry semantic conventions.
  • Series budget per team, visible on a dashboard of active series by job or team.
  • Quarterly cleanup with mimirtool: whatever isn’t used gets dropped at scrape time.
  • Growth alert: a sudden change in active series per job points to a deploy with a problematic label.

Checklist

  • Top 10 metrics by series count identified
  • No unique IDs or raw URLs as labels
  • Histogram buckets reviewed
  • metric_relabel_configs dropping unused metrics and labels
  • sample_limit and per-tenant limits configured
  • Recording rules for the most-queried dashboards
  • Dashboard of active series per team and a growth alert

Every series costs memory, disk, and money. The question for every new label is simple: how many values can it have, and who is going to query it?

Get updates on new posts and stay in the loop!