Skip to content

Commit a893585

Browse files
arielb1Ariel Ben-Yehuda
andauthored
metrics.rs integration (#68)
Co-authored-by: Ariel Ben-Yehuda <[email protected]>
1 parent a566938 commit a893585

File tree

6 files changed

+665
-2
lines changed

6 files changed

+665
-2
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tokio_unstable)'] }
1919

2020
[features]
2121
default = ["rt"]
22+
metrics-rs-integration = ["dep:metrics"]
2223
rt = ["tokio"]
2324

2425
[dependencies]
2526
tokio-stream = "0.1.11"
2627
futures-util = "0.3.19"
2728
pin-project-lite = "0.2.7"
2829
tokio = { version = "1.41.0", features = ["rt", "time", "net"], optional = true }
30+
metrics = { version = "0.24", optional = true }
2931

3032
[dev-dependencies]
3133
axum = "0.6"
@@ -35,6 +37,9 @@ num_cpus = "1.13.1"
3537
serde = { version = "1.0.136", features = ["derive"] }
3638
serde_json = "1.0.79"
3739
tokio = { version = "1.41.0", features = ["full", "rt", "time", "macros", "test-util"] }
40+
metrics-util = { version = "0.19", features = ["debugging"] }
41+
metrics = { version = "0.24" }
42+
metrics-exporter-prometheus = { version = "0.16", features = ["uds-listener"] }
3843

3944
[[example]]
4045
name = "runtime"

src/lib.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,54 @@ async fn do_work() {
102102
tokio::time::sleep(Duration::from_millis(100)).await;
103103
}
104104
}
105-
```"##
105+
```
106+
107+
### Monitoring and publishing runtime metrics (unstable)
108+
109+
If the `metrics-rs-integration` feature is additionally enabled, this crate allows
110+
publishing runtime metrics externally via [metrics-rs](metrics) exporters.
111+
112+
For example, you can use [metrics_exporter_prometheus] to make metrics visible
113+
to Prometheus. You can see the [metrics_exporter_prometheus] and [metrics-rs](metrics)
114+
docs for guidance on configuring exporters.
115+
116+
The published metrics are the same as the fields of [RuntimeMetrics], but with
117+
a "tokio_" prefix added, for example `tokio_workers_count`.
118+
119+
[metrics_exporter_prometheus]: https://docs.rs/metrics_exporter_prometheus
120+
[RuntimeMetrics]: crate::RuntimeMetrics
121+
122+
This example exports Prometheus metrics by listening on a local Unix socket
123+
called `prometheus.sock`, which you can access for debugging by
124+
`curl --unix-socket prometheus.sock localhost`.
125+
126+
```
127+
use std::time::Duration;
128+
129+
#[tokio::main]
130+
async fn main() {
131+
metrics_exporter_prometheus::PrometheusBuilder::new()
132+
.with_http_uds_listener("prometheus.sock")
133+
.install()
134+
.unwrap();
135+
tokio::task::spawn(
136+
tokio_metrics::RuntimeMetricsReporterBuilder::default()
137+
// the default metric sampling interval is 30 seconds, which is
138+
// too long for quick tests, so have it be 1 second.
139+
.with_interval(std::time::Duration::from_secs(1))
140+
.describe_and_run(),
141+
);
142+
// Run some code
143+
tokio::task::spawn(async move {
144+
for _ in 0..1000 {
145+
tokio::time::sleep(Duration::from_millis(10)).await;
146+
}
147+
})
148+
.await
149+
.unwrap();
150+
}
151+
```
152+
"##
106153
)]
107154

108155
macro_rules! cfg_rt {
@@ -122,6 +169,8 @@ cfg_rt! {
122169
RuntimeMetrics,
123170
RuntimeMonitor,
124171
};
172+
#[cfg(feature = "metrics-rs-integration")]
173+
pub use runtime::metrics_rs_integration::{RuntimeMetricsReporterBuilder, RuntimeMetricsReporter};
125174
}
126175

127176
mod task;

src/runtime.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use std::time::{Duration, Instant};
22
use tokio::runtime;
33

4+
#[cfg(feature = "metrics-rs-integration")]
5+
pub(crate) mod metrics_rs_integration;
6+
47
#[cfg(any(docsrs, all(tokio_unstable, feature = "rt")))]
58
#[cfg_attr(docsrs, doc(cfg(all(tokio_unstable, feature = "rt"))))]
69
/// Monitors key metrics of the tokio runtime.

0 commit comments

Comments
 (0)