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 via
setup_with_exporterto prove that a span was actually recorded, not just that its getters return the right values. - 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
- A single open span. Create one with
spanorclient_span(orSpan::newfor full control overSpanKind); it becomes the “current” span on this thread until it’s dropped (orSpan::endis called explicitly, which is equivalent). - 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§
- Attribute
Value - A span attribute value — matches OTLP
AnyValue’s basic variants. - Exporter
Config - Which backend to export spans to.
- Span
Kind - OTLP
SpanKind. Numbering matches the OTLP spec (INTERNAL=1,SERVER=2,CLIENT=3) so the numeric value can be cast directly withas i32when building OTLP JSON.PRODUCER/CONSUMERare intentionally not exposed — nothing in this crate does message-queue instrumentation yet.
Traits§
- Exporter
- Destination for completed spans.
Functions§
- client_
span - Start a new
SpanKind::Clientchild span for an outbound call to another service (an HTTP request, a gRPC call, …). - current_
traceparent - Return the W3C
traceparentvalue for the innermost span currently being processed on this thread (the deepest activeSpan, not necessarily the request root). ReturnsNonewhen no span 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:
- setup_
with_ exporter - Like
setup, but takes the exporter directly instead of building one fromTracingConfig::exporter. Lets you wire in anyExporter— includingCapturingExporter— through the same code path production code uses, rather than onlyExporterConfig’s three built-in choices. - shutdown
- Flush all buffered spans to the exporter. Call before the process exits to ensure no spans are lost.
- span
- Start a new
SpanKind::Internalchild span nested under the currently active span (or a fresh trace if none is active). Use for internal work like a database query or cache lookup.