-
Notifications
You must be signed in to change notification settings - Fork 544
ValueMap interface change #2117
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
Merged
cijothomas
merged 5 commits into
open-telemetry:main
from
fraillt:value-map-interface-change
Nov 1, 2024
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
606d126
ValueMap interface change
fraillt 5ff18ca
Merge branch 'main' into value-map-interface-change
lalitb 1f54f90
Merge branch 'main' into value-map-interface-change
cijothomas ffe0d4a
Merge branch 'main' into value-map-interface-change
lalitb 133f317
Merge branch 'main' into value-map-interface-change
cijothomas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,23 +7,22 @@ use crate::metrics::data::HistogramDataPoint; | |
use crate::metrics::data::{self, Aggregation, Temporality}; | ||
use opentelemetry::KeyValue; | ||
|
||
use super::Number; | ||
use super::{AtomicTracker, AtomicallyUpdate, Operation, ValueMap}; | ||
|
||
struct HistogramUpdate; | ||
|
||
impl Operation for HistogramUpdate { | ||
fn update_tracker<T: Default, AT: AtomicTracker<T>>(tracker: &AT, value: T, index: usize) { | ||
tracker.update_histogram(index, value); | ||
} | ||
} | ||
use super::ValueMap; | ||
use super::{Aggregator, Number}; | ||
|
||
struct HistogramTracker<T> { | ||
buckets: Mutex<Buckets<T>>, | ||
} | ||
|
||
impl<T: Number> AtomicTracker<T> for HistogramTracker<T> { | ||
fn update_histogram(&self, index: usize, value: T) { | ||
impl<T> Aggregator<T> for HistogramTracker<T> | ||
where | ||
T: Number, | ||
{ | ||
type InitConfig = usize; | ||
/// Value and bucket index | ||
type PreComputedValue = (T, usize); | ||
|
||
fn update(&self, (value, index): (T, usize)) { | ||
let mut buckets = match self.buckets.lock() { | ||
Ok(guard) => guard, | ||
Err(_) => return, | ||
|
@@ -32,15 +31,10 @@ impl<T: Number> AtomicTracker<T> for HistogramTracker<T> { | |
buckets.bin(index, value); | ||
buckets.sum(value); | ||
} | ||
} | ||
|
||
impl<T: Number> AtomicallyUpdate<T> for HistogramTracker<T> { | ||
type AtomicTracker = HistogramTracker<T>; | ||
|
||
fn new_atomic_tracker(buckets_count: Option<usize>) -> Self::AtomicTracker { | ||
let count = buckets_count.unwrap(); | ||
fn create(count: &usize) -> Self { | ||
HistogramTracker { | ||
buckets: Mutex::new(Buckets::<T>::new(count)), | ||
buckets: Mutex::new(Buckets::<T>::new(*count)), | ||
} | ||
} | ||
} | ||
|
@@ -94,7 +88,7 @@ impl<T: Number> Buckets<T> { | |
/// Summarizes a set of measurements as a histogram with explicitly defined | ||
/// buckets. | ||
pub(crate) struct Histogram<T: Number> { | ||
value_map: ValueMap<HistogramTracker<T>, T, HistogramUpdate>, | ||
value_map: ValueMap<T, HistogramTracker<T>>, | ||
bounds: Vec<f64>, | ||
record_min_max: bool, | ||
record_sum: bool, | ||
|
@@ -103,9 +97,11 @@ pub(crate) struct Histogram<T: Number> { | |
|
||
impl<T: Number> Histogram<T> { | ||
pub(crate) fn new(boundaries: Vec<f64>, record_min_max: bool, record_sum: bool) -> Self { | ||
// TODO fix the bug, by first removing NaN and only then getting buckets_count | ||
// once we know the reason for performance degradation | ||
let buckets_count = boundaries.len() + 1; | ||
let mut histogram = Histogram { | ||
value_map: ValueMap::new_with_buckets_count(buckets_count), | ||
value_map: ValueMap::new(buckets_count), | ||
bounds: boundaries, | ||
record_min_max, | ||
record_sum, | ||
|
@@ -122,14 +118,20 @@ impl<T: Number> Histogram<T> { | |
|
||
pub(crate) fn measure(&self, measurement: T, attrs: &[KeyValue]) { | ||
let f = measurement.into_float(); | ||
|
||
// Ignore NaN and infinity. | ||
// Only makes sense if T is f64, maybe this could be no-op for other cases? | ||
// TODO: uncomment once we know the reason for performance degradation | ||
// if f.is_infinite() || f.is_nan() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cijothomas Let's add this check as well? |
||
// return; | ||
// } | ||
// This search will return an index in the range `[0, bounds.len()]`, where | ||
// it will return `bounds.len()` if value is greater than the last element | ||
// of `bounds`. This aligns with the buckets in that the length of buckets | ||
// is `bounds.len()+1`, with the last bucket representing: | ||
// `(bounds[bounds.len()-1], +∞)`. | ||
let index = self.bounds.partition_point(|&x| x < f); | ||
self.value_map.measure(measurement, attrs, index); | ||
|
||
self.value_map.measure((measurement, index), attrs); | ||
} | ||
|
||
pub(crate) fn delta( | ||
|
@@ -350,3 +352,69 @@ impl<T: Number> Histogram<T> { | |
(h.data_points.len(), new_agg.map(|a| Box::new(a) as Box<_>)) | ||
} | ||
} | ||
|
||
// TODO: uncomment once we know the reason for performance degradation | ||
// #[cfg(test)] | ||
// mod tests { | ||
|
||
// use super::*; | ||
|
||
// #[test] | ||
// fn when_f64_is_nan_or_infinity_then_ignore() { | ||
// struct Expected { | ||
// min: f64, | ||
// max: f64, | ||
// sum: f64, | ||
// count: u64, | ||
// } | ||
// impl Expected { | ||
// fn new(min: f64, max: f64, sum: f64, count: u64) -> Self { | ||
// Expected { | ||
// min, | ||
// max, | ||
// sum, | ||
// count, | ||
// } | ||
// } | ||
// } | ||
// struct TestCase { | ||
// values: Vec<f64>, | ||
// expected: Expected, | ||
// } | ||
|
||
// let test_cases = vec![ | ||
// TestCase { | ||
// values: vec![2.0, 4.0, 1.0], | ||
// expected: Expected::new(1.0, 4.0, 7.0, 3), | ||
// }, | ||
// TestCase { | ||
// values: vec![2.0, 4.0, 1.0, f64::INFINITY], | ||
// expected: Expected::new(1.0, 4.0, 7.0, 3), | ||
// }, | ||
// TestCase { | ||
// values: vec![2.0, 4.0, 1.0, -f64::INFINITY], | ||
// expected: Expected::new(1.0, 4.0, 7.0, 3), | ||
// }, | ||
// TestCase { | ||
// values: vec![2.0, f64::NAN, 4.0, 1.0], | ||
// expected: Expected::new(1.0, 4.0, 7.0, 3), | ||
// }, | ||
// TestCase { | ||
// values: vec![4.0, 4.0, 4.0, 2.0, 16.0, 1.0], | ||
// expected: Expected::new(1.0, 16.0, 31.0, 6), | ||
// }, | ||
// ]; | ||
|
||
// for test in test_cases { | ||
// let h = Histogram::new(vec![], true, true); | ||
// for v in test.values { | ||
// h.measure(v, &[]); | ||
// } | ||
// let res = h.value_map.no_attribute_tracker.buckets.lock().unwrap(); | ||
// assert_eq!(test.expected.max, res.max); | ||
// assert_eq!(test.expected.min, res.min); | ||
// assert_eq!(test.expected.sum, res.total); | ||
// assert_eq!(test.expected.count, res.count); | ||
// } | ||
// } | ||
// } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.