Expand description
OpenTelemetry-compatible distributed tracing.
OtelLayer is a Middleware that:
- Reads the W3C
traceparentheader from incoming requests and continues an existing trace, or starts a fresh one. - Creates an HTTP server span with standard semantic attributes.
- Stores the active span context in thread-local storage so downstream
middleware (e.g.
crate::proxy::ReverseProxy) can propagate it. - Records the completed span to a configurable exporter.
§Quick start
use rust_web_server::app::App;
use rust_web_server::core::New;
use rust_web_server::otel::{OtelLayer, TracingConfig, ExporterConfig};
// Dev: print spans to stdout.
rust_web_server::otel::setup(TracingConfig {
service_name: "my-service".to_string(),
service_version: env!("CARGO_PKG_VERSION").to_string(),
exporter: ExporterConfig::Stdout,
sample_rate: 1.0,
batch_size: 128,
});
let app = App::new().wrap(OtelLayer);§Production: OTLP HTTP export
use rust_web_server::otel::{ExporterConfig, TracingConfig};
rust_web_server::otel::setup(TracingConfig {
service_name: "my-service".to_string(),
service_version: "1.0.0".to_string(),
exporter: ExporterConfig::Otlp {
endpoint: "http://localhost:4318".to_string(),
},
sample_rate: 0.1,
batch_size: 512,
});Alternatively, set environment variables before calling setup_from_env:
OTEL_SERVICE_NAME=my-service
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
OTEL_TRACES_SAMPLER_ARG=0.1 # sample rate 0.0–1.0 (default 1.0)Structs§
- Capturing
Exporter - Captures spans in memory instead of exporting them. Use in unit tests.
- Otel
Layer - Middleware that creates an HTTP server span for each request.
- Otlp
Http Exporter - Send spans to an OTLP-compatible collector over HTTP (JSON encoding).
- Span
Data - A completed span ready for export.
- Stdout
Exporter - Print one JSON line per span to stdout. Useful for development and for
piping into
jqor a log aggregator. - Trace
Context - Parsed W3C
traceparentheader value. - Tracing
Config - Configuration for the tracing subsystem.
Enums§
- Exporter
Config - Which backend to export spans to.
Traits§
- Exporter
- Destination for completed spans.
Functions§
- current_
traceparent - Return the W3C
traceparentvalue for the span currently being processed on this thread. ReturnsNonewhen noOtelLayerspan is active. - flush
- Flush buffered spans without shutting down. Useful in tests.
- new_
span_ id - Generate a new 64-bit span ID.
- new_
trace_ id - Generate a new 128-bit trace ID. Not cryptographically random but unique enough for tracing purposes across service restarts.
- setup
- Initialize tracing with an explicit config. Call once at startup before the server starts accepting requests.
- setup_
from_ env - Initialize tracing from standard OpenTelemetry environment variables:
- shutdown
- Flush all buffered spans to the exporter. Call before the process exits to ensure no spans are lost.