Expand description
CSV exporter for the metrics crate.
This crate provides a CsvRecorder that implements metrics::Recorder,
plus a CsvBuilder for configuration and installation.
Output is long-form CSV with a fixed header: timestamp_rfc3339,name,kind,labels,value.
The name field is the sanitized metric name. The labels field is a |-delimited list of
key=value pairs in the order provided by the metrics key. Components are lowercased and
whitespace plus |/= characters are normalized to underscores.
It can be installed directly or composed with other recorders using metrics-util fanout
layers.
Locally recorded histograms are summarized into quantiles. Each
histogram emits one row per statistic, with the statistic carried in the labels field as a
stat=<name> pair and a scalar in the value field. Finite samples feed a DDSketch quantile
summary that produces count, min, q50/q90/q95/q99/q999, and max (values
formatted to 6 decimals). count is always emitted, so a registered histogram with no samples
still produces a stat=count row with value 0. Non-finite samples are reported as -inf,
inf, and nan counts.
Local histogram buckets are cleared after each snapshot, so quantiles describe the samples recorded
since the previous flush interval.
The output file is created immediately when the recorder is constructed. The base output path must not already exist; creation fails if it does. The CSV header is written once on creation.
§Output format
timestamp_rfc3339,name,kind,labels,value
2026-01-08T12:00:00.123Z,requests_total,counter,method=get,42
2026-01-08T12:00:00.123Z,temperature,gauge,,21.400000
2026-01-08T12:00:00.123Z,latency_ms,histogram,stat=count,8
2026-01-08T12:00:00.123Z,latency_ms,histogram,stat=q50,1.500000
2026-01-08T12:00:00.123Z,latency_ms,histogram,stat=q99,2.000000§Name and label encoding
http_requests_total
method=get|status=200Labels are emitted in the order provided by the metrics key. Components are lowercased, and
whitespace plus |/= are normalized to underscores.
§Basic usage (Smol runtime)
use std::{time::Duration};
use metrics_exporter_csv::{CsvBuilder};
let (recorder, exporter) = CsvBuilder::default()
.with_path("metrics.csv")
.with_flush_interval(Duration::from_secs(3))
.build()
.expect("build recorder");
metrics::set_global_recorder(recorder).expect("install recorder");
smol::spawn(exporter);§Deterministic shutdown
Drop the recorder to request a final flush. The exporter future should be allowed to run until it completes.
§Runtime requirement
CsvBuilder::build returns an exporter future that must be driven by a Smol executor (for
example via smol::spawn or smol::block_on). CsvBuilder::install spawns a background
thread that runs a single-threaded Smol executor to drive the exporter future and returns
once the thread is started.
When built with the tokio feature, CsvBuilder::install_with_tokio spawns the exporter
future onto the current Tokio runtime.
Structs§
- CsvBuilder
- Builder for configuring and installing the CSV recorder.
- CsvRecorder
- CSV recorder implementing
metrics::Recorder.
Enums§
- Build
Error - Error type for builder and installation operations.
- CsvError
- Error type for the CSV exporter.
Type Aliases§
- Exporter
Future - The exporter background task future.