Skip to content

Two services

Two HTTP services, one calling the other: a shop that asks an inventory service how many of an item are in stock before it takes an order. One trace covers the whole order:

  • the shop’s request span, GET /orders/{item};
  • inside it, a client span for its call to the inventory, GET, which sends the trace along in a traceparent header;
  • the inventory’s request span, GET /stock/{item}, which picks the trace up from that header.

A backend shows the three as one tree, and every log line from either service carries the same trace id.

Here both services run in one program, which sends the shop one order and stops, so one command shows both sides. Each of yours is a program of its own. The trace still crosses from one to the other, because it travels in the request’s headers.

examples/two-services.rs
use axum::Router;
use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::routing::get;
use oxtel_reqwest::reqwest_middleware::ClientWithMiddleware;
use oxtel_tower::HttpServerLayer;
use tokio::net::TcpListener;
/// The inventory service: how many of an item are in stock.
async fn stock(Path(item): Path<String>) -> String {
tracing::info!(item, "counting stock");
"12".into()
}
/// The shop: it asks the inventory before it takes an order. The client
/// sends the request in a span of its own, inside the order's span, and
/// puts the trace in its headers.
async fn order(
State(inventory): State<ClientWithMiddleware>,
Path(item): Path<String>,
) -> Result<String, StatusCode> {
let stock = inventory
.get(format!("http://127.0.0.1:3002/stock/{item}"))
.send()
.await
.and_then(|response| Ok(response.error_for_status()?))
.map_err(|_| StatusCode::BAD_GATEWAY)?
.text()
.await
.map_err(|_| StatusCode::BAD_GATEWAY)?;
tracing::info!(item, stock, "taking order");
Ok(format!("ordered {item}\n"))
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// First, so the HTTP layers find oxtel's recorder. Keep the guard until
// main returns: dropping it sends what's left.
let _telemetry = oxtel::init()?;
let inventory = Router::new()
.route("/stock/{item}", get(stock))
.layer(HttpServerLayer::new());
let listener = TcpListener::bind("127.0.0.1:3002").await?;
tokio::spawn(async move { axum::serve(listener, inventory).await });
// After `oxtel::init()`, so the client's durations are in seconds.
let client = oxtel_reqwest::client(reqwest::Client::new());
let shop = Router::new()
.route("/orders/{item}", get(order))
.with_state(client)
.layer(HttpServerLayer::new());
let listener = TcpListener::bind("127.0.0.1:3001").await?;
tokio::spawn(async move { axum::serve(listener, shop).await });
// One order, as a customer would send it, without a trace of its own.
reqwest::get("http://127.0.0.1:3001/orders/tea")
.await?
.error_for_status()?;
Ok(())
}
Cargo.toml
[dependencies]
oxtel = "0.4"
oxtel-tower = { version = "0.4", features = ["axum"] }
oxtel-reqwest = "0.4"
axum = "0.8"
reqwest = "0.13"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
tracing = "0.1"

From a checkout of the repository:

Terminal window
cargo run -p oxtel-examples --example two-services
OTEL_TRACES_EXPORTER=console cargo run -p oxtel-examples --example two-services # and the spans

The log lines of both services:

Standard error
{"timestamp":"2026-11-27T16:32:41.254389Z","level":"INFO","target":"oxtel","message":"log lines: standard error, as JSON; traces, metrics and logs: not exported (set OTEL_EXPORTER_OTLP_ENDPOINT to send them to a collector)"}
{"timestamp":"2026-11-27T16:32:41.258455Z","level":"INFO","target":"two_services","message":"counting stock","trace_id":"80fea65712454a65420ef24e040619b9","span_id":"26dc4dc996b65a3e","http.request.method":"GET","url.path":"/stock/tea","url.scheme":"http","http.route":"/stock/{item}","server.address":"127.0.0.1","server.port":3002,"network.protocol.version":"1.1","item":"tea"}
{"timestamp":"2026-11-27T16:32:41.258547Z","level":"INFO","target":"oxtel_tower::server","message":"request","trace_id":"80fea65712454a65420ef24e040619b9","span_id":"26dc4dc996b65a3e","http.request.method":"GET","url.path":"/stock/tea","url.scheme":"http","http.route":"/stock/{item}","server.address":"127.0.0.1","server.port":3002,"network.protocol.version":"1.1","http.response.status_code":200,"http.server.request.duration":9.925e-5}
{"timestamp":"2026-11-27T16:32:41.258836Z","level":"INFO","target":"two_services","message":"taking order","trace_id":"80fea65712454a65420ef24e040619b9","span_id":"7a6d1641e0d2a4d0","http.request.method":"GET","url.path":"/orders/tea","url.scheme":"http","http.route":"/orders/{item}","server.address":"127.0.0.1","server.port":3001,"network.protocol.version":"1.1","item":"tea","stock":"12"}
{"timestamp":"2026-11-27T16:32:41.258875Z","level":"INFO","target":"oxtel_tower::server","message":"request","trace_id":"80fea65712454a65420ef24e040619b9","span_id":"7a6d1641e0d2a4d0","http.request.method":"GET","url.path":"/orders/tea","url.scheme":"http","http.route":"/orders/{item}","server.address":"127.0.0.1","server.port":3001,"network.protocol.version":"1.1","http.response.status_code":200,"http.server.request.duration":0.001024125}
  • Every line has the same trace id, from the inventory’s as much as the shop’s.
  • Each service writes a request line when it finishes a request, with the status and how long it took in seconds.
  • The inventory’s lines come first: the shop takes the order only after the inventory answers.

