OpenTelemetry for Rust and axum

Trace a Rust web service with the tracing crate you already use, and send its requests, queries, logs and panics to Flowhawk.

The Flowhawk Rust SDK is OpenTelemetry for Rust with the setup done. Add its tracing layer once and the spans and events your code and libraries already emit (axum through tower-http, sqlx, tonic, reqwest-middleware) become traces and logs.

What you get

  • Every request as a trace, with its database calls and outgoing HTTP calls, in Traces.
  • Requests matched to your business flows by service and route on our side, so you see "checkout is failing at place order" rather than a list of endpoints.
  • Exceptions grouped into Issues, slow and N+1 database queries on each database's page, and deploys found from service.version.
  • Alerts, incidents and the on-call agent, which reads these traces when something breaks.

Set it up

1. Install

cargo add flowhawk                  # Rust 1.85 or later
cargo add tower-http --features trace
cargo add tracing tracing-subscriber

2. Start it first in main, and name each request by its route

use axum::{body::Body, extract::MatchedPath, http::Request, response::Response};
use axum::{routing::post, Router};
use tower_http::trace::TraceLayer;
use tracing_subscriber::prelude::*;

#[tokio::main]
async fn main() {
    let options = flowhawk::Options { service: "orders".into(), ..Default::default() };
    let telemetry = flowhawk::init(options).unwrap();
    tracing_subscriber::registry().with(telemetry.tracing_layer()).init();

    let app = Router::new().route("/orders/{id}/pay", post(pay)).layer(
        TraceLayer::new_for_http()
            .make_span_with(|req: &Request<Body>| {
                let route = req.extensions().get::<MatchedPath>();
                let route = route.map(|p| p.as_str());
                flowhawk::request_span(req, route)
            })
            .on_response(
                |res: &Response, _: std::time::Duration, span: &tracing::Span| {
                    flowhawk::record_status(span, res.status().as_u16());
                },
            ),
    );
    let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await;
    let listener = listener.unwrap();
    axum::serve(listener, app).await.unwrap();
    telemetry.shutdown();
}

Each request is named POST /orders/{id}/pay and continues the caller's trace. sqlx queries become database spans inside it, an error logged with tracing::error!(error = &e as &dyn std::error::Error, "charge failed") shows in Issues with its backtrace, and panics are sent before the process goes on as it would.

3. Point it at Flowhawk

First create an ingest key: in the app, open Settings > API keys and pick Services. In the examples below, KEY stands for that key. Keep it out of your repository, like any other secret.

FLOWHAWK_API_KEY=KEY
FLOWHAWK_CONTROL_URL=https://api.flowhawk.dev
OTEL_EXPORTER_OTLP_ENDPOINT=https://ingest.flowhawk.dev
FLOWHAWK_ENVIRONMENT=production

Check it arrived

Open Setup in the app. It lists each service that sent data in the last 15 minutes. If yours isn't there, Help and troubleshooting says why telemetry can be refused (a wrong key, an empty balance) and how to see it.

Common questions

What about actix-web or hyper?

Start a span per request with flowhawk::http::ServerSpan::start(method, uri, headers) and end it with the status code. It works with anything built on the http crate's types (Rust guide).

Can I use plain OpenTelemetry for Rust?

Yes. opentelemetry-otlp with the OTEL_ settings from the language guides sends to Flowhawk too. The SDK adds flow tags, sampling per flow, panic reports and the logs bridge.