tokio_blocked/lib.rs
1//! This crate provides [`TokioBlockedLayer`], a tracing-rs layer that
2//! tracks when tokio tasks are blocked by synchronous code.
3//!
4//! See [`TokioBlockedLayer`] for more details.
5//!
6//! ## Usage
7//!
8//! Add this to your `Cargo.toml`:
9//!
10//! ```toml
11//! [dependencies]
12//! tracing = "0.1"
13//! tracing-subscriber = { version = "0.3", features = ["fmt", "env-filter"] }
14//! tokio = { version = "1.47", features = ["rt-multi-thread", "tracing", "macros", "time"] }
15//!
16//! tokio-blocked = "*"
17//! ````
18//!
19//! Then, in your application code:
20//!
21//! ```rust
22//! use std::time::Duration;
23//! use tokio_blocked::TokioBlockedLayer;
24//! use tracing_subscriber::{
25//! EnvFilter, Layer as _, layer::SubscriberExt as _, util::SubscriberInitExt as _,
26//! };
27//!
28//! #[tokio::main]
29//! async fn main() {
30//! // Prepare the tracing-subscriber logger with both a regular format logger
31//! // and the TokioBlockedLayer.
32//!
33//! {
34//! let fmt = tracing_subscriber::fmt::layer().with_filter(EnvFilter::from_default_env());
35//!
36//! let blocked = TokioBlockedLayer::new()
37//! .with_warn_busy_single_poll(Some(Duration::from_micros(150)));
38//!
39//! tracing_subscriber::registry()
40//! .with(fmt)
41//! .with(blocked)
42//! .init();
43//! }
44//!
45//! tokio::task::spawn(async {
46//! // BAD!
47//! // This produces a warning log message.
48//! std::thread::sleep(Duration::from_secs(2));
49//! })
50//! .await
51//! .unwrap();
52//! }
53//! ```
54
55mod layer;
56
57pub use self::layer::TokioBlockedLayer;