5454//! }
5555//! }
5656//! ```
57- //!
58- //! ### Monitoring and publishing task metrics
59- //!
60- //! If the `metrics-rs-integration` feature is enabled, this crate allows
61- //! publishing task metrics externally via [metrics-rs](metrics) exporters.
62- //!
63- //! For example, you can use [metrics_exporter_prometheus] to make metrics visible
64- //! to [Prometheus]. You can see the [metrics_exporter_prometheus] and [metrics-rs](metrics)
65- //! docs for guidance on configuring exporters.
66- //!
67- //! The published metrics are the same as the fields and methods of [TaskMetrics], but with
68- //! a "tokio_" prefix added, for example `tokio_instrumented_count`.
69- //!
70- //! [metrics_exporter_prometheus]: https://docs.rs/metrics_exporter_prometheus
71- //! [TaskMetrics]: crate::TaskMetrics
72- //! [Prometheus]: https://prometheus.io
73- //!
74- //! This example exports [Prometheus] metrics by listening on a local Unix socket
75- //! called `prometheus.sock`, which you can access for debugging by
76- //! `curl --unix-socket prometheus.sock localhost`.
77- //!
78- //! ```
79- //! use std::time::Duration;
80- //!
81- //! use metrics::Key;
82- //!
83- //! #[tokio::main]
84- //! async fn main() {
85- //! metrics_exporter_prometheus::PrometheusBuilder::new()
86- //! .with_http_uds_listener("prometheus.sock")
87- //! .install()
88- //! .unwrap();
89- //! let monitor = tokio_metrics::TaskMonitor::new();
90- //! tokio::task::spawn(
91- //! tokio_metrics::TaskMetricsReporterBuilder::new(|name| {
92- //! let name = name.replacen("tokio_", "my_task_", 1);
93- //! Key::from_parts(name, &[("application", "my_app")])
94- //! })
95- //! // the default metric sampling interval is 30 seconds, which is
96- //! // too long for quick tests, so have it be 1 second.
97- //! .with_interval(std::time::Duration::from_secs(1))
98- //! .describe_and_run(monitor.clone()),
99- //! );
100- //! // Run some code
101- //! tokio::task::spawn(monitor.instrument(async move {
102- //! for _ in 0..1000 {
103- //! tokio::time::sleep(Duration::from_millis(10)).await;
104- //! }
105- //! }))
106- //! .await
107- //! .unwrap();
108- //! }
109- //! ```
11057
11158#![ cfg_attr(
11259 feature = "rt" ,
@@ -156,18 +103,23 @@ async fn do_work() {
156103 }
157104}
158105```
106+ "##
107+ ) ]
159108
160- ### Monitoring and publishing runtime metrics
109+ # exporters.
115+ publishing metrics externally via [metrics-rs](metrics) exporters.
164116
165117For example, you can use [metrics_exporter_prometheus] to make metrics visible
166118to [Prometheus]. You can see the [metrics_exporter_prometheus] and [metrics-rs](metrics)
167119docs for guidance on configuring exporters.
168120
169- The published metrics are the same as the fields and methods of [RuntimeMetrics], but with
170- a "tokio_" prefix added, for example `tokio_workers_count`.
121+ The published metrics are the same as the fields and methods of [RuntimeMetrics] and [TaskMetrics],
122+ but with a "tokio_" prefix added, for example `tokio_workers_count` and `tokio_instrumented_count `.
171123
172124[metrics_exporter_prometheus]: https://docs.rs/metrics_exporter_prometheus
173125[RuntimeMetrics]: crate::RuntimeMetrics
@@ -180,6 +132,8 @@ called `prometheus.sock`, which you can access for debugging by
180132```
181133use std::time::Duration;
182134
135+ use metrics::Key;
136+
183137#[tokio::main]
184138async fn main() {
185139 metrics_exporter_prometheus::PrometheusBuilder::new()
@@ -193,12 +147,81 @@ async fn main() {
193147 .with_interval(std::time::Duration::from_secs(1))
194148 .describe_and_run(),
195149 );
150+ let monitor = tokio_metrics::TaskMonitor::new();
151+ tokio::task::spawn(
152+ tokio_metrics::TaskMetricsReporterBuilder::new(|name| {
153+ let name = name.replacen("tokio_", "my_task_", 1);
154+ Key::from_parts(name, &[("application", "my_app")])
155+ })
156+ // the default metric sampling interval is 30 seconds, which is
157+ // too long for quick tests, so have it be 1 second.
158+ .with_interval(std::time::Duration::from_secs(1))
159+ .describe_and_run(monitor.clone()),
160+ );
161+ // Run some code
162+ tokio::task::spawn(monitor.instrument(async move {
163+ for _ in 0..1000 {
164+ tokio::time::sleep(Duration::from_millis(10)).await;
165+ }
166+ }))
167+ .await
168+ .unwrap();
169+ }
170+ ```
171+ "##
172+ ) ]
173+
174+ # exporters.
181+
182+ For example, you can use [metrics_exporter_prometheus] to make metrics visible
183+ to [Prometheus]. You can see the [metrics_exporter_prometheus] and [metrics-rs](metrics)
184+ docs for guidance on configuring exporters.
185+
186+ The published metrics are the same as the fields and methods of [TaskMetrics], but with
187+ a "tokio_" prefix added, for example `tokio_instrumented_count`.
188+
189+ [metrics_exporter_prometheus]: https://docs.rs/metrics_exporter_prometheus
190+ [TaskMetrics]: crate::TaskMetrics
191+ [Prometheus]: https://prometheus.io
192+
193+ This example exports [Prometheus] metrics by listening on a local Unix socket
194+ called `prometheus.sock`, which you can access for debugging by
195+ `curl --unix-socket prometheus.sock localhost`.
196+
197+ ```
198+ use std::time::Duration;
199+
200+ use metrics::Key;
201+
202+ #[tokio::main]
203+ async fn main() {
204+ metrics_exporter_prometheus::PrometheusBuilder::new()
205+ .with_http_uds_listener("prometheus.sock")
206+ .install()
207+ .unwrap();
208+ let monitor = tokio_metrics::TaskMonitor::new();
209+ tokio::task::spawn(
210+ tokio_metrics::TaskMetricsReporterBuilder::new(|name| {
211+ let name = name.replacen("tokio_", "my_task_", 1);
212+ Key::from_parts(name, &[("application", "my_app")])
213+ })
214+ // the default metric sampling interval is 30 seconds, which is
215+ // too long for quick tests, so have it be 1 second.
216+ .with_interval(std::time::Duration::from_secs(1))
217+ .describe_and_run(monitor.clone()),
218+ );
196219 // Run some code
197- tokio::task::spawn(async move {
220+ tokio::task::spawn(monitor.instrument( async move {
198221 for _ in 0..1000 {
199222 tokio::time::sleep(Duration::from_millis(10)).await;
200223 }
201- })
224+ }))
202225 .await
203226 .unwrap();
204227}
0 commit comments