# Metric cardinality

## What it is

Metric cardinality describes how many distinct time series a metric can produce
when its dimensions take different values. A request counter could have
dimensions for method, route and status. Each observed combination can create a
separate series. Those dimensions make the counter useful for questions about a
route or type of result. They also influence how much data the monitoring system
keeps and processes.

The number of values in one dimension matters together with the existing
dimensions. If an application adds an individual order identifier to a metric,
it can produce a new combination each time a different order is processed. It
may create many series even if the service has only a few routes. A value that
changes frequently can cause series churn. The number active at a particular
moment and the number that appear over a longer period answer different
questions.

Every monitoring system defines the details of how it identifies a series and
applies limits. In the Prometheus model a metric name and its set of label
values identify a series. OpenTelemetry metric processing can apply limits to
distinct attribute combinations. Read the configuration and check which
attributes are added by instrumentation, collectors and the backend, because all
those stages can affect what finally arrives.

## Why it matters

Operators need enough dimensions to diagnose real changes. A request metric
divided by a stable route template might reveal that repeated failures affect
exports. If the application records every actual URL as a label, a route
containing an order ID can create thousands of separate series from that same
pattern. The useful group remains the route. An individual identifier is often
easier to examine in a carefully controlled trace or log.

High cardinality can affect more than storage. The monitoring system may spend
time creating series or updating indexes, and a query can ask for a large set of
combinations. Increased pressure during an incident is particularly costly. An
error that includes a different exception message per request can cause a new
series for each failure at the time somebody most needs the aggregate signal.

A dimension can still deserve its cost when it answers an important question. An
application might need to understand whether a request delay affects one of a
few supported regions. Budget the number of values, normalise their meaning and
verify what happens to a value outside the expected set. That decision gives the
metric an intentional scope and a clear limit on the combinations it can
produce.

## What it looks like in practice

Consider a small API whose request metric records method, route template and
status family. With five templates, two common methods and three observed status
families, there could be up to thirty combinations for those labels if every
combination occurred. Added dimensions such as a deployment environment or
replica can increase the total. Measure the actual distribution and the number
of newly created series over a representative interval.

Try a fixture that requests many distinct order URLs. The instrumentation should
record their stable route template while retaining the method and outcome
information that operators need. Inspect the labels the collector receives and
the latest data stored by the monitoring backend. A check at the source can miss
values appended later by a shared adapter. Compare both sides before relying on
a limit.

Engineer's
[Grafana and Prometheus monitoring case study](/portfolio/cut-operational-risk-with-grafana-and-prometheus/)
describes selecting meaningful measures and connecting alert thresholds with
operations. The route example shows one way to keep such a measurement useful as
traffic grows. Preserve the grouping that lets an operator find an affected
route and provide a separate way to investigate an individual request when
needed.

Also review what happens on release. A deployment can create new instance labels
even when routes and outcomes stay the same. Decide how long those old series
remain relevant and how a query groups over current and previous instances. A
fixture that repeats the same traffic through two application revisions can show
whether the change increases the number of combinations and changes an alert's
meaning.

## Where it goes wrong

A label can accidentally capture an unpredictable value while the code looks
reasonable. An exception class might come from a small list, while its detailed
message can contain filenames, identifiers or sensor positions. Choose stable
categories for aggregate measures and verify that the detail path follows the
application's privacy and retention policy. Excess detail in labels can create
both a cost and information exposure.

Limits need testing as well. A system may combine excess attribute combinations
into an overflow series, refuse them or apply a different configured policy.
Verify what happens when a test input exceeds the configured limit. A quiet
dashboard can conceal that observations were dropped or grouped. Give operators
a way to detect an unexpected increase in cardinality before it erases the
distinctions they use during a failure.

A capacity limit can apply to active series, series created per minute or
allocated storage. Record which measure is constrained before tuning labels. A
workload with a moving process identifier may keep only a small number of active
series while creating many new ones over the retention period. Test a rolling
replacement and examine both counts. This shows whether a change improves the
particular resource the team wanted to protect.

Finally, attach an owner and purpose to each label. Review series count, churn
and query cost when a new dimension is proposed. Retain a small fixture covering
known routes and deliberately varied inputs. A useful metric lets the team ask
consistent questions about the workload without letting one new field make
collection or diagnosis unreliable.

## Further reading

- [Prometheus instrumentation guidance on labels](https://prometheus.io/docs/practices/instrumentation/#do-not-overuse-labels).
  Discusses choosing dimensions and the cost of large combinations.
- [OpenTelemetry SDK cardinality limits](https://opentelemetry.io/docs/specs/otel/metrics/sdk/#cardinality-limits).
  Describes limits and overflow handling.
- [The Prometheus data model](https://prometheus.io/docs/concepts/data_model/).
  Defines how names and labels identify time series.

<https://engineer.company/glossary/cardinality/>
