Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clear/reset methods for histograms and counters #199

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/encoding/protobuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,35 @@ mod tests {
);
assert_eq!(1, value.count);
assert_eq!(11, value.buckets.len());
assert_eq!(1u64, value.buckets.iter().map(|bucket| bucket.count).sum());
}
_ => panic!("wrong value type"),
}

histogram.clear();

let metric_set = encode(&registry).unwrap();

let family = metric_set.metric_families.first().unwrap();
assert_eq!("my_histogram", family.name);
assert_eq!("My histogram.", family.help);

assert_eq!(
openmetrics_data_model::MetricType::Histogram as i32,
extract_metric_type(&metric_set)
);

match extract_metric_point_value(&metric_set) {
openmetrics_data_model::metric_point::Value::HistogramValue(value) => {
assert_eq!(
Some(openmetrics_data_model::histogram_value::Sum::DoubleValue(
0.0
)),
value.sum
);
assert_eq!(0, value.count);
assert_eq!(11, value.buckets.len());
assert_eq!(0u64, value.buckets.iter().map(|bucket| bucket.count).sum());
}
_ => panic!("wrong value type"),
}
Expand Down
28 changes: 28 additions & 0 deletions src/metrics/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ impl<N, A: Atomic<N>> Counter<N, A> {
self.value.inc_by(v)
}

/// Resets the [`Counter`] to `0`, returning the previous value.
pub fn reset(&self) -> N {
self.value.reset()
}

/// Get the current value of the [`Counter`].
pub fn get(&self) -> N {
self.value.get()
Expand All @@ -110,6 +115,9 @@ pub trait Atomic<N> {
/// Increase the value.
fn inc_by(&self, v: N) -> N;

/// Reset the value to `0`.
fn reset(&self) -> N;

/// Get the the value.
fn get(&self) -> N;
}
Expand All @@ -124,6 +132,10 @@ impl Atomic<u64> for AtomicU64 {
self.fetch_add(v, Ordering::Relaxed)
}

fn reset(&self) -> u64 {
self.swap(Default::default(), Ordering::Relaxed)
}

fn get(&self) -> u64 {
self.load(Ordering::Relaxed)
}
Expand All @@ -138,6 +150,10 @@ impl Atomic<u32> for AtomicU32 {
self.fetch_add(v, Ordering::Relaxed)
}

fn reset(&self) -> u32 {
self.swap(Default::default(), Ordering::Relaxed)
}

fn get(&self) -> u32 {
self.load(Ordering::Relaxed)
}
Expand All @@ -164,6 +180,10 @@ impl Atomic<f64> for AtomicU64 {
old_f64
}

fn reset(&self) -> f64 {
f64::from_bits(self.swap(Default::default(), Ordering::Relaxed))
}

fn get(&self) -> f64 {
f64::from_bits(self.load(Ordering::Relaxed))
}
Expand Down Expand Up @@ -231,6 +251,14 @@ mod tests {
assert_eq!(1, counter.get());
}

#[test]
fn inc_reset_and_get() {
let counter: Counter = Counter::default();
assert_eq!(0, counter.inc());
assert_eq!(1, counter.reset());
assert_eq!(0, counter.get());
}

#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
#[test]
fn f64_stored_in_atomic_u64() {
Expand Down
12 changes: 12 additions & 0 deletions src/metrics/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ impl Histogram {
}
}

/// Clears all observations.
pub fn clear(&self) {
let mut inner = self.inner.write();
inner.sum = Default::default();
inner.count = Default::default();

for (_, value) in &mut inner.buckets {
*value = 0;
}
}

pub(crate) fn get(&self) -> (f64, u64, MappedRwLockReadGuard<Vec<(f64, u64)>>) {
let inner = self.inner.read();
let sum = inner.sum;
Expand Down Expand Up @@ -149,6 +160,7 @@ mod tests {
fn histogram() {
let histogram = Histogram::new(exponential_buckets(1.0, 2.0, 10));
histogram.observe(1.0);
histogram.clear();
}

#[test]
Expand Down