Skip to content

Commit

Permalink
counter should not reset...
Browse files Browse the repository at this point in the history
  • Loading branch information
timglabisch committed Dec 13, 2023
1 parent 70dae0b commit bc6b693
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

#[derive(Debug, Default, Clone)]
pub struct ResettingCounterMetric {
pub struct CounterMetric {
val: Arc<AtomicU64>,
}

impl ModifyMetric for ResettingCounterMetric {
impl ModifyMetric for CounterMetric {
fn observe(&self, value: i32) {
if let Ok(val_as_u64) = u64::try_from(value) {
self.val.fetch_add(val_as_u64, Ordering::Relaxed);
}
}
}

impl EncodeMetric for ResettingCounterMetric {
impl EncodeMetric for CounterMetric {
fn encode(&self, mut encoder: MetricEncoder) -> Result<(), Error> {
let current_value = self.val.swap(0, Ordering::Relaxed);
let current_value = self.val.load(Ordering::Relaxed);
encoder.encode_counter::<(), u64, u64>(&current_value, None)
}

Expand All @@ -29,6 +29,6 @@ impl EncodeMetric for ResettingCounterMetric {
}
}

impl TypedMetric for ResettingCounterMetric {
impl TypedMetric for CounterMetric {
const TYPE: MetricType = MetricType::Counter;
}
2 changes: 1 addition & 1 deletion openmetrics_udpserver/src/metrics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod resetting_counter;
pub mod counter;
pub mod resetting_value_metric;

pub trait ModifyMetric {
Expand Down
6 changes: 3 additions & 3 deletions openmetrics_udpserver/src/processor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::config::Config;
use crate::metrics::resetting_counter::ResettingCounterMetric;
use crate::metrics::counter::CounterMetric;
use crate::metrics::resetting_value_metric::ResettingSingleValMetric;
use crate::metrics::ModifyMetric;
use anyhow::anyhow;
Expand Down Expand Up @@ -70,7 +70,7 @@ impl Processor {
MetricType::Sum => {
let metric_get_result = self
.get_or_register_metric(&metric_name, || {
ResettingCounterMetric::default()
CounterMetric::default()
});
Self::observe_metric(metric_get_result, inbound_metric).await;
}
Expand Down Expand Up @@ -122,7 +122,7 @@ impl Processor {

let rc_metric = Arc::new(metric);
self.metrics.insert(metric_name.clone(), rc_metric.clone());
Ok(rc_metric as Arc<dyn ModifyMetric + Send + Sync>)
Ok(rc_metric)
}
Err(_) => Err(anyhow!("unable to lock metric registry for writing")),
};
Expand Down

0 comments on commit bc6b693

Please sign in to comment.