pub struct Counter<N = u64, A = AtomicU64> { /* private fields */ }
Expand description

Open Metrics Counter to measure discrete events.

Single monotonically increasing value metric.

Counter is generic over the actual data type tracking the Counter state as well as the data type used to interact with the Counter. Out of convenience the generic type parameters are set to use an AtomicU64 as a storage and u64 on the interface by default.

Examples

Using AtomicU64 as storage and u64 on the interface

let counter: Counter = Counter::default();
counter.inc();
let _value: u64 = counter.get();

Using AtomicU64 as storage and f64 on the interface

let counter = Counter::<f64, AtomicU64>::default();
counter.inc();
let _value: f64 = counter.get();

Implementations§

Increase the Counter by 1, returning the previous value.

Increase the Counter by v, returning the previous value.

Examples found in repository?
src/metrics/exemplar.rs (line 126)
118
119
120
121
122
123
124
125
126
127
    pub fn inc_by(&self, v: N, label_set: Option<S>) -> N {
        let mut inner = self.inner.write();

        inner.exemplar = label_set.map(|label_set| Exemplar {
            label_set,
            value: v.clone(),
        });

        inner.counter.inc_by(v)
    }

Get the current value of the Counter.

Examples found in repository?
src/metrics/counter.rs (line 182)
181
182
183
    fn encode(&self, mut encoder: MetricEncoder) -> Result<(), std::fmt::Error> {
        encoder.encode_counter::<(), _, u64>(&self.get(), None)
    }
More examples
Hide additional examples
src/metrics/exemplar.rs (line 133)
131
132
133
134
135
136
    pub fn get(&self) -> (N, MappedRwLockReadGuard<Option<Exemplar<S, N>>>) {
        let inner = self.inner.read();
        let value = inner.counter.get();
        let exemplar = RwLockReadGuard::map(inner, |inner| &inner.exemplar);
        (value, exemplar)
    }

Exposes the inner atomic type of the Counter.

This should only be used for advanced use-cases which are not directly supported by the library.

The caller of this function has to uphold the property of an Open Metrics counter namely that the value is monotonically increasing, i.e. either stays the same or increases.

Examples found in repository?
src/metrics/exemplar.rs (line 147)
146
147
148
    pub fn inner(&self) -> MappedRwLockReadGuard<A> {
        RwLockReadGuard::map(self.inner.read(), |inner| inner.counter.inner())
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Encode the given instance in the OpenMetrics text encoding.
The OpenMetrics metric type of the instance.
The OpenMetrics metric type.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.