Logging and Tracing
The tracing spans every transport opens, W3C trace context across transports, and exporting both with OpenTelemetry.
Ulo emits tracing events and spans and installs no subscriber. The subscriber is the application's: tracing_subscriber::fmt for logs on stderr, an OpenTelemetry layer for traces, or both on one registry. The example is examples/38-tracing.
One span per call
Each transport wraps dispatch in a span named and attributed by the OpenTelemetry semantic conventions:
| Transport | Span name | Attributes |
|---|---|---|
| HTTP | GET /users/{id} | http.request.method, http.route, url.path, url.scheme, http.response.status_code |
| gRPC | package.Service/Method | rpc.system = "grpc", rpc.service, rpc.method, rpc.grpc.status_code |
| RPC | the pattern | rpc.system, rpc.method, and messaging.system on a broker |
| WebSocket | the event, such as chat.send; ws.connect for the connect phase | ws.event, url.path on connect |
Every span also carries ulo.transport (the transport's key) and ulo.handler. Under a mount prefix, the HTTP span name and http.route carry the full route.
Trace context across transports
W3C Trace Context travels with the call. Each transport reads the incoming context before it opens the execution:
| Transport | Read from | Written to, on an outgoing call |
|---|---|---|
| HTTP | the request's traceparent and tracestate headers | not applicable; the framework has no HTTP client |
| gRPC | the request metadata | the request metadata, by ulo_grpc::outgoing |
| RPC | the frame's headers | the frame's headers, by RpcClient |
| WebSocket | the handshake headers; each message takes the connection's context as its parent | not applicable |
On a broker the headers travel as the broker's own, so a consumer that is not Ulo reads them.
With no tracing exporter installed, an incoming context is passed through exactly as received, so a trace that crosses an Ulo service stays whole. A participant that exports spans starts the span as a child of the incoming context, or as the root of a new trace when there is none. An invalid incoming traceparent counts as absent and the request still succeeds.
An outgoing call names the execution it continues. This is how the context follows a call from one service to the next:
// Hop 1, HTTP: `.within(&exec)` writes the execution's context into the RPC call.
#[ulo_http::get("/quote/{sku}")]
async fn quote(&self, Path(sku): Path<String>, exec: ExecutionRef) -> Result<Json<Quoted>, BoxError> {
let reserved: Reserved = self.stock.request("stock.reserve", &sku).within(&exec).await?;
/* .. */
}
// Hop 2, RPC: `outgoing` writes the execution's context into the gRPC call's metadata.
#[ulo_rpc::message("stock.reserve")]
async fn reserve(&self, sku: Payload<String>, exec: ExecutionRef) -> Result<Reserved, BoxError> {
let request = ulo_grpc::outgoing(&exec, Request::new(pb::QuoteRequest { sku: sku.0 }))?;
let price = (*self.pricing).clone().quote(request).await?.into_inner();
/* .. */
}A handler reads the context of its execution with exec.trace(), which gives a TraceContext with traceparent().
fn traceparent(exec: &ExecutionRef) -> String {
exec.trace().map(|trace| trace.traceparent()).unwrap_or_default()
}traceparent is reserved on a call made with .within: the client refuses a caller-set traceparent header on it, and a caller-set tracestate unless the context was originated by an exporter. A call without .within carries no context.
AppBuilder::trace(TraceMode::Off) turns it all off: nothing is read or written, and traceparent is an ordinary header.
What the example shows
In examples/38-tracing, an HTTP edge calls an RPC service, which calls a gRPC service, as three apps in one process. A request carrying traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01 is answered with the traceparent each hop saw. tests/chain.rs asserts:
- the trace id is the one the caller sent, on all three hops;
- each hop has a span of its own, and each span's parent is the span that called it;
- the edge span's parent is the caller's span, marked remote;
- with no incoming
traceparent, the edge starts a trace: a root span, and the same new trace id on every hop below it.
Exporting with OpenTelemetry
ulo-otel exports the spans the transports already open and adds none. ulo-otel-otlp sends them to a collector over HTTP. Install the exporter in main:
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::layer::SubscriberExt;
use ulo_config::Env;
use ulo_otel::{Otel, OtelOptions};
use ulo_otel_otlp::OtlpExporter;
let otel = Otel::new(OtelOptions::from_env(&Env::process())?, OtlpExporter::http())?;
tracing::subscriber::set_global_default(tracing_subscriber::registry().with(LevelFilter::INFO).with(otel.layer()))?;Import its module in the root module, m.import(otel.module()). The module binds the TraceSource that opens each span as a child of the incoming context, and it flushes the exporter at shutdown.
OtelOptions::from_envreadsOTEL_SERVICE_NAME,OTEL_EXPORTER_OTLP_ENDPOINT,OTEL_EXPORTER_OTLP_HEADERS,OTEL_TRACES_SAMPLERandOTEL_TRACES_SAMPLER_ARG.OtelOptions::from_config(&config, path)reads the same fields from a configuration section.- Sampling is parent-based: a remote parent whose sampled flag is clear exports nothing.
OTEL_TRACES_SAMPLERacceptsparentbased_always_on,parentbased_always_offandparentbased_traceidratio. - Filter the subscriber. The layer exports every span it is given, and h2 and hyper open trace-level spans. One request through the example chain exported 170 spans without the
INFOfilter. - The flush runs in the application-shutdown hook, after the sockets close, so the last spans are included. It needs a
Timer: an app without one that imports the module is refused at startup.
The exporter takes an Exporter, and the example's tests replace the OTLP one with an in-memory exporter (tests/support/memory.rs, about 20 lines).
Logs
Write logs with tracing macros and install a subscriber in main. Panic messages, unclassified error text and secrets are redacted before they reach an error handler or a response, so a log line written from an error handler does not carry them. The default panic hook prints the raw panic message to stderr before that redaction. Call ulo::panic::install_redacting_hook() at the start of main to print it redacted.