pub struct AggregateCounter { /* private fields */ }Expand description
A Prometheus CounterVec wrapper that automatically produces an extra
unlabeled aggregate metric (the sum of all label combinations) in
addition to the per-label counters.
This is useful when you want a single metric name to expose both a total
and a per-dimension breakdown without maintaining a separate Counter.
§Example
use prometheus::Opts;
use prometheus::core::Collector;
use prometheus_extensions::AggregateCounter;
let counter = AggregateCounter::new(
Opts::new("rpc_calls_total", "Total RPC calls"),
&["service"],
).unwrap();
counter.with_label_values(&["auth"]).inc_by(10.0);
counter.with_label_values(&["billing"]).inc_by(20.0);
let families = counter.collect();
let metrics = families[0].get_metric();
// First metric is the aggregate (no labels, value = 30)
assert_eq!(metrics[0].get_label().len(), 0);
assert_eq!(metrics[0].get_counter().value(), 30.0);
// Remaining metrics are the per-label counters
assert_eq!(metrics.len(), 3); // 1 aggregate + 2 labeledImplementations§
Source§impl AggregateCounter
impl AggregateCounter
Sourcepub fn new(opts: Opts, label_names: &[&str]) -> Result<Self, Error>
pub fn new(opts: Opts, label_names: &[&str]) -> Result<Self, Error>
Create a new AggregateCounter.
opts defines the metric name and help string.
label_names are the label dimensions for the per-label counters.
Sourcepub fn with_label_values(&self, vals: &[&str]) -> Counter
pub fn with_label_values(&self, vals: &[&str]) -> Counter
Return a Counter for the given label values, creating it if it
does not already exist.
Trait Implementations§
Source§impl Clone for AggregateCounter
impl Clone for AggregateCounter
Source§fn clone(&self) -> AggregateCounter
fn clone(&self) -> AggregateCounter
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for AggregateCounter
impl !RefUnwindSafe for AggregateCounter
impl Send for AggregateCounter
impl Sync for AggregateCounter
impl Unpin for AggregateCounter
impl UnsafeUnpin for AggregateCounter
impl !UnwindSafe for AggregateCounter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more