Course contents · Lesson 19 of 21

Correlate Metrics, Logs and Traces in One Workflow

Putting It Together · Lesson 19 of 21 · 11 min Hands-on

An alert says checkout's p95 latency has jumped. Prometheus tells you when, Jaeger holds hundreds of traces from that minute and Elasticsearch thousands of log lines. Matching timestamps by eye across three tools is slow guesswork. This lesson shows how to correlate metrics, logs and traces through one shared ID: you wire Grafana to all three stores, then walk from a latency spike to the slow span and its log line.

Shared identifiers: what links the signals

To correlate signals is to find the evidence that describes the same event. Time alone is a weak link: the lab's shop serves about 300 requests a minute, so "the same minute" matches hundreds of traces and log lines. A shared identifier, one value written into every signal, narrows that to one request.

The strongest one is the trace_id, the 32-character hex ID that every span of one request shares; Context Propagation showed how it travels. Since lesson 14, shop-demo-otel writes it into every log line of a traced request, next to span_id, the ID of the request's own span.

Metrics can't carry it as a label: a value that changes with every request creates a new series per request, as Metric Labels and Cardinality explained. Metrics share coarser identifiers instead: here service="shop-demo-otel" and route="/checkout", the names the traces and logs use.

An exemplar bridges the gap: one example measurement stored beside a series, outside its labels, with a value, a timestamp and labels of its own, here a real request's trace_id. Each histogram bucket keeps the latest one from a request that landed in it, so a slow bucket points at a slow trace. In the diagram, notice that all three signals hold the same string.

One trace_id, 4bf92f3577b34da6a3ce929d0e0e4736, with arrows to three cards. Metric exemplar from Prometheus: a histogram bucket line for route /checkout ending in an exemplar with that trace_id, value 1.09 seconds, at 10:05:13. Trace in Jaeger: GET /checkout 1.09 s with inventory.lookup 46 ms, SELECT and payment.charge 1.04 s, and the same Trace ID. Log line in Elasticsearch: message order confirmed, route /checkout, duration_ms 1093.4 and the same trace_id. Two Grafana links lead from the exemplar and from the log field to the trace: Query with Jaeger and View trace in Jaeger. Values illustrative.
The app writes one string into the exemplar and the log line, so either one can open the trace.

The lab wiring in 06-correlate-signals

The new lab runs one traced app and the stores from earlier sections: Prometheus, Jaeger behind the Collector, and lesson 17's Filebeat and Logstash pipeline into Elasticsearch. Its shop-demo-otel is lesson 14's code, mounted unchanged, plus app/shop_metrics.py, which serves metrics on port 9464 and attaches the exemplar after each request:

span_context = trace.get_current_span().get_span_context()
exemplar = {"trace_id": format(span_context.trace_id, "032x")} if span_context.is_valid else None
DURATION.labels(request.method, route).observe(seconds, exemplar=exemplar)

DURATION is lesson 6's histogram http_request_duration_seconds, built with the Python library prometheus-client. format writes the ID as 32 hex characters, exactly as the log lines do. If the forms differed, even by one leading zero, they would never match.

Exemplars need two more things. They exist only in OpenMetrics, a newer text format for /metrics, which the library serves when the scraper's Accept header asks for it, as Prometheus 3 does by default. And Prometheus keeps them only with --enable-feature=exemplar-storage, which the lab's compose file sets. To see them, ask for OpenMetrics yourself (one line, trimmed, values illustrative):

$ curl -s -H 'Accept: application/openmetrics-text' localhost:9464/metrics | grep trace_id
http_request_duration_seconds_bucket{le="0.25",method="POST",route="/checkout"} 41.0 # {trace_id="9e1c…"} 0.1834 1795689913.12

After the # come the exemplar's labels, the request's duration in seconds and when it was recorded. Without the header you get the classic format, which has no exemplars.

Grafana gets all three stores as provisioned data sources, from grafana/provisioning/datasources/datasources.yml. Two settings make the links (trimmed):

  - name: Prometheus
    jsonData:
      exemplarTraceIdDestinations:
        - name: trace_id
          datasourceUid: jaeger
  - name: Elasticsearch
    jsonData:
      dataLinks:
        - field: trace_id
          datasourceUid: jaeger
          url: '$${__value.raw}'
          urlDisplayLabel: View trace in Jaeger

exemplarTraceIdDestinations names the exemplar label that holds a trace ID and the data source that opens it, uid jaeger. A data link does the same for a log field: as an internal link, one that opens another Grafana data source, it sends the field's raw value to Jaeger as the query. Loki calls this a derived field. The $$ stops Grafana from reading ${…} as an environment variable.

