# 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](https://flowhawk.dev/docs/signals#traces).
- Requests matched to your [business flows](https://flowhawk.dev/docs/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](https://flowhawk.dev/docs/signals#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](https://flowhawk.dev/docs/signals#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](https://flowhawk.dev/docs/help) 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](https://flowhawk.dev/docs/languages#rust)).

### Can I use plain OpenTelemetry for Rust?

Yes. `opentelemetry-otlp` with the `OTEL_` settings from the [language guides](https://flowhawk.dev/docs/languages) sends to Flowhawk too. The SDK adds flow tags, sampling per flow, panic reports and the logs bridge.

## Related

[**OpenTelemetry for Go**Trace a Go service on net/http, Gin, chi or echo, and send its requests, queries, logs and panics to Flowhawk.](https://flowhawk.dev/integrations/opentelemetry-go) [**Kubernetes monitoring**Pod logs, node and pod metrics and your services' traces from a Kubernetes cluster, with two Helm charts you may already know.](https://flowhawk.dev/integrations/kubernetes-monitoring) [**LLM observability**See what your app's model calls cost, how slow they are and which business flow they serve, from the OpenTelemetry data you may already send.](https://flowhawk.dev/integrations/llm-observability)

[Try it free for 14 days](https://app.flowhawk.dev/signup?trial=1) [Read the getting started guide](https://flowhawk.dev/docs/getting-started)

---

URL: https://flowhawk.dev/integrations/opentelemetry-rust
