#[derive(ExportMetrics)]
{
// Attributes available to this derive:
#[metric_prefix]
#[help]
#[otlp]
#[clickhouse]
}
Expand description
Derive macro for exporting metrics in Prometheus, DogStatsD, and OTLP formats.
Generates methods:
export_prometheus(&self, output: &mut String)— Prometheus text formatexport_dogstatsd(&self, output: &mut String, tags: &[(&str, &str)])— DogStatsD formatexport_dogstatsd_delta(...)— DogStatsD with per-sink delta temporalityexport_dogstatsd_with_temporality(...)— runtime-selectable cumulative or delta exportexport_otlp(...)— OTLP protobuf (only when#[otlp]attribute is present)export_clickhouse(...)— ClickHouse rows (only when#[clickhouse]attribute is present)
Supports unlabeled metrics (Counter, Gauge, GaugeF64, Histogram,
Distribution, SampledTimer), compile-time labeled metrics
(LabeledCounter<L>, LabeledGauge<L>, LabeledHistogram<L>,
LabeledSampledTimer<L>), and runtime-labeled metrics (DynamicCounter,
DynamicGauge, DynamicGaugeI64, DynamicHistogram, DynamicDistribution).
§Example
ⓘ
use fast_telemetry::{Counter, Histogram, Gauge, LabeledCounter, DeriveLabel};
#[derive(Copy, Clone, Debug, DeriveLabel)]
#[label_name = "method"]
enum HttpMethod { Get, Post, Put, Delete }
#[derive(ExportMetrics)]
#[metric_prefix = "proxy"]
pub struct ProxyMetrics {
#[help = "Total requests proxied"]
pub requests: Counter,
#[help = "Requests by HTTP method"]
pub requests_by_method: LabeledCounter<HttpMethod>,
#[help = "Request latency in microseconds"]
pub latency: Histogram,
#[help = "Current memory usage"]
pub memory_mb: Gauge,
}
let metrics = ProxyMetrics::new();
// Prometheus export
let mut prom_output = String::new();
metrics.export_prometheus(&mut prom_output);
// DogStatsD export (with optional tags)
let mut statsd_output = String::new();
metrics.export_dogstatsd(&mut statsd_output, &[("env", "prod")]);