Skip to content

Commit

Permalink
Do not suppress 0 values
Browse files Browse the repository at this point in the history
0 is a valid value for a gauge or a counter (e.g. "number of active sessions"
can easily be zero and that state is distinct from is distinct 1) and zero
samples in a period is legit for a histogram over an interval.

For gauges we should also not reset to zero on each render, but we should
continue to do so for counters.

Suppression of idle metrics should be done using `StatsdBuilder::idle_timeout`.

Fixes dialtone#5
  • Loading branch information
ijc committed Nov 15, 2024
1 parent eed983f commit 80d8143
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
13 changes: 8 additions & 5 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,6 @@ impl StatsdBuilder {
///
/// Refer to the documentation for [`MetricKindMask`](metrics_util::MetricKindMask) for more
/// information on defining a metric kind mask.
///
/// When a metric is rendered its value is replaced with a "zero-value" for that `MetricKind`
/// however any metric with a state "zero-value" will not be rendered and will be cleaned up
/// when its corresponding idle timeout expires.
#[must_use]
pub fn idle_timeout(mut self, mask: MetricKindMask, timeout: Option<Duration>) -> Self {
self.idle_timeout = timeout;
Expand Down Expand Up @@ -614,7 +610,12 @@ mod tests {
gauge1.set(-3.44);
let rendered = handle.render();
// each render call will reset the value of the counter
let expected_gauge = "basic.gauge:-3.44|g|#wutang:forever\n\n";
let expected_gauge = concat!(
"basic.counter:0|c\n",
"\n",
"basic.gauge:-3.44|g|#wutang:forever\n",
"\n",
);
assert_eq!(rendered, expected_gauge);

let key = Key::from_name("basic.histogram");
Expand All @@ -623,6 +624,8 @@ mod tests {
let rendered = handle.render();

let histogram_data = concat!(
"basic.counter:0|c\n\n",
"basic.gauge:-3.44|g|#wutang:forever\n\n",
"basic.histogram.min:12|g\n",
"basic.histogram.max:12|g\n",
"basic.histogram.avg:12|g\n",
Expand Down
14 changes: 1 addition & 13 deletions src/recorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Inner {
}

let (name, labels) = key_to_parts(&key, Some(&self.global_tags));
let value = f64::from_bits(gauge.get_inner().swap(0, Ordering::Acquire));
let value = f64::from_bits(gauge.get_inner().load(Ordering::Acquire));
let entry = gauges
.entry(name)
.or_insert_with(HashMap::new)
Expand Down Expand Up @@ -103,9 +103,6 @@ impl Inner {
for (name, mut by_labels) in counters.drain() {
let mut wrote = false;
for (labels, value) in by_labels.drain() {
if value == 0 {
continue;
}
wrote = true;
write_metric_line::<&str, u64>(
&mut output,
Expand All @@ -128,9 +125,6 @@ impl Inner {
for (name, mut by_labels) in gauges.drain() {
let mut wrote = false;
for (labels, value) in by_labels.drain() {
if value == 0.0 {
continue;
}
wrote = true;
write_metric_line::<&str, f64>(
&mut output,
Expand All @@ -156,9 +150,6 @@ impl Inner {
let (sum, count) = match distribution {
Distribution::Summary(summary, quantiles, sum) => {
let count = summary.count();
if count == 0 {
continue;
}
wrote = true;
let snapshot = summary.snapshot(Instant::now());
for quantile in quantiles.iter() {
Expand Down Expand Up @@ -192,9 +183,6 @@ impl Inner {
}
Distribution::Histogram(histogram) => {
let count = histogram.count();
if count == 0 {
continue;
}
wrote = true;
for (le, count) in histogram.buckets() {
write_metric_line(
Expand Down

0 comments on commit 80d8143

Please sign in to comment.