Expand description
Prometheus extensions for richer metric collection.
This crate provides three utilities that complement the
prometheus crate:
| Type | Purpose |
|---|---|
AggregateCounter | A CounterVec wrapper that automatically emits an extra unlabeled total alongside every per-label counter. |
ScientificEncoder | A lightweight Prometheus text-format encoder that renders counter values in scientific notation (1.23E4) with a trailing comma after the last label — matching the format Kafka JMX exporters produce. |
Sensor | A lock-free exponential moving average (EMA) gauge for tracking rates or throughput. |
§Quick start
use prometheus::Opts;
use prometheus::core::Collector;
use prometheus_extensions::{AggregateCounter, ScientificEncoder};
// 1. Create an aggregate counter
let counter = AggregateCounter::new(
Opts::new("http_requests_total", "Total HTTP requests"),
&["method"],
).unwrap();
counter.with_label_values(&["GET"]).inc_by(100.0);
counter.with_label_values(&["POST"]).inc_by(42.0);
// Collecting yields 3 metrics: the unlabeled total (142) plus the two labeled ones.
let families = counter.collect();
assert_eq!(families[0].get_metric().len(), 3);
// 2. Encode in Kafka-compatible scientific notation
let encoder = ScientificEncoder::new();
let mut buf = Vec::new();
encoder.encode(&families, &mut buf).unwrap();
let output = String::from_utf8(buf).unwrap();
assert!(output.contains("http_requests_total 1.4200000000E2"));§Sensor (EMA)
use prometheus_extensions::Sensor;
let sensor = Sensor::new(0.05); // alpha = 0.05
sensor.measure(100.0);
sensor.measure(200.0);
// EMA tracks the smoothed value
let value = sensor.get();
assert!(value > 0.0);Structs§
- Aggregate
Counter - A Prometheus
CounterVecwrapper that automatically produces an extra unlabeled aggregate metric (the sum of all label combinations) in addition to the per-label counters. - Scientific
Encoder - A Prometheus text-format encoder that renders counter values in scientific
notation (
{:.10E}) and appends a trailing comma after the last label. - Sensor
- A lock-free exponential moving average (EMA) gauge.