1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! 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
//! ```text
//! 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
//! ```text
//! http_requests_total
//! method=get|status=200
//! ```
//!
//! Labels are emitted in the order provided by the metrics key. Components are lowercased, and
//! whitespace plus `|`/`=` are normalized to underscores.
//!
//! ## Basic usage (Smol runtime)
//! ```no_run
//! use std::{time::Duration};
//! use metrics_exporter_csv::{CsvBuilder};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! 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);
//! # Ok(())
//! # }
//! ```
//!
//! ## 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.
pub use crate::;
pub use SnapshotSource;