Logs & Traces
Loki, Tempo, OpenTelemetry, and exemplars
Metrics tell you that something is wrong and where. To learn why, you need the other two signals from Why Monitoring: logs and traces. Grafana's open-source stack adds one backend for each, and the real value comes from linking all three.
Loki: logs, the Prometheus way
Loki stores logs, and borrows Prometheus's central idea: streams are identified by labels, and only the labels are indexed. The log text itself is compressed into chunks and searched by brute force at query time. That makes Loki much cheaper to run than full-text indexers like Elasticsearch, at the cost of slower searches over large time ranges.
LogQL looks like PromQL:
# all demo-app logs containing ALERT
{container="demo-app"} |= "ALERT"
# parse key=value pairs, filter on a field
{container="demo-app"} |= "ALERT" | logfmt | severity="critical"
# turn logs into a metric: alerts received per minute, by receiver
sum by (receiver) (count_over_time({container="demo-app"} |= "ALERT" | logfmt [1m]))The last query is the powerful part: LogQL can compute metrics from logs, and Grafana can graph them next to Prometheus data.
Logs get into Loki through an agent such as Grafana Alloy (the
successor to Promtail) or the OpenTelemetry Collector, which tails
container logs and attaches labels like container, namespace, and
app.
Loki has the same cardinality rules as Prometheus. Labels must be low
cardinality: app, env, level. Never a request ID or user ID.
Those belong in the log line, where you filter with |= or parse them
at query time. A high-cardinality label creates millions of tiny
streams and makes Loki slow and expensive.
Tempo: traces
A trace follows one request through every service it touches. Each
hop is a span, with a start time, duration, service name, and
attributes. Spans share a trace ID, passed between services in
HTTP headers (traceparent, from the W3C Trace Context standard).
Tempo stores traces cheaply in object storage, indexed only by trace ID, with TraceQL for searching:
# slow requests to the orders endpoint
{ resource.service.name = "demo-app" && span.http.route = "/api/orders" && duration > 500ms }
# traces where any span errored
{ status = error }Traces come from OpenTelemetry instrumentation: SDKs in every major language, plus auto-instrumentation that wraps common HTTP and database libraries without code changes.
Exemplars: from a metric to a trace
An exemplar is a sample trace ID attached to a histogram bucket observation. When the p99 latency spikes, Grafana shows dots on the graph; each dot links to an actual request that landed in that bucket.
# exposition with an exemplar (OpenMetrics format)
http_request_duration_seconds_bucket{route="/api/orders",le="0.5"} 1832 # {trace_id="4bf92f3577b34da6"} 0.47 1791783000.000Enabling exemplars takes three things: the client library records them
(with the current trace ID), Prometheus is started with
--enable-feature=exemplar-storage, and the Grafana Prometheus data
source has an Exemplars link to the Tempo data source.
Linking the signals in Grafana
Grafana's data source settings wire the signals together:
- Prometheus → Tempo: exemplar links on graphs.
- Tempo → Loki: "Logs for this span" opens Loki filtered to the same service and time range, or the trace ID.
- Loki → Tempo: derived fields turn a
trace_id=...in a log line into a link to the trace. - Tempo → Prometheus: span metrics and service graphs, so traces generate RED metrics automatically.
Each signal alone answers one kind of question. Linked, they turn an investigation into clicks instead of copy-pasting timestamps and IDs between tools. "Error budget burning" → click the spike → see the slow trace → click the failing span → read the exception in its logs. That's the workflow worth building toward.
The OpenTelemetry Collector
In larger setups, a Collector sits between applications and backends: it receives metrics, logs, and traces (OTLP, Prometheus scrape, Jaeger, Zipkin, log files), processes them (batching, sampling, filtering, adding resource attributes), and exports to Prometheus, Loki, Tempo, or any vendor. It decouples instrumentation from where the data ends up.
Extending this stack
A natural next step for the repo is adding to compose_prod.yaml:
loki: grafana/loki # :3100, local filesystem storage
alloy: grafana/alloy # tails Docker container logs -> Loki
tempo: grafana/tempo # :3200, receives OTLP on :4317/:4318plus Loki and Tempo data sources in grafana/provisioning/datasources/,
and OpenTelemetry instrumentation in the demo app (the
opentelemetry-instrumentation packages wrap http.server automatically).
That's the observability half of the Capstone.