| Crates.io | opentelemetry-instrumentation-tokio |
| lib.rs | opentelemetry-instrumentation-tokio |
| version | 0.1.2 |
| created_at | 2025-12-12 16:48:39.263208+00 |
| updated_at | 2025-12-15 10:10:07.167196+00 |
| description | OpenTelemetry instrumentation for Tokio runtime metrics |
| homepage | |
| repository | https://github.com/sandhose/opentelemetry-instrumentation-tokio |
| max_upload_size | |
| id | 1981847 |
| size | 57,751 |
This crate provides observability for Tokio runtimes by exposing runtime metrics through OpenTelemetry.
[dependencies]
opentelemetry-instrumentation-tokio = "0.1"
opentelemetry = "0.31"
opentelemetry_sdk = "0.31"
tokio = { version = "1", features = ["rt-multi-thread"] }
use opentelemetry_sdk::metrics::SdkMeterProvider;
#[tokio::main]
async fn main() {
// Setup your meter provider
let provider = SdkMeterProvider::builder().build();
opentelemetry::global::set_meter_provider(provider);
// Instrument the current runtime
opentelemetry_instrumentation_tokio::observe_current_runtime();
// Your application code
}
let handle = tokio::runtime::Handle::current();
opentelemetry_instrumentation_tokio::observe_runtime(&handle);
Use custom labels to distinguish metrics from different runtimes. Labels are merged with the automatically added tokio.runtime.id (when available) so you can disambiguate runtimes without manual guards or deduplication.
use opentelemetry_instrumentation_tokio::Config;
Config::new()
.with_label("runtime.name", "api")
.observe_current_runtime();
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap();
runtime.block_on(async {
Config::new()
.with_label("runtime.name", "worker")
.observe_current_runtime();
});
use opentelemetry::KeyValue;
Config::new()
.with_labels([
KeyValue::new("service", "api"),
KeyValue::new("env", "production"),
])
.observe_current_runtime();
These metrics work without any special configuration:
tokio.workers - Number of worker threadstokio.global_queue_depth - Tasks in global queuetokio.worker.park_count - Worker park operations (per-worker)tokio.worker.busy_duration - Worker busy time in ms (per-worker)tokio_unstable (19 additional metrics)Most metrics require building with the tokio_unstable cfg flag:
RUSTFLAGS="--cfg tokio_unstable" cargo build
See the Tokio documentation for more details.
Available metrics with tokio_unstable:
Runtime-level metrics:
tokio.blocking_threads - Blocking thread pool sizetokio.active_tasks - Number of alive taskstokio.idle_blocking_threads - Idle blocking threadstokio.remote_schedules - Remote task schedulestokio.budget_forced_yields - Budget-forced yieldstokio.spawned_tasks_count - Total spawned taskstokio.blocking_queue_depth - Blocking queue depthI/O driver metrics:
tokio.io_driver.fd_registrations - FD registrationstokio.io_driver.fd_deregistrations - FD deregistrationstokio.io_driver.fd_readies - Ready events processedPer-worker metrics (all with tokio.worker.index attribute):
tokio.worker.noops - No-op wake-upstokio.worker.task_steals - Tasks stolentokio.worker.steal_operations - Steal operationstokio.worker.polls - Task pollstokio.worker.local_schedules - Local task schedulestokio.worker.overflows - Local queue overflowstokio.worker.local_queue_depth - Local queue depthtokio.worker.mean_poll_time - Mean poll duration (ns)tokio.worker.poll_time_bucket - Poll time histogram (requires config + runtime support)Licensed under the Apache License, Version 2.0.