Video in progress
How do you set up one-click navigation in Grafana from a log line to the matching trace, and from a trace to its logs?
The main requirement is that every log line has traceId as a separate field. Then in the Loki data source you link traceId to Tempo through a "derived field" or a JSON field, and in the Tempo data source the "trace to logs" setting opens a Loki query by that ID from a span.
📖 Answer
During an incident, most of the time goes into copying IDs between signals by hand. You should set this up once.
Application side: with Micrometer Tracing, Spring Boot 3 puts traceId and spanId into MDC itself. In text logs they appear automatically in the default pattern on Boot 3.2+ (logging.pattern.correlation), and in JSON logs they are a separate field. JSON is better: you don't have to write a regex.
Loki to Tempo. In the Loki data source you add a derived field:
# Grafana provisioning, Loki data source
jsonData:
derivedFields:
- name: TraceID
matcherRegex: '"traceId":"(\w+)"'
url: '$${__value.raw}'
datasourceUid: tempo
Now every log line gets a "Tempo" button next to it.
Tempo to Loki. In the Tempo data source you configure tracesToLogsV2: which Loki data source to use, which resource attributes to filter by (service.name mapped to the app label), and a slightly wider time range around the trace. When you click "Logs for this span", Loki opens a query like {app="payment"} |= "<traceId>".
Common problems:
- the log has a
traceId, but Tempo has no such trace: sampling. Logs are written at 100% and traces at 10%. That is normal, but you need to explain it to the team; - the Loki labels and the
service.namein the trace don't match, so "trace to logs" returns nothing. This is where a naming standard pays off; - the name of the
traceIdfield differs between services (trace_id,traceId), and one regex doesn't catch them all.
Sign up to mark studied questions and save your progress.