With OTEL_TRACES_EXPORTER=console, as in the second command above, the spans are printed on standard output as they end, so the innermost comes first:

Standard output
2026-11-27T16:32:41.656238Z span "GET /stock/{item}" kind=server status=unset duration=0.055ms trace_id=3e33359f31c6f56f1d96610dd775c8d0 span_id=ef6a6d4560d62dca parent_id=5e7a2ddc518271fa http.request.method=GET url.path=/stock/tea url.scheme=http http.route=/stock/{item} server.address=127.0.0.1 server.port=3002 network.protocol.version=1.1 http.response.status_code=200 event="counting stock" event="request"
2026-11-27T16:32:41.655913Z span "GET" kind=client status=unset duration=0.514ms trace_id=3e33359f31c6f56f1d96610dd775c8d0 span_id=5e7a2ddc518271fa parent_id=c390f6525051b55c http.request.method=GET server.address=127.0.0.1 server.port=3002 url.full=http://127.0.0.1:3002/stock/tea http.response.status_code=200 network.protocol.version=1.1
2026-11-27T16:32:41.655846Z span "GET /orders/{item}" kind=server status=unset duration=0.614ms trace_id=3e33359f31c6f56f1d96610dd775c8d0 span_id=c390f6525051b55c http.request.method=GET url.path=/orders/tea url.scheme=http http.route=/orders/{item} server.address=127.0.0.1 server.port=3001 network.protocol.version=1.1 http.response.status_code=200 event="taking order" event="request"
  • The client span’s parent_id is the shop’s span id, and the inventory’s parent_id is the client span’s. That’s the chain a backend draws as a tree.
  • The client span is named for the method alone, GET, since a client doesn’t know the server’s routes. The URL is in url.full, without its query string, which may hold secrets.

CI runs the example both ways and checks it still prints these lines.

The names follow the OpenTelemetry conventions for HTTP.

Server span nameThe method and route, such as GET /stock/{item}, from oxtel-tower’s HttpServerLayer
Client span nameThe method, such as GET, from oxtel-reqwest
http.request.method, http.response.status_codeOn both
http.route, url.path, url.schemeOn the server span
url.full, server.address, server.portOn the client span; the server span has the address and port it was reached at
error.typeA 5xx on the server span; a 4xx or 5xx on the client span, or why there was no response, such as timeout or connect
http.server.request.durationA histogram of each request’s duration in seconds, by method, route and status
http.server.active_requestsThe requests in progress
http.client.request.durationA histogram of each call’s duration in seconds, by method, server and status
  • Name each service. Set OTEL_SERVICE_NAME for each, such as shop and inventory: that’s how a backend tells their spans apart in the trace.
  • Send traces only to your own services. A traceparent header tells the receiver your trace id. For calls that leave your systems, add oxtel_reqwest::HttpClientMiddleware::new().with_propagation(false) to the client instead of using oxtel_reqwest::client.
  • Decide where traces start. A service that requests reach from outside continues any trace a caller sends. HttpServerLayer::new().with_propagation(false) starts a new trace for each request instead.
  • Keep the headers through proxies. A proxy or gateway between the services must pass traceparent and tracestate on, or the trace splits in two.
  • Make one client, after oxtel::init(), and share it, as the shop shares it through axum’s state: a reqwest client keeps its connections open for the next request.
  • Other clients: a hyper or tower client gets the same span and metric from oxtel-tower’s HttpClientLayer.
  • Stop gracefully, as in a web service, so the last requests’ telemetry is sent.