Skip to main content

Crate oxtel_reqwest

Crate oxtel_reqwest 

Source
Expand description

HTTP client spans and metrics for reqwest, as the OpenTelemetry semantic conventions describe, with the trace carried on to the server. Part of oxtel.

One line wraps a reqwest client, and requests are made as before:

let client = oxtel_reqwest::client(reqwest::Client::new());
let items = client.get("https://api.example/items").send().await?.text().await?;

Every request gets:

  • a client span named for its method, with http.request.method, server.address, server.port, url.full, network.protocol.version and http.response.status_code. A 4xx or 5xx response, or a request that got no response, is an error, with error.type saying why: the status code, or timeout, connect, redirect, body, decode or request.
  • W3C traceparent and tracestate headers, so a server that reads them, such as one with oxtel-tower’s HttpServerLayer, continues the trace.
  • http.client.request.duration in seconds, by method, server, scheme, status and error.type.

The query string and any credentials in the URL are never recorded, since either may hold secrets. These are the spans and metrics oxtel-tower’s HttpClientLayer records, so a program using both gets the same telemetry from each.

client returns reqwest-middleware’s ClientWithMiddleware, whose methods are reqwest’s own. A program with other middleware adds HttpClientMiddleware with the rest. After retry middleware, each attempt gets a span of its own; a redirect that reqwest follows itself is part of the same span.

let client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new())
    // No trace context for requests that leave your systems.
    .with(oxtel_reqwest::HttpClientMiddleware::new().with_propagation(false))
    .build();

Spans are recorded with tracing and metrics with metrics, so they go wherever the program’s subscriber and recorder send them: with oxtel::init(), to OTLP or Prometheus. Make the client after oxtel::init(), so the duration is recorded in seconds with the conventions’ buckets.

Re-exports§

pub use reqwest_middleware;

Structs§

HttpClientMiddleware
reqwest-middleware middleware that records each request: a client span, trace context sent along, and http.client.request.duration. See the crate documentation.

Functions§

client
client, with every request it makes recorded: HttpClientMiddleware with its defaults.