arthrop0d

Brief articles on varied topics.

Metric Exemplars and Trace-Based Incident Investigation

observability

Metric exemplars are extra information stored alongside an aggregate metric sample, usually including a trace ID or span ID that produced that sample. They let you jump from a chart or alert—such as “95th-percentile latency is too high”—to a concrete distributed trace showing one request that contributed to the problem.

The problem exemplars solve

Metrics are excellent for answering whether a system is unhealthy. A counter can show that errors increased, and a histogram can show that request latency crossed a threshold. They are compact because many individual events are combined into time-series data points.

That compression is also the problem. An alert may tell you that checkout requests are slow, but not which request was slow, which service introduced the delay, or which database query was involved. You then switch to logs or traces and search by time, service, route, and other clues. During an incident, that investigation can be slow and ambiguous.

A trace is the record of one request or other operation as it travels through multiple services. A span is one timed operation within that trace, such as an HTTP handler, cache lookup, or database query. A trace ID identifies the whole journey; a span ID identifies one operation in it.

An exemplar preserves a small connection between the compressed metric and one of those individual operations.

A metric says, “many requests had this behavior.” An exemplar can say, “here is one trace where it happened.”

What a metric sample normally contains

Consider a request-duration histogram. A histogram groups observed durations into buckets, such as requests completed within 100 milliseconds, 500 milliseconds, or one second. At each reporting interval, the monitoring system stores values such as the number of observations in each bucket and the total sum.

Those values are aggregates. A bucket might contain 12,000 requests, but the stored metric does not ordinarily retain the identity of any request in that group. Adding a trace ID as a normal metric label would be the wrong solution: every request could create a distinct time series, causing cardinality—the number of unique label combinations—to grow without bound.

An exemplar is different. It is attached to a particular metric data point or histogram observation as optional metadata, rather than becoming part of the metric’s identity. The metric remains aggregatable and low-cardinality, while a limited number of observations retain a pointer to traces.

An exemplar commonly contains:

The exact fields depend on the metric format and storage system. Prometheus-compatible systems, for example, represent exemplars alongside samples in an exposition format, and observability interfaces can use the stored trace ID to construct a trace link.

How the connection is made

The mechanism works while the request is still being processed, when tracing context is available:

  1. A request enters a service and receives a trace ID. The current operation also has a span ID.
  2. Instrumentation measures an event, such as the request duration.
  3. The metric instrumentation updates an aggregate counter or histogram.
  4. For some observations, it records an exemplar containing the observation and the current trace or span identifier.
  5. The metric exporter sends the aggregate metric and its exemplar metadata to the monitoring backend.
  6. When an engineer views the metric, the backend displays an exemplar marker or trace link. Selecting it opens the associated trace in the tracing system.

The metric and trace systems must agree on how to find one another. Typically, the trace ID is stored in a standard field and the dashboard is configured with the tracing backend’s URL pattern. The metric backend does not need to contain the complete trace; it only needs the identifier and enough configuration to link to it.

Exemplars are usually sampled, meaning the system keeps only some eligible observations. Keeping one for every request would bring back much of the storage and cost problem that aggregation avoids. Selection can favor unusual values, such as a slow request, or can use a sampling policy that records occasional examples. Therefore, an exemplar is a useful representative, not a guarantee that the worst request—or every request—will be available.

Following an alert to a trace

Suppose an alert fires because the checkout latency histogram shows an increase in its high-duration buckets. On the dashboard, exemplar markers appear near several metric points. You select one marker and open its trace.

The trace may show that the checkout service spent most of its time waiting for inventory, while the inventory service spent most of its time on a database query. Without the exemplar, the metric established that checkout was slow; the trace explains where the time went for one concrete request.

This is especially useful when the metric’s labels describe broad dimensions such as service, route, and status. Those labels help locate the affected population. The exemplar supplies a specific member of that population for detailed inspection.

Important limits

An exemplar cannot repair missing tracing context. Background work, asynchronous queues, or incorrectly propagated context may produce a metric observation with no usable trace ID. Trace sampling can also mean that the ID exists in the metric but the corresponding full trace was not retained.

IDs should not be placed in ordinary metric labels, and exemplar attributes should be kept small. They are intended as navigation metadata, not as a second event log. Finally, a linked trace explains one observed path; confirm the pattern with other traces, logs, and metrics before treating it as the universal cause.

In practice, the term exemplar in a dashboard, exporter setting, or incident report means: this aggregate data point has a surviving connection to an individual observation, often a trace, that you can inspect next.

← All articles