Skip to content

Lesson 2 of 9 · 23m

Context, Correlation, and the Cardinality Trap

Propagate request and trace context through services while controlling high-cardinality labels that quietly explode cost and query latency.

During a multi-service outage, the first argument is rarely about the error. It is about whether two teams are even looking at the same request. Correlation context is how you stop arguing. A trace ID, request ID, tenant, deployment, and span hierarchy turn isolated symptoms into a timeline. But the same power that makes telemetry useful can bankrupt it when every unique value becomes a metric label.

Propagate context at the edges

HTTP services commonly use W3C Trace Context:

traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
tracestate: billing=worker-3,region=us-east

If the edge generates IDs, every downstream service should accept, preserve, and emit them:

import uuid
from flask import request

@app.before_request
def attach_request_context():
    request.request_id = request.headers.get("x-request-id") or str(uuid.uuid4())
    # Add request_id to the logging context for the rest of the request.

Do not invent a new correlation scheme in each service. The goal is boring compatibility: one trace can cross nginx, the API, workers, queues, and scheduled jobs.

Business context needs boundaries

Useful dimensions often include tenant_id, customer_id, order_id, feature, route, dependency, and region. They are not equally safe everywhere. Logs can carry high-cardinality identifiers because each event is a record. Metrics generally cannot because every unique label combination creates a time series.

safe metric labels       risky metric labels
status=success|failure   customer_id=cus_1842
route=/v1/login          request_id=req_9e34a0
dependency=stripe        email=person@example.org
region=us-east           container_id=89f0...

A metric like http_requests_total{route,status,region} is useful. Add customer_id and a busy service may mint millions of series. Prometheus memory rises, dashboards slow down, and the monitoring system becomes the incident.

Use the right tool for each dimension

High-cardinality investigation belongs in logs and traces. Aggregate health belongs in metrics. A practical split:

metrics: request count, error ratio, latency histogram, queue depth
traces:  exact dependency sequence for one request
logs:    exceptional events, business decisions, unique identifiers

When someone says "we need to graph every user separately," they usually need a log query, a trace search, or a bounded experiment — not a permanent metric label.

Normalize routes before labeling

Never label metrics with the raw path if it contains object IDs. /v1/users/42 and /v1/users/43 are one operation. Use the router template:

raw path:      /v1/users/42/invoices/1001
metric route:  /v1/users/{user_id}/invoices/{invoice_id}

Framework middleware often exposes this template. If it does not, normalize explicitly at the route layer. Treat unexpected unmatched routes as a bounded other bucket.

Field notes

  • Propagate correlation IDs into background jobs when work is enqueued; otherwise asynchronous processing becomes a blind spot.
  • Queue messages need their own identifiers plus the original request context. One is not a substitute for the other.
  • Trace sampling can hide rare incidents. Preserve errors and interesting outliers even when successful traffic is sampled aggressively.
  • User IDs in telemetry may be personal data. Apply the same retention and access-control thinking you would to any customer dataset.
  • Cardinality explosions often appear after deploys. Monitor the monitoring system: series count, ingestion bytes, dropped spans, and query latency.

Was this lesson helpful?

Sign in to rate