Skip to content

Commit 1b15645

Browse files
arielb1Ariel Ben-Yehuda
andauthored
docs: add metrics-rs-integration readme (#77)
Co-authored-by: Ariel Ben-Yehuda <[email protected]>
1 parent ff02d20 commit 1b15645

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ More information about where cargo looks for configuration files can be found
153153
Missing this configuration file during compilation will cause tokio-metrics to not work, and alternating
154154
between building with and without this configuration file included will cause full rebuilds of your project.
155155

156+
### Collecting Runtime Metrics directly
157+
156158
The `rt` feature of `tokio-metrics` is on by default; simply check that you do
157159
not set `default-features = false` when declaring it as a dependency; e.g.:
158160
```toml
@@ -181,6 +183,50 @@ tokio::spawn(do_work());
181183
tokio::spawn(do_work());
182184
```
183185

186+
### Collecting Runtime Metrics via metrics.rs
187+
188+
If you also enable the `metrics-rs-integration` feature, you can use [metrics.rs] exporters to export metrics outside of your process. `metrics.rs` supports a variety of exporters, including [Prometheus].
189+
190+
The exported metrics by default will be exported with their name, preceded by `tokio_`. For example, `tokio_workers_count` for the [`workers_count`] metric. This can be customized by using the `with_metrics_tranformer` function.
191+
192+
If you want to use [Prometheus], you could have this `Cargo.toml`:
193+
194+
```toml
195+
[dependencies]
196+
tokio-metrics = { version = "0.4.1", features = ["metrics-rs-integration"] }
197+
metrics = "0.24"
198+
# You don't actually need to use the Prometheus exporter with uds-listener enabled,
199+
# it's just here as an example.
200+
metrics-exporter-prometheus = { version = "0.16", features = ["uds-listener"] }
201+
```
202+
203+
Then, you can launch a metrics exporter:
204+
```rust
205+
// This makes metrics visible via a local Unix socket with name prometheus.sock
206+
// You probably want to do it differently.
207+
//
208+
// If you use this exporter, you can access the metrics for debugging
209+
// by running `curl --unix-socket prometheus.sock localhost`.
210+
metrics_exporter_prometheus::PrometheusBuilder::new()
211+
.with_http_uds_listener("prometheus.sock")
212+
.install()
213+
.unwrap();
214+
215+
// This line launches the reporter that monitors the Tokio runtime and exports the metrics.
216+
tokio::task::spawn(
217+
tokio_metrics::RuntimeMetricsReporterBuilder::default().describe_and_run(),
218+
);
219+
220+
// run some tasks
221+
tokio::spawn(do_work());
222+
tokio::spawn(do_work());
223+
tokio::spawn(do_work());
224+
```
225+
226+
Of course, it will work with any other [metrics.rs] exporter.
227+
228+
[metrics.rs]: https://docs.rs/metrics
229+
184230
### Runtime Metrics
185231
#### Base Metrics
186232
- **[`workers_count`]**

0 commit comments

Comments
 (0)