LogDrop
Backend TracingRust

Add the Crate

Add the LogDrop Rust crate, put its layer in your tracing subscriber and trace the libraries your service uses.

Three changes to your service: add the crate, add its layer to your tracing subscriber at the beginning of main, and add one line for each library you want traced. Without settings the layer does nothing, so the same build runs traced and untraced.

1. Add the crate and its layer

In tracing, a subscriber receives the spans and events that your code and its libraries create, and passes them to its layers. Add the crate to Cargo.toml, then add the LogDrop layer to your subscriber at the beginning of main:

Cargo.toml
[dependencies]
logdrop = { version = "0.1.0-alpha1", features = ["axum", "reqwest"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["fmt", "env-filter"] }
use tracing_subscriber::prelude::*;

#[tokio::main]
async fn main() {
    tracing_subscriber::registry()
        .with(tracing_subscriber::fmt::layer().with_filter(tracing_subscriber::EnvFilter::new("info")))
        .with(logdrop::layer()) // reads LOGDROP_*; no filter of its own
        .init();
    let _logdrop = logdrop::ShutdownGuard; // sends what is still queued when main returns

    // build the router, clients and database pool here
}

logdrop::layer() reads the settings, sets OpenTelemetry's global tracer provider and the W3C trace context and baggage propagators, and writes one start line. Create it before the code that creates servers, clients and database pools. A service without a subscriber of its own can call logdrop::init() instead, which installs one with only the LogDrop layer.

ShutdownGuard sends the spans still queued, for at most 10 seconds, when it is dropped at the end of main. A service that stops through std::process::exit skips that: call logdrop::shutdown() before it if the last spans matter.

Without settings, with invalid settings or with LOGDROP_ENABLED=false, the service runs untraced, with one line on standard error that says why. No call of the crate ever fails your service.

Filters

A filter decides which spans and events a layer sees. Put your log filter on the log layer, as above, not on the whole subscriber. The LogDrop layer chooses on its own: it records spans at INFO level and above (never the DEBUG and TRACE spans of libraries), error events and sqlx's statement events. A filter on the whole subscriber that hides DEBUG events of sqlx::query hides the database statements from LogDrop too.

2. Trace the libraries you use

A span is one operation: a request your service handles, or a call it makes to a database or another service. A Rust binary cannot load an agent at run time, so each library below needs the line shown, with the logdrop feature in the last column.

LibraryLineFeature
axumRouter::new().route(...).layer(logdrop::axum::TraceLayer)axum
tonic serverServer::builder().layer(logdrop::axum::TraceLayer)axum
Other tower HTTP serversServiceBuilder::new().layer(logdrop::axum::TraceLayer)axum
reqwestreqwest_middleware::ClientBuilder::new(client).with(logdrop::reqwest::TraceMiddleware).build()reqwest
sqlxnothing: statements are recordednone

Things to know

These details decide whether a call is recorded, how it is named and whether it joins the request's trace.

  • axum: add the layer with Router::layer (or route_layer), so a request is named by its route template, such as GET /orders/{id}. A layer outside the router sees no route and names the request by its method only.
  • tonic: a gRPC request is named package.Service/Method and carries its gRPC status code when the answer has it in its headers.
  • reqwest: the middleware sends the trace context to the service it calls, so that service's spans join the same trace. Use the ClientWithMiddleware it builds for every call.
  • sqlx: sqlx reports each statement when it finishes; the crate records it as a call of the current span, from its start to its end. The database comes from DATABASE_URL, sqlx's own convention: its system (postgres, mysql, mariadb, sqlite), host, port and database name, never the user, password or parameters. Without DATABASE_URL, statements are recorded with the system other_sql. The statement is sent only as a value-free template.
  • Your own spans: work inside a request joins the request's trace when it runs in the request's span, for example through .instrument(span) on a future you spawn. See Custom spans.

3. Give it its settings

The layer does nothing until it has its settings: at least the traces endpoint, the ingest key and the service name. How you give them depends on where the service runs.

  • On Kubernetes, one pod annotation does it: Kubernetes.
  • Elsewhere, set the LOGDROP_* environment variables: Other hosts.

On this page