|
1 | | -# OpenTelemetry Log Appender for tracing - Example |
| 1 | +# Getting started with OpenTelemetry Rust Logging |
2 | 2 |
|
3 | | -This example shows how to use the opentelemetry-appender-tracing crate, which is a |
4 | | -[logging |
5 | | -appender](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/glossary.md#log-appender--bridge) |
6 | | -that bridges logs from the [tracing crate](https://tracing.rs/tracing/#events) to |
7 | | -OpenTelemetry. The example setups a LoggerProvider with stdout exporter, so logs |
8 | | -are emitted to stdout. |
| 3 | +This example demonstrates the basics of logging with OpenTelemetry in Rust. If |
| 4 | +you're new to OpenTelemetry logging, this is a great place to start! |
| 5 | + |
| 6 | +## Understanding OpenTelemetry Logging |
| 7 | + |
| 8 | +**Important**: OpenTelemetry does not provide its own end-user facing logging |
| 9 | +API. Instead, it integrates with existing, popular logging libraries in the Rust |
| 10 | +ecosystem. This example uses the [tracing |
| 11 | +crate](https://docs.rs/tracing/latest/tracing/), one of the most widely-used |
| 12 | +logging frameworks in Rust. |
| 13 | + |
| 14 | +The way this works is through a [log appender (or |
| 15 | +bridge)](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/glossary.md#log-appender--bridge) - in this case, the `opentelemetry-appender-tracing` crate. The appender |
| 16 | +captures logs emitted through the `tracing` crate and forwards them to |
| 17 | +OpenTelemetry's logging pipeline. |
| 18 | + |
| 19 | +## What This Example Does |
| 20 | + |
| 21 | +This example: |
| 22 | + |
| 23 | +1. Sets up an OpenTelemetry `LoggerProvider` with resource attributes (like |
| 24 | + service name) |
| 25 | +2. Configures a **stdout exporter** to output logs to the console (for |
| 26 | + simplicity) |
| 27 | +3. Bridges the `tracing` crate to OpenTelemetry using |
| 28 | + `opentelemetry-appender-tracing` |
| 29 | +4. Emits a sample log event using the `tracing` library's `error!` macro |
| 30 | +5. Properly shuts down the logging pipeline |
| 31 | + |
| 32 | +**Note on Exporters**: This example uses the stdout exporter for demonstration |
| 33 | +purposes. In production scenarios, you would typically use other exporters such |
| 34 | +as: |
| 35 | + |
| 36 | +- **OTLP exporter** (`opentelemetry-otlp`) to send logs to an OpenTelemetry |
| 37 | + Collector or compatible backend |
| 38 | +- Other vendor-specific exporters for your observability platform |
9 | 39 |
|
10 | 40 | ## Usage |
11 | 41 |
|
12 | | -Run the following, and Logs emitted using [tracing](https://docs.rs/tracing/latest/tracing/) |
13 | | -will be written out to stdout. |
| 42 | +Run the example to see logs emitted through the `tracing` crate being captured |
| 43 | +and output via OpenTelemetry: |
14 | 44 |
|
15 | 45 | ```shell |
16 | 46 | cargo run |
17 | 47 | ``` |
| 48 | + |
| 49 | +You'll see the log output in your console, demonstrating how OpenTelemetry |
| 50 | +captures and processes logs from the `tracing` library. |
0 commit comments