The workflow: from a latency spike to the suspicious span

The flowchart is the routine for a "checkout is slow" alert. Notice that after step 3 you stop matching by time: the trace_id carries you onward.

Flowchart. 1: an alert or report, checkout p95 is high. 2: spot the spike on the p95 panel and note the minute. Decision: an exemplar marker near the spike? Yes: 3, click the marker and choose Query with Jaeger. No: 3, search Jaeger for the service with Min Duration 1s. Both lead to 4: read the trace, find the span whose own time is long and keep its trace_id, because the same string is in the log lines. 5: read its logs with trace_id:"4bf92f35…". 6: note the culprit, payment.charge at 1.04 s. Dashed: next lesson, a profile, for which code inside the span is slow.
Metrics find the minute, an exemplar or a search finds the trace, and its ID finds the logs.

Spot the spike, then find a slow trace

The lab's Shop Correlations dashboard has a p95 panel for /checkout with the query's Exemplars option on. Each marker is one exemplar: hover over it to see its trace_id, then click Query with Jaeger to open that trace in Explore.

No marker near the spike? Search Jaeger as in lesson 15: service shop-demo-otel, Min Duration 1s, a lookback covering the spike. Follow the longest bar down until no child explains its time: here payment.charge fills about 1 s of a 1.09 s request.

Read that trace's logs, note the suspicious span

In Explore, pick the Elasticsearch data source, choose the Logs query type and search trace_id:"…" in Lucene syntax, Elasticsearch's classic query language. Kibana, which this lab leaves out to save memory, takes the KQL query trace_id: "…" on a data view like lesson 18's. One line comes back, with details such as order_id that a support ticket might quote.

In the line's details, the trace_id field carries the View trace in Jaeger link back. The suspicious span is payment.charge: the shop waited on its payment provider.

The timeline shows why the ID matters: the p95 jump, the exemplar, the trace and the log line share a minute, but only the ID singles out one request. Notice too that the exemplar reads 1.09 s while p95 shows about 2 s: p95 is estimated between bucket bounds, the exemplar is one real request.

Three panels on one time axis from 10:02 to 10:07, with the minute 10:05 highlighted. Metric: p95 latency near 0.5 s jumps to about 2 s during 10:05 and falls back after 10:06; exemplar markers sit low before and near 1 s during the spike, one highlighted: trace_id 4bf92f35, 1.09 s. Trace, zoomed in: started 10:05:12.0, GET /checkout 1.09 s, inventory.lookup 46 ms, SELECT 0.3 ms and payment.charge 1.04 s, the step to look at. Logs: a tick per log line, then 10:05:11.9 info GET /products; highlighted, 10:05:13.1 info order confirmed, duration_ms 1093.4, trace_id 4bf92f35, written as it ended; then 10:05:13.4 error checkout failed. Values illustrative.
Time puts the three signals in the same minute; only the shared ID picks out the same request.

Tips, and what is still missing

Name things once. Use the same service name and route values in all three signals: a query for service="shop" finds nothing when the logs say shop-demo-otel.

Mind the time range. Explore searches metrics and logs only inside its time picker, so start a few minutes before the spike. A log line is stamped when its request ended, and Grafana shows your browser's time zone while the app logs UTC.

Don't over-link. Link only fields that answer the next question, and never turn trace_id into a metric label. With trace sampling, a log line may name a trace that was never kept, and empty pages teach people to ignore links. This lab keeps every trace.

A trace shows which step was slow, not which code inside it. payment.charge only waits, so the trace says enough. GET /report is one long span with no children, its time spent computing. Only a profile, a record of where CPU time went, names the function; the next lesson adds one.

Check yourself

A teammate wants a trace_id label on http_requests_total, so every request links to its trace. Why are exemplars the better choice?

A per-request label value creates a new series for every request and swamps Prometheus. An exemplar stores the ID beside the existing series, for a sample of requests.

The p95 panel shows a spike but no markers, yet curl on port 9464 with the OpenMetrics header prints trace_id lines. Where do you look?

After the app: either Prometheus runs without --enable-feature=exemplar-storage and drops them, or the panel's query has the Exemplars option off.

You find a slow trace from 40 minutes ago. With Explore on Last 15 minutes, the Elasticsearch search for its trace_id returns nothing. Is the log line missing?

Probably not. Explore searches logs only inside its time picker, and this line is 40 minutes old. Widen the range.

Go deeper