Skip to content

Lesson 7 of 13 · 13m

A Label Is a Database Row

Every unique labelset mints a series held in RAM, and http.route is the fix for path-shaped cardinality explosions.

In Prometheus, a label is not a tag on a metric. A metric name plus the complete set of label name/value pairs is the series — a row in the TSDB's Head block, held in RAM, indexed by postings lists. http_requests_total{path="/users/8471", status="200"} and http_requests_total{path="/users/8472", status="200"} are two different series, two allocations, two index entries. Every new label value combination mints a new series whether or not anything ever queries it.

This is the bill that arrives labeled "cardinality," and it multiplies, it doesn't add. Fifty pods × forty routes × five status classes = 10,000 series for one metric. Add a le dimension — a histogram with twelve buckets — and each labelset costs twelve bucket series plus _sum and _count, so that "one metric" is really fourteen series per labelset. The day someone ships user_id or a raw path as a label, the Head block starts minting a series per request, ingestion churn climbs, GC pressure climbs with it, and the server's memory curve looks like a launch trajectory. prometheus_tsdb_head_series is the metric that tells you you did this; scrape_samples_scraped shows the sample volume per scrape and scrape_series_added shows how much of it was brand-new — the churn detector, since a steady service should mint near-zero new series after warm-up.

"Labels are tags" is the wrong model

Tags imply an unconstrained set you filter on. Prometheus labels are dimensional axes — every axis's cardinality multiplies the storage. Grafana Labs' own cardinality postmortems name this the leading cause of Prometheus OOMs and crashloops, and every hosted backend (Mimir, Cortex, M3DB) enforces per-tenant series limits that reject your writes with 400s once you cross them. The failure mode isn't slow queries; it's the TSDB eating its RAM or your remote-write queue silently dropping.

Where unbounded values come from

Instrumentation defaults. An HTTP auto-instrumentor that uses the request path puts /users/8471 — or worse, /reset-password?token=..., which also leaks secrets into your metrics — into a label. This is why OpenTelemetry's semantic conventions separate http.route (the template, /users/:id, bounded by your route table) from url.path (unbounded, an attribute, never a Prometheus label). http.target existed in older semconv versions and carried path+query — if your instrumentation predates the split, that label is the same trap under an old name. If your metrics contain raw paths, your instrumentation is emitting a series mint, not a metric.

The fixes are concrete

Bound values at instrumentation: use http.route, collapse ID-shaped segments, cap the distinct values any label can take. Drop what you don't need at scrape time with metric_relabel_configs — action: drop for whole metrics, labeldrop/labelkeep for axes — before remote write amplifies them. A metric_relabel_configs entry like {action: labeldrop, regex: "(net|app)_.*"} strips the infra-label namespaces exporters pile on when you don't need them. sample_limit, target_limit, and label_limit per scrape_config bound what a single target can emit. On the backend side, Mimir/Cortex's max_global_series_per_user and per-metric limits exist so one team's session_id label can't take down the tenant.

Practice

  • Every label value must be bounded by something you can enumerate: routes, status classes, regions. If you can't list the possible values, it isn't a label.
  • Track prometheus_tsdb_head_series per job and set a cardinality budget per metric — under ~10k series is a sane ceiling for a single instrumentation metric.
  • Histograms multiply by bucket count; choose buckets deliberately (next lesson) and never add a label axis to a histogram casually.
  • Drop before remote write: relabeled-away series never leave your RAM, and that's the cheapest place to kill them.

Cardinality isn't a scaling problem that arrives at millions of requests. It's a schema decision that arrives at the first path label.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.