Skip to content
Merged
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
28 changes: 15 additions & 13 deletions unleash-yggdrasil/src/impact_metrics/gauge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ impl Gauge {
}

fn set_internal(&self, value: f64, labels: Option<&MetricLabels>) {
if !value.is_finite() {
return;
}
let key = get_label_key(labels);
self.values.insert(key, value);
}
Expand All @@ -42,6 +45,9 @@ impl Gauge {
}

fn inc_internal(&self, value: f64, labels: Option<&MetricLabels>) {
if !value.is_finite() {
return;
}
let key = get_label_key(labels);
self.values
.entry(key)
Expand All @@ -62,6 +68,9 @@ impl Gauge {
}

fn dec_internal(&self, value: f64, labels: Option<&MetricLabels>) {
if !value.is_finite() {
return;
}
let key = get_label_key(labels);
self.values
.entry(key)
Expand All @@ -70,20 +79,13 @@ impl Gauge {
}

pub(crate) fn collect(&self) -> CollectedMetric {
let mut samples = Vec::new();
let samples: Vec<GaugeMetricSample> = self
.values
.iter()
.map(|entry| GaugeMetricSample::new(parse_label_key(entry.key()), *entry.value()))
.collect();

for mut entry in self.values.iter_mut() {
let value = std::mem::take(entry.value_mut());
if value != 0.0 {
samples.push(GaugeMetricSample::new(parse_label_key(entry.key()), value));
}
}

self.values.retain(|_, v| *v != 0.0);

if samples.is_empty() {
samples.push(GaugeMetricSample::zero());
}
self.values.clear();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a small race window where values inserted between iteration and clear may be lost. should be fine for gauges

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think it's a problem here, the lock is held at a higher level in Ygg, this is all sync code unless Edge decides it wants to start doing this for whatever reason


CollectedMetric::new_gauge(&self.opts.name, &self.opts.help, samples)
}
Expand Down
60 changes: 60 additions & 0 deletions unleash-yggdrasil/src/impact_metrics/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ mod tests {
BucketMetricSample, CounterMetricSample, GaugeMetricSample, HistogramBucket,
};
use std::collections::HashMap;
use test_case::test_case;

fn counter_sample(value: i64) -> CounterMetricSample {
CounterMetricSample::new(HashMap::new(), value)
Expand Down Expand Up @@ -381,6 +382,26 @@ mod tests {
);
}

#[test]
fn should_return_empty_samples_for_gauge_after_collect() {
let registry = InMemoryMetricRegistry::default();
registry.define_gauge(MetricOptions::new("test_gauge", "gauge test"));

registry.set_gauge("test_gauge", 5.0);

let first_collect = registry.collect();
let expected1 = CollectedMetric::new_gauge(
"test_gauge",
"gauge test",
vec![GaugeMetricSample::new(HashMap::new(), 5.0)],
);
assert_eq!(first_collect, vec![expected1]);

let second_collect = registry.collect();
let expected2 = CollectedMetric::new_gauge("test_gauge", "gauge test", vec![]);
assert_eq!(second_collect, vec![expected2]);
}

#[test]
fn should_observe_histogram_values() {
let registry = InMemoryMetricRegistry::default();
Expand Down Expand Up @@ -541,4 +562,43 @@ mod tests {

assert_eq!(restored_samples, original_samples);
}

#[test_case(f64::INFINITY; "positive infinity")]
#[test_case(f64::NEG_INFINITY; "negative infinity")]
#[test_case(f64::NAN; "NaN")]
fn should_silently_drop_invalid_values_for_all_metrics(invalid: f64) {
let registry = InMemoryMetricRegistry::default();
registry.define_gauge(MetricOptions::new("g", "h"));
registry.define_histogram(BucketMetricOptions::new("h", "h", vec![1.0]));

registry.set_gauge("g", 5.0);
registry.set_gauge("g", invalid);
registry.inc_gauge_by("g", invalid);
registry.dec_gauge_by("g", invalid);
registry.observe_histogram("h", 0.5);
registry.observe_histogram("h", invalid);

let metrics = registry.collect();

assert_eq!(
metrics,
vec![
CollectedMetric::new_gauge(
"g",
"h",
vec![GaugeMetricSample::new(HashMap::new(), 5.0)]
),
CollectedMetric::new_bucket(
"h",
"h",
vec![BucketMetricSample {
labels: HashMap::new(),
count: 1,
sum: 0.5,
buckets: vec![bucket(1.0, 1), bucket(f64::INFINITY, 1)],
}]
),
]
);
}
}
7 changes: 0 additions & 7 deletions unleash-yggdrasil/src/impact_metrics/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,6 @@ impl GaugeMetricSample {
pub(crate) fn new(labels: MetricLabels, value: f64) -> Self {
Self { labels, value }
}

pub(crate) fn zero() -> Self {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was different from node and java

Self {
labels: HashMap::new(),
value: 0.0,
}
}
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand Down
Loading