Tracing an Axum app end to end with OpenTelemetry

From a tracing span in a handler to a trace in Tempo, with logs in Loki correlated by trace_id. A minimal, production-shaped setup.

1 min read

Rust in production · part 1 of 1

Cet article existe en français

tracing is the logging and instrumentation crate of the Rust async ecosystem. OpenTelemetry is the vendor-neutral protocol to ship that data somewhere useful. Together they give you traces, metrics and logs from one set of annotations.

The pipeline#

text
onhlt ──OTLP/gRPC──▶ OpenTelemetry Collector ──▶ Tempo       (traces)
                                              ├─▶ Prometheus  (metrics)
                                              └─▶ Loki        (logs)
                                                       ▲
                                                 Grafana reads all three

The application only knows one endpoint, the Collector. Swapping a backend never touches Rust code.

Instrumenting handlers#

rust
#[tracing::instrument(skip(app), fields(lang = %ctx.lang))]
async fn article(State(app): State<AppState>, ctx: Ctx, Path(slug): Path<String>) -> Response {
    let post = app.db.post(ctx.lang, &slug).await?;
    // ...
}

Each request gets a root span from tower-http’s TraceLayer. SQL queries, cache lookups and template rendering become child spans, so a slow page shows exactly where the time went.

Correlating logs and traces#

Logs are emitted as JSON with the current trace_id. In Grafana, a log line in Loki links straight to its trace in Tempo, and back.

What to measure first#

  • Latency per route, at p50, p95 and p99.
  • Cache hit ratio.
  • Duration and outcome of each content sync.
  • Remaining GitHub API quota.

Four numbers, and you know whether the site is healthy.

Written by

Software, backend and DevOps engineer. I write Rust, run Kubernetes and measure everything with OpenTelemetry.

↑↓ navigate ↵ open Esc close