This example demonstrates the basics of logging with OpenTelemetry in Rust. If you're new to OpenTelemetry logging, this is a great place to start!
Important: OpenTelemetry does not provide its own end-user facing logging API. Instead, it integrates with existing, popular logging libraries in the Rust ecosystem. This example uses the tracing crate, one of the most widely-used logging frameworks in Rust.
The way this works is through a log appender (or
bridge) - in this case, the opentelemetry-appender-tracing crate. The appender
captures logs emitted through the tracing crate and forwards them to
OpenTelemetry's logging pipeline.
This example:
- Sets up an OpenTelemetry
LoggerProviderwith resource attributes (like service name) - Configures a stdout exporter to output logs to the console (for simplicity)
- Bridges the
tracingcrate to OpenTelemetry usingopentelemetry-appender-tracing - Emits a sample log event using the
tracinglibrary'serror!macro - Properly shuts down the logging pipeline
Note on Exporters: This example uses the stdout exporter for demonstration purposes. In production scenarios, you would typically use other exporters such as:
- OTLP exporter (
opentelemetry-otlp) to send logs to an OpenTelemetry Collector or compatible backend - Other vendor-specific exporters for your observability platform
Run the example to see logs emitted through the tracing crate being captured
and output via OpenTelemetry:
cargo runYou'll see the log output in your console, demonstrating how OpenTelemetry
captures and processes logs from the tracing library.