Skip to content

Commit 0097d16

Browse files
authored
Simplify Histogram code (#2028)
1 parent cb1ffb5 commit 0097d16

File tree

1 file changed

+30
-45
lines changed

1 file changed

+30
-45
lines changed

opentelemetry-sdk/src/metrics/internal/histogram.rs

+30-45
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,36 @@ impl<T: Number<T>> Buckets<T> {
4343
}
4444
}
4545

46-
/// Summarizes a set of measurements with explicitly defined buckets.
47-
struct HistValues<T> {
48-
record_sum: bool,
49-
bounds: Vec<f64>,
46+
/// Summarizes a set of measurements as a histogram with explicitly defined
47+
/// buckets.
48+
pub(crate) struct Histogram<T> {
5049
values: Mutex<HashMap<AttributeSet, Buckets<T>>>,
50+
bounds: Vec<f64>,
51+
record_min_max: bool,
52+
record_sum: bool,
53+
start: Mutex<SystemTime>,
5154
}
5255

53-
impl<T: Number<T>> HistValues<T> {
54-
fn new(mut bounds: Vec<f64>, record_sum: bool) -> Self {
55-
bounds.retain(|v| !v.is_nan());
56-
bounds.sort_by(|a, b| a.partial_cmp(b).expect("NaNs filtered out"));
57-
58-
HistValues {
59-
record_sum,
60-
bounds,
56+
impl<T: Number<T>> Histogram<T> {
57+
pub(crate) fn new(boundaries: Vec<f64>, record_min_max: bool, record_sum: bool) -> Self {
58+
let mut histogram = Histogram {
6159
values: Mutex::new(Default::default()),
62-
}
60+
bounds: boundaries,
61+
record_min_max,
62+
record_sum,
63+
start: Mutex::new(SystemTime::now()),
64+
};
65+
66+
histogram.bounds.retain(|v| !v.is_nan());
67+
histogram
68+
.bounds
69+
.sort_by(|a, b| a.partial_cmp(b).expect("NaNs filtered out"));
70+
71+
histogram
6372
}
64-
}
6573

66-
impl<T: Number<T>> HistValues<T> {
67-
fn measure(&self, measurement: T, attrs: AttributeSet) {
74+
pub(crate) fn measure(&self, measurement: T, attrs: &[KeyValue]) {
75+
let attrs: AttributeSet = attrs.into();
6876
let f = measurement.into_float();
6977

7078
// This search will return an index in the range `[0, bounds.len()]`, where
@@ -109,35 +117,12 @@ impl<T: Number<T>> HistValues<T> {
109117
b.sum(measurement)
110118
}
111119
}
112-
}
113-
114-
/// Summarizes a set of measurements as a histogram with explicitly defined
115-
/// buckets.
116-
pub(crate) struct Histogram<T> {
117-
hist_values: HistValues<T>,
118-
record_min_max: bool,
119-
start: Mutex<SystemTime>,
120-
}
121-
122-
impl<T: Number<T>> Histogram<T> {
123-
pub(crate) fn new(boundaries: Vec<f64>, record_min_max: bool, record_sum: bool) -> Self {
124-
Histogram {
125-
hist_values: HistValues::new(boundaries, record_sum),
126-
record_min_max,
127-
start: Mutex::new(SystemTime::now()),
128-
}
129-
}
130-
131-
pub(crate) fn measure(&self, measurement: T, attrs: &[KeyValue]) {
132-
let attrs: AttributeSet = attrs.into();
133-
self.hist_values.measure(measurement, attrs)
134-
}
135120

136121
pub(crate) fn delta(
137122
&self,
138123
dest: Option<&mut dyn Aggregation>,
139124
) -> (usize, Option<Box<dyn Aggregation>>) {
140-
let mut values = match self.hist_values.values.lock() {
125+
let mut values = match self.values.lock() {
141126
Ok(guard) if !guard.is_empty() => guard,
142127
_ => return (0, None),
143128
};
@@ -174,9 +159,9 @@ impl<T: Number<T>> Histogram<T> {
174159
start_time: start,
175160
time: t,
176161
count: b.count,
177-
bounds: self.hist_values.bounds.clone(),
162+
bounds: self.bounds.clone(),
178163
bucket_counts: b.counts.clone(),
179-
sum: if self.hist_values.record_sum {
164+
sum: if self.record_sum {
180165
b.total
181166
} else {
182167
T::default()
@@ -207,7 +192,7 @@ impl<T: Number<T>> Histogram<T> {
207192
&self,
208193
dest: Option<&mut dyn Aggregation>,
209194
) -> (usize, Option<Box<dyn Aggregation>>) {
210-
let values = match self.hist_values.values.lock() {
195+
let values = match self.values.lock() {
211196
Ok(guard) if !guard.is_empty() => guard,
212197
_ => return (0, None),
213198
};
@@ -248,9 +233,9 @@ impl<T: Number<T>> Histogram<T> {
248233
start_time: start,
249234
time: t,
250235
count: b.count,
251-
bounds: self.hist_values.bounds.clone(),
236+
bounds: self.bounds.clone(),
252237
bucket_counts: b.counts.clone(),
253-
sum: if self.hist_values.record_sum {
238+
sum: if self.record_sum {
254239
b.total
255240
} else {
256241
T::default()

0 commit comments

Comments
 (0)