pub struct Config { /* private fields */ }Expand description
Configuration for Tokio runtime instrumentation.
§Multiple Runtimes with Custom Labels
use opentelemetry::KeyValue;
use opentelemetry_instrumentation_tokio::Config;
let rt1 = Runtime::new().unwrap();
let rt2 = Runtime::new().unwrap();
// Add custom labels to distinguish runtimes
Config::new()
.with_label("runtime.name", "api-server")
.observe_runtime(rt1.handle());
Config::new()
.with_label("runtime.name", "worker")
.observe_runtime(rt2.handle());Implementations§
Source§impl Config
impl Config
Sourcepub fn with_labels(self, labels: impl IntoIterator<Item = KeyValue>) -> Self
pub fn with_labels(self, labels: impl IntoIterator<Item = KeyValue>) -> Self
Add custom labels to this runtime’s metrics.
Labels help distinguish metrics from different runtimes when observing multiple runtimes in the same process.
When tokio_unstable is enabled, a tokio.runtime.id label is
automatically added in addition to any custom labels.
§Examples
use opentelemetry::KeyValue;
use opentelemetry_instrumentation_tokio::Config;
Config::new()
.with_labels([
KeyValue::new("runtime.name", "worker-pool"),
KeyValue::new("env", "production"),
])
.observe_current_runtime();Sourcepub fn with_label(self, key: impl Into<Key>, value: impl Into<Value>) -> Self
pub fn with_label(self, key: impl Into<Key>, value: impl Into<Value>) -> Self
Add a single custom label to this runtime’s metrics.
This method can be chained to add multiple labels.
§Examples
use opentelemetry_instrumentation_tokio::Config;
Config::new()
.with_label("runtime.name", "api-server")
.with_label("runtime.purpose", "http-requests")
.observe_current_runtime();Sourcepub fn observe_current_runtime(self)
pub fn observe_current_runtime(self)
Observe metrics for the current Tokio runtime.
This is a convenience method that calls Self::observe_runtime with
the current runtime handle.
§Panics
Panics if called outside of a Tokio runtime context.
§Examples
use opentelemetry_instrumentation_tokio::Config;
Config::new().observe_current_runtime();Sourcepub fn observe_runtime(self, handle: &Handle)
pub fn observe_runtime(self, handle: &Handle)
Observe metrics for a specific Tokio runtime.
Registers OpenTelemetry observable instruments that expose Tokio runtime metrics. The metrics are collected on-demand by the configured meter provider.
This function can be called multiple times to observe multiple runtimes.
Each runtime’s metrics will be distinguished by the labels configured
via Self::with_labels or Self::with_label.
When tokio_unstable is enabled, a tokio.runtime.id label is
automatically added.
§Examples
use opentelemetry_instrumentation_tokio::Config;
let handle = tokio::runtime::Handle::current();
Config::new().observe_runtime(&handle);§Panics
Panics if the global runtime registry is poisoned.