Skip to main content

Crate otel_bootstrap

Crate otel_bootstrap 

Source
Expand description

One-call OpenTelemetry bootstrap — traces + metrics + logs with OTLP export.

Call init_telemetry at main() before starting the server. Keep the returned TelemetryHandles alive for the duration of the process — dropping them flushes and shuts down both providers.

Configuration is via environment variables per the OpenTelemetry spec:

  • OTEL_EXPORTER_OTLP_ENDPOINT (default: http://localhost:4317 for gRPC, http://localhost:4318 for HTTP)
  • OTEL_EXPORTER_OTLP_PROTOCOL (grpc or http/protobuf) — selects transport when both features are enabled
  • OTEL_EXPORTER_OTLP_TIMEOUT — export timeout in milliseconds (default: 10 000 ms)
  • OTEL_SERVICE_NAME (overridden by the service_name argument)
  • OTEL_TRACES_SAMPLER / OTEL_TRACES_SAMPLER_ARG (fallback when no explicit sampler is set)

§Env var handling: otel-bootstrap vs SDK

Env varHandled by
OTEL_SERVICE_NAMEotel-bootstrap (falls back to SDK default)
OTEL_TRACES_SAMPLER / OTEL_TRACES_SAMPLER_ARGotel-bootstrap
OTEL_EXPORTER_OTLP_PROTOCOLotel-bootstrap
OTEL_EXPORTER_OTLP_ENDPOINTotel-bootstrap
OTEL_EXPORTER_OTLP_TIMEOUTotel-bootstrap
OTEL_BSP_MAX_EXPORT_BATCH_SIZESDK (batch span processor)
OTEL_METRIC_EXPORT_INTERVALSDK (periodic reader)
Per-signal endpoints (OTEL_EXPORTER_OTLP_TRACES_ENDPOINT etc.)SDK

Re-exports§

pub use instrumented_port::Instrumented;
pub use instrumented_port::InstrumentedArc;
pub use log_bridge::PROPAGATED_SPAN_FIELDS;
pub use log_bridge::SpanLogAttrs;
pub use log_bridge::record_span_log_attr;
pub use log_bridge::record_span_log_attr_on;
pub use spanned::Spanned;
pub use spanned::in_span;

Modules§

axum_middleware
Axum tower middleware for W3C trace context propagation.
grpc_middleware
Tonic gRPC trace-context propagation, client and server side.
instrumented_port
Generic client-span wrapper for outbound hexagonal port calls.
log_bridge
profiling
span_enrichment
Span enrichment via the EnrichSpan trait.
spanned
Await-safe replacement for the let _guard = span.enter(); pattern.
testing
No-op / testing mode for otel-bootstrap.

Structs§

MtlsMaterial
mTLS material for the gRPC transport. Requires the grpc-mtls feature.
Telemetry
Entry point for configuring telemetry via a builder pattern.
TelemetryBuilder
Builder for configuring telemetry options incrementally.
TelemetryHandles
Handles returned by init_telemetry or TelemetryBuilder::init.

Enums§

ExportProtocol
OTLP export protocol.
LogFormat
Stdout log encoding installed by TelemetryBuilder.
TraceSampler
Trace sampler configuration.

Functions§

axum_layer
Returns a ready-to-use tower::Layer that extracts W3C trace context from incoming HTTP requests, creates a span with standard HTTP semantic-convention attributes, and injects trace context into response headers.
build_resource
Build a Resource enriched with semantic-convention attributes.
grpc_client_layer
Construct the tower Layer that injects the current trace context into outgoing gRPC request metadata.
grpc_server_layer
Construct the tower Layer that extracts trace context from incoming gRPC request metadata and opens a child span.
init_telemetry
Initialise OpenTelemetry traces + metrics with OTLP gRPC export.
init_telemetry_with_sampler
Initialise OpenTelemetry traces + metrics with OTLP gRPC export and an explicit trace sampler.
span_enricher_layer
Construct the tower Layer that calls span_enrichment::EnrichSpan::enrich_span on every request that carries a T extension.