metrics-exporter-csv
CSV exporter for the metrics crate.
Installation
[]
= "0.1.0"
Usage (Smol runtime)
use ;
use ;
# async
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.
Quick install (background thread)
use ;
use ;
default
.with_path
.with_flush_interval
.install
.expect;
Tokio install (optional)
Enable the tokio feature to spawn the exporter on the current Tokio runtime:
[]
= { = "0.1.0", = ["tokio"] }
use ;
use ;
async
Output format (long-form)
Each flush writes one row per metric, except histograms, which write one row per statistic:
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
2026-01-08T12:00:00.123Z,latency_ms,histogram,stat=max,5.000000
timestamp_rfc3339is UTC with millisecond precision.kindis one ofcounter,gauge, orhistogram.nameis the sanitized metric name.labelsis a|-delimited list ofkey=valuepairs (empty when no labels). Histogram rows additionally carry the statistic as astat=<name>pair, appended to any real labels.- For locally recorded metrics,
valueis a scalar, formatted as:- Counters: integer value at flush time.
- Gauges: fixed 6-decimal float (rounded).
- Histograms: one row per statistic, with the statistic named in
stat=. Finite samples feed a DDSketch quantile summary producingcount,min,q50/q90/q95/q99/q999, andmax(6-decimal floats;countis an integer).countis always present, so an empty histogram still emits astat=countrow with value0. Non-finite samples are reported as-inf,inf, andnancounts. Quantiles describe only the samples recorded in the interval, since buckets are drained each flush.
Name and label encoding
The name column is the sanitized metric name, and labels encodes label key/value pairs:
http_requests_total
method=get|status=200
Labels are emitted in the order provided by the metrics key (no additional sorting).
Components are lowercased, and whitespace plus | and = characters are normalized to
underscores. Other characters are preserved.
Flush semantics
- The recorder snapshots all known metrics every flush interval (default: 3 seconds).
- Counters and gauges are sampled at flush time.
- Locally recorded histograms are cleared on each snapshot, so histogram counts represent the samples recorded since the previous flush interval.
Deterministic shutdown
Drop the recorder to request a final flush. The exporter future should be allowed to run until it completes.
Multi-exporter composition
When combining multiple recorders, build the CSV recorder and fan out with metrics-util:
use ;
use ;
# async
Notes
- Output is long-form CSV with a fixed header:
timestamp_rfc3339,name,kind,labels,value. - The
namefield is the sanitized metric name. Thelabelsfield containskey=valuepairs delimited by|in the order provided by the metrics key. Components are lowercased and whitespace plus|/=are normalized to underscores. - Locally recorded histograms emit one row per statistic, with the statistic in a
stat=<name>label and a scalarvalue:count,min,q50/q90/q95/q99/q999,max(quantiles from a DDSketch summary), plus-inf/inf/nancounts when present. - If the path has no extension,
.csvis appended. - The base path must not already exist; creation fails with an error if it does. The base file is created when the recorder is constructed and the header is written once.
Received snapshots
Enable the optional prometheus feature to implement SnapshotSource, whose
snapshot method returns Vec<MetricFamily> from
prometheus_client::encoding::prometheus_protobuf::prometheus_data_model.
Pass that source to CsvBuilder::with_snapshot_source.
Each flush writes the source's current counters, gauges, and histograms alongside
local measurements, preserving source labels with the encoding described above.
Other metric types are ignored. Histogram rows use stat labels for count, sum,
schema, zero threshold/count, and the original bucket, span, delta, and count
collections. Collections are debug-formatted strings in the value column;
buckets are not decoded or converted into interval quantiles.