@@ -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
108155macro_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
127176mod task;
0 commit comments