|
| 1 | +# SDK Self-Diagnostics via Metrics |
| 2 | + |
| 3 | +Status: |
| 4 | +[Development](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/document-status.md) |
| 5 | + |
| 6 | +The OpenTelemetry Rust SDK can emit metrics about its own internal state, |
| 7 | +following the [semantic conventions for SDK metrics](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/otel/sdk-metrics.md). |
| 8 | + |
| 9 | +## Implemented Metrics |
| 10 | + |
| 11 | +### `otel.sdk.processor.log.processed` |
| 12 | + |
| 13 | +- **Instrument**: `Counter<u64>` |
| 14 | +- **Unit**: `{log_record}` |
| 15 | +- **Description**: The number of log records for which processing has finished, |
| 16 | + either successful or failed. |
| 17 | +- **Component**: `BatchLogProcessor` |
| 18 | + |
| 19 | +**Attributes:** |
| 20 | + |
| 21 | +| Attribute | Value | |
| 22 | +|-----------|-------| |
| 23 | +| `otel.component.type` | `batching_log_processor` | |
| 24 | +| `otel.component.name` | `batching_log_processor/{id}` (auto-assigned) | |
| 25 | +| `error.type` | `queue_full` when dropped due to full queue; `already_shutdown` when emitted after shutdown. Absent on success. | |
| 26 | + |
| 27 | +The counter is incremented on every `emit()` call: once for successful |
| 28 | +enqueue, once with `error.type=queue_full` when dropped due to a full queue, |
| 29 | +and once with `error.type=already_shutdown` when emitted after the processor |
| 30 | +has been shut down. |
| 31 | + |
| 32 | +## Feature Gate |
| 33 | + |
| 34 | +Self-diagnostics metrics require the `experimental_metrics_bound_instruments` |
| 35 | +feature on `opentelemetry_sdk`. This feature is not enabled by default. |
| 36 | + |
| 37 | +Without bound instruments, every `Counter::add()` call would need to resolve |
| 38 | +attributes to the internal aggregation state — roughly 50 ns per call on the |
| 39 | +`emit()` hot path. With bound instruments, attributes are resolved once at |
| 40 | +construction and subsequent `add()` calls are a single atomic increment at |
| 41 | +~1.8 ns. Since `emit()` is called for every log record in the application, |
| 42 | +this overhead matters. Bound instruments are what make self-diagnostics |
| 43 | +practical without measurable performance impact. |
| 44 | + |
| 45 | +## Provider Initialization Order |
| 46 | + |
| 47 | +The `BatchLogProcessor` obtains a `Meter` from the global `MeterProvider` |
| 48 | +during construction. Rust's `global::meter()` returns a snapshot — it does |
| 49 | +**not** retroactively upgrade if the global provider changes later. |
| 50 | + |
| 51 | +For self-diagnostics to produce real data, the global `MeterProvider` must be |
| 52 | +set **before** creating the `LoggerProvider` (and its `BatchLogProcessor`). |
| 53 | +The recommended setup order is: |
| 54 | + |
| 55 | +```rust |
| 56 | +// 1. MeterProvider first. Optionally set up a throwaway thread-local fmt |
| 57 | +// subscriber so that any internal logs emitted during MeterProvider |
| 58 | +// construction still appear on stdout. This is not required — without it |
| 59 | +// those few startup-time debug messages are simply lost. |
| 60 | +let meter_provider = { |
| 61 | + let _guard = tracing::subscriber::set_default( |
| 62 | + tracing_subscriber::fmt().with_env_filter("info").finish(), |
| 63 | + ); |
| 64 | + let mp = init_metrics(); |
| 65 | + global::set_meter_provider(mp.clone()); |
| 66 | + mp |
| 67 | +}; // _guard drops here, removing the throwaway subscriber |
| 68 | + |
| 69 | +// 2. LoggerProvider second (BatchLogProcessor picks up the real meter) |
| 70 | +let logger_provider = init_logs(); |
| 71 | + |
| 72 | +// 3. Full tracing subscriber (fmt + OTel bridge) |
| 73 | +tracing_subscriber::registry() |
| 74 | + .with(OpenTelemetryTracingBridge::new(&logger_provider)) |
| 75 | + .with(fmt::layer()) |
| 76 | + .init(); |
| 77 | + |
| 78 | +// 4. TracerProvider last (init logs captured by OTel pipeline) |
| 79 | +let tracer_provider = init_traces(); |
| 80 | +global::set_tracer_provider(tracer_provider.clone()); |
| 81 | +``` |
| 82 | + |
| 83 | +See the OTLP examples (`basic-otlp`, `basic-otlp-http`) for the full pattern |
| 84 | +including a temporary thread-local `fmt` subscriber during MeterProvider setup. |
| 85 | + |
| 86 | +If the `MeterProvider` is set after the `LoggerProvider`, the counter will be |
| 87 | +backed by a no-op meter and silently produce nothing. This is harmless but |
| 88 | +means no self-diagnostics data. |
| 89 | + |
| 90 | +## TODO |
| 91 | + |
| 92 | +- Emit `otel.sdk.processor.log.queue.size` (current queue depth). |
| 93 | +- Emit `otel.sdk.processor.log.queue.capacity` (configured max queue size). |
| 94 | +- Emit `otel.sdk.exporter.log.exported` from log exporters. |
| 95 | +- Add self-diagnostics to `BatchSpanProcessor`. |
| 96 | +- Add self-diagnostics to `SimpleLogProcessor`. |
| 97 | +- Record logs lost when `shutdown_with_timeout` times out (the background |
| 98 | + thread may still hold unfinished exports and queued records). |
| 99 | +- Long-term: `global::meter()` currently returns a snapshot that does not |
| 100 | + reflect later calls to `set_meter_provider()`. Investigate whether the |
| 101 | + global meter can be made to pick up provider changes after the fact. |
0 commit comments