Overview
Theopentelemetry-appender-tracing crate bridges the tracing crate to OpenTelemetry logs. It provides a tracing::Layer implementation that converts tracing events into OpenTelemetry LogRecords, enabling seamless integration for async Rust applications.
Unlike traces and metrics, OpenTelemetry does not provide a dedicated logging API for end-users. Instead, it recommends using existing logging libraries like
tracing and bridging them to OpenTelemetry logs.Key Features
- Integrates as a
tracing-subscriberLayeralongside other layers (e.g.,fmt) - Automatically attaches OpenTelemetry trace context (
TraceId,SpanId,TraceFlags) to logs - Automatically associates OpenTelemetry
Resourceto logs - Supports exporting to OpenTelemetry-compatible backends (OTLP, stdout, etc.)
- Optional: Capture span attributes in log records (experimental)
Installation
Add the following to yourCargo.toml:
Quick Start
1
Create a LoggerProvider
Set up the OpenTelemetry logger provider:
2
Create the tracing bridge layer
Create the
OpenTelemetryTracingBridge layer:3
Register with tracing subscriber
Combine with other layers and initialize:
4
Emit logs
Use standard
tracing macros:5
Shutdown
Flush remaining logs:
Complete Example
Field Mapping
The appender mapstracing::Event to OpenTelemetry LogRecord:
Basic Mapping
If a field named
message exists, it’s used as the log body. Otherwise, the body is empty or derived from the event’s formatted message.Severity Mapping
Type Mapping
tracing field types are mapped toAnyValue:
Event Names and Targets
Event Names
You can specify event names explicitly:Targets
Targets group logs by module:target field as the OpenTelemetry instrumentation scope name.
Trace Context Integration
When logs are emitted within an active OpenTelemetry span, the trace context is automatically attached:- trace_id: Links the log to the distributed trace
- span_id: Links the log to the specific span
- trace_flags: Sampling information
Span Attributes (Experimental)
With theexperimental_span_attributes feature, span fields are automatically captured as log attributes:
user_id = 12345(from span)session_id = "abc-def"(from span)status = 500(from event)
Nested Spans
Attributes from all parent spans are collected:Attribute Allowlist
Filter which span attributes to capture:user_id and request_id span attributes will be captured in logs.
Filtering
By Level
Usetracing-subscriber filters:
Suppress Telemetry Loops
When using OTLP exporters, prevent telemetry-induced-telemetry loops:Advanced Configuration
Builder Pattern
Use the builder for advanced configuration:Multiple Layers
Combine with othertracing layers:
Batch Processing
For production, use batch processing:Custom Log Processor
You can create custom processors to enrich logs:Comparison with log Appender
Choose the
tracing appender if:
- You’re building async applications (tokio, async-std)
- You want hierarchical span context
- You need advanced filtering capabilities
- You want automatic trace correlation
log appender if:
- You have existing code using the
logcrate - You need simple, synchronous logging
- You don’t need span correlation
Feature Flags
Limitations
-
No Valuable support: The appender does not currently support the
valuablecrate for efficient serialization. See issue #2819. -
tracing-opentelemetry: This appender converts
tracingevents into logs, not spans. For convertingtracingspans to OpenTelemetry spans, use the third-partytracing-opentelemetrycrate.
Troubleshooting
Logs not appearing
-
Check that the layer is registered:
-
Verify filtering isn’t too restrictive:
-
Always shutdown the provider:
Trace context not attached
Ensure you’re using OpenTelemetry’s tracing, not justtracing:
Span attributes not captured
Enable the feature flag:See Also
log Appender
Alternative appender for the
log crateLog Processors
Configure batch and simple processors
Bridge API
Understanding the underlying API
Overview
Return to logs overview