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.durationandhttp.server.active_requestsmetrics.
The code
Section titled “The code”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(())}Dependencies
Section titled “Dependencies”[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"Run it
Section titled “Run it”From a checkout of the repository:
cargo run -p oxtel-examples --example webcurl localhost:3000/orders/7For production
Section titled “For production”- Stop gracefully. On a signal, the server finishes the requests in flight and
mainreturns, so oxtel sends what’s pending. Without it, the last few seconds of telemetry are lost. In a container, the platform sendsSIGTERMrather than Ctrl-C: wait for both. - Name the service.
OTEL_SERVICE_NAMEis how backends will show it. - Send it somewhere. Set
OTEL_EXPORTER_OTLP_ENDPOINTto your collector, as in Get started.