Skip to content

A web service

An HTTP service built on axum. Every request gets:

  • a span named by its route, such as GET /orders/{id}, continuing the caller’s trace if it sent one;
  • a log line when it finishes, with its status and duration;
  • the trace id on every log line written while handling it;
  • http.server.request.duration and http.server.active_requests metrics.
examples/web.rs
use axum::Router;
use axum::extract::Path;
use axum::routing::get;
async fn order(Path(id): Path<u32>) -> String {
tracing::info!(order.id = id, "looking up order");
format!("order {id}\n")
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// First, so the HTTP layer finds the metrics recorder. Keep the guard
// until main returns.
let _telemetry = oxtel::init()?;
let app = Router::new()
.route("/orders/{id}", get(order))
.layer(oxtel_tower::HttpServerLayer::new());
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?;
tracing::info!("listening on http://127.0.0.1:3000");
axum::serve(listener, app)
// On Ctrl-C, finish the requests in flight and let main return, so
// the guard is dropped and sends what's pending. Without this,
// Ctrl-C ends the process at once and the last few seconds of
// telemetry are lost.
.with_graceful_shutdown(async {
let _ = tokio::signal::ctrl_c().await;
})
.await?;
Ok(())
}
Cargo.toml
[dependencies]
oxtel = "0.3"
oxtel-tower = { version = "0.3", features = ["axum"] }
axum = "0.8"
tokio = { version = "1", features = ["macros", "rt-multi-thread", "signal"] }
tracing = "0.1"

From a checkout of the repository:

Terminal window
cargo run -p oxtel-examples --example web
curl localhost:3000/orders/7
  • Stop gracefully. On a signal, the server finishes the requests in flight and main returns, so oxtel sends what’s pending. Without it, the last few seconds of telemetry are lost. In a container, the platform sends SIGTERM rather than Ctrl-C: wait for both.
  • Name the service. OTEL_SERVICE_NAME is how backends will show it.
  • Send it somewhere. Set OTEL_EXPORTER_OTLP_ENDPOINT to your collector, as in Get started.