6 releases
| new 0.3.0 | Apr 2, 2026 |
|---|---|
| 0.2.2 | Mar 6, 2025 |
| 0.1.3 | Oct 24, 2023 |
| 0.1.2 | Jul 31, 2023 |
#2 in #grafana
1,347 downloads per month
61KB
1.5K
SLoC
metrics-exporter-influx
Metrics reporter for https://github.com/metrics-rs/metrics that writes to InfluxDB.
Usage
Configuration
Writing to a stderr
use std::time::Duration;
#[tokio::main]
async fn main() {
InfluxBuilder::new().with_duration(Duration::from_secs(60)).install()?;
}
Writing to a file
use std::fs::File;
#[tokio::main]
async fn main() {
InfluxBuilder::new()
.with_writer(File::create("/tmp/out.metrics")?)
.install()?;
}
Writing to http
Influx
#[tokio::main]
async fn main() {
InfluxBuilder::new()
.with_influx_api(
"http://localhost:8086",
"db/rp",
None,
None,
None,
Some("ns".to_string())
)
.install()?;
}
Grafana Cloud
Grafana Cloud supports the Influx Line Protocol exported by this exporter.
#[tokio::main]
async fn main() {
InfluxBuilder::new()
.with_grafna_cloud_api(
"https://https://influx-prod-03-prod-us-central-0.grafana.net/api/v1/push/influx/write",
Some("username".to_string()),
Some("key")
)
.install()?;
}
Builder API
The exporter runs as a tokio task. A tokio runtime should already be active
(e.g. via #[tokio::main]) when calling build(), build_and_spawn(), or
install(). If no runtime is found, one will be created automatically, but
this is not the preferred path.
InfluxBuilder provides three ways to create a recorder and exporter:
| Method | Returns | Description |
|---|---|---|
build() |
(InfluxRecorder, ExporterFuture) |
Caller spawns the exporter future, passes the task to shutdown_handle(), and calls set_global_recorder(). |
build_and_spawn() |
(InfluxRecorder, InfluxShutdownHandle) |
Spawns the exporter internally. Caller calls set_global_recorder(). |
install() |
InfluxShutdownHandle |
Spawns the exporter and sets the global recorder. The most common entry point. |
Graceful Shutdown
Calling close() on an InfluxShutdownHandle signals the background exporter loop
to perform a final flush, then joins the task. This ensures no metrics are lost when
the process exits (e.g. receiving SIGTERM).
let handle = InfluxBuilder::new()
.with_grafana_cloud_api(endpoint, username, password)?
.install()?;
// ... application runs ...
handle.close(); // final flush + join
For build() callers who spawn the exporter themselves, wire the task back in for
a clean shutdown:
let (recorder, exporter) = InfluxBuilder::new()
.with_grafana_cloud_api(endpoint, username, password)?
.build()?;
let exporter_task = tokio::spawn(exporter);
let shutdown = recorder.shutdown_handle(exporter_task);
metrics::set_global_recorder(recorder).unwrap();
// ... application runs ...
shutdown.close(); // signal → final flush → join
Dependencies
~11–27MB
~288K SLoC