Skip to content

Commit 3f87777

Browse files
committed
Move time from DataPoint to Sum/Gauge
1 parent 506a4f9 commit 3f87777

File tree

9 files changed

+132
-156
lines changed

9 files changed

+132
-156
lines changed

opentelemetry-proto/src/transform/metrics.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ pub mod tonic {
295295
.iter()
296296
.map(|dp| TonicNumberDataPoint {
297297
attributes: dp.attributes.iter().map(Into::into).collect(),
298-
start_time_unix_nano: to_nanos(dp.start_time),
299-
time_unix_nano: to_nanos(dp.time),
298+
start_time_unix_nano: to_nanos(sum.start_time),
299+
time_unix_nano: to_nanos(sum.time),
300300
exemplars: dp.exemplars.iter().map(Into::into).collect(),
301301
flags: TonicDataPointFlags::default() as u32,
302302
value: Some(dp.value.into()),
@@ -319,8 +319,8 @@ pub mod tonic {
319319
.iter()
320320
.map(|dp| TonicNumberDataPoint {
321321
attributes: dp.attributes.iter().map(Into::into).collect(),
322-
start_time_unix_nano: to_nanos(dp.start_time),
323-
time_unix_nano: to_nanos(dp.time),
322+
start_time_unix_nano: to_nanos(gauge.start_time),
323+
time_unix_nano: to_nanos(gauge.time),
324324
exemplars: dp.exemplars.iter().map(Into::into).collect(),
325325
flags: TonicDataPointFlags::default() as u32,
326326
value: Some(dp.value.into()),

opentelemetry-sdk/src/metrics/data/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ pub trait Aggregation: fmt::Debug + any::Any + Send + Sync {
5858
pub struct Gauge<T> {
5959
/// Represents individual aggregated measurements with unique attributes.
6060
pub data_points: Vec<DataPoint<T>>,
61+
/// The time when the time series was started.
62+
pub start_time: SystemTime,
63+
/// The time when the time series was recorded.
64+
pub time: SystemTime,
6165
}
6266

6367
impl<T: fmt::Debug + Send + Sync + 'static> Aggregation for Gauge<T> {
@@ -74,6 +78,10 @@ impl<T: fmt::Debug + Send + Sync + 'static> Aggregation for Gauge<T> {
7478
pub struct Sum<T> {
7579
/// Represents individual aggregated measurements with unique attributes.
7680
pub data_points: Vec<DataPoint<T>>,
81+
/// The time when the time series was started.
82+
pub start_time: SystemTime,
83+
/// The time when the time series was recorded.
84+
pub time: SystemTime,
7785
/// Describes if the aggregation is reported as the change from the last report
7886
/// time, or the cumulative changes since a fixed start time.
7987
pub temporality: Temporality,
@@ -96,10 +104,6 @@ pub struct DataPoint<T> {
96104
/// Attributes is the set of key value pairs that uniquely identify the
97105
/// time series.
98106
pub attributes: Vec<KeyValue>,
99-
/// The time when the time series was started.
100-
pub start_time: SystemTime,
101-
/// The time when the time series was recorded.
102-
pub time: SystemTime,
103107
/// The value of this data point.
104108
pub value: T,
105109
/// The sampled [Exemplar]s collected during the time series.
@@ -110,8 +114,6 @@ impl<T: Copy> Clone for DataPoint<T> {
110114
fn clone(&self) -> Self {
111115
Self {
112116
attributes: self.attributes.clone(),
113-
start_time: self.start_time,
114-
time: self.time,
115117
value: self.value,
116118
exemplars: self.exemplars.clone(),
117119
}
@@ -338,8 +340,6 @@ mod tests {
338340
fn validate_cloning_data_points() {
339341
let data_type = DataPoint {
340342
attributes: vec![KeyValue::new("key", "value")],
341-
start_time: std::time::SystemTime::now(),
342-
time: std::time::SystemTime::now(),
343343
value: 0u32,
344344
exemplars: vec![Exemplar {
345345
filtered_attributes: vec![],

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

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ use std::{marker, sync::Arc};
22

33
use opentelemetry::KeyValue;
44

5-
use crate::metrics::{
6-
data::{Aggregation, Gauge},
7-
Temporality,
8-
};
5+
use crate::metrics::{data::Aggregation, Temporality};
96

107
use super::{
118
exponential_histogram::ExpoHistogram, histogram::Histogram, last_value::LastValue,
@@ -99,31 +96,15 @@ impl<T: Number> AggregateBuilder<T> {
9996

10097
/// Builds a last-value aggregate function input and output.
10198
pub(crate) fn last_value(&self) -> (impl Measure<T>, impl ComputeAggregation) {
102-
let lv_filter = Arc::new(LastValue::new());
103-
let lv_agg = Arc::clone(&lv_filter);
99+
let lv = Arc::new(LastValue::new());
100+
let agg_lv = Arc::clone(&lv);
104101
let t = self.temporality;
105102

106103
(
107-
self.filter(move |n, a: &[KeyValue]| lv_filter.measure(n, a)),
108-
move |dest: Option<&mut dyn Aggregation>| {
109-
let g = dest.and_then(|d| d.as_mut().downcast_mut::<Gauge<T>>());
110-
let mut new_agg = if g.is_none() {
111-
Some(Gauge {
112-
data_points: vec![],
113-
})
114-
} else {
115-
None
116-
};
117-
let g = g.unwrap_or_else(|| new_agg.as_mut().expect("present if g is none"));
118-
119-
match t {
120-
Some(Temporality::Delta) => {
121-
lv_agg.compute_aggregation_delta(&mut g.data_points)
122-
}
123-
_ => lv_agg.compute_aggregation_cumulative(&mut g.data_points),
124-
}
125-
126-
(g.data_points.len(), new_agg.map(|a| Box::new(a) as Box<_>))
104+
self.filter(move |n, a: &[KeyValue]| lv.measure(n, a)),
105+
move |dest: Option<&mut dyn Aggregation>| match t {
106+
Some(Temporality::Delta) => agg_lv.delta(dest),
107+
_ => agg_lv.cumulative(dest),
127108
},
128109
)
129110
}
@@ -211,7 +192,7 @@ impl<T: Number> AggregateBuilder<T> {
211192
#[cfg(test)]
212193
mod tests {
213194
use crate::metrics::data::{
214-
DataPoint, ExponentialBucket, ExponentialHistogram, ExponentialHistogramDataPoint,
195+
DataPoint, ExponentialBucket, ExponentialHistogram, ExponentialHistogramDataPoint, Gauge,
215196
Histogram, HistogramDataPoint, Sum,
216197
};
217198
use std::{time::SystemTime, vec};
@@ -224,11 +205,11 @@ mod tests {
224205
let mut a = Gauge {
225206
data_points: vec![DataPoint {
226207
attributes: vec![KeyValue::new("a", 1)],
227-
start_time: SystemTime::now(),
228-
time: SystemTime::now(),
229208
value: 1u64,
230209
exemplars: vec![],
231210
}],
211+
start_time: SystemTime::now(),
212+
time: SystemTime::now(),
232213
};
233214
let new_attributes = [KeyValue::new("b", 2)];
234215
measure.call(2, &new_attributes[..]);
@@ -251,19 +232,17 @@ mod tests {
251232
data_points: vec![
252233
DataPoint {
253234
attributes: vec![KeyValue::new("a1", 1)],
254-
start_time: SystemTime::now(),
255-
time: SystemTime::now(),
256235
value: 1u64,
257236
exemplars: vec![],
258237
},
259238
DataPoint {
260239
attributes: vec![KeyValue::new("a2", 1)],
261-
start_time: SystemTime::now(),
262-
time: SystemTime::now(),
263240
value: 2u64,
264241
exemplars: vec![],
265242
},
266243
],
244+
start_time: SystemTime::now(),
245+
time: SystemTime::now(),
267246
temporality: if temporality == Temporality::Delta {
268247
Temporality::Cumulative
269248
} else {
@@ -294,19 +273,17 @@ mod tests {
294273
data_points: vec![
295274
DataPoint {
296275
attributes: vec![KeyValue::new("a1", 1)],
297-
start_time: SystemTime::now(),
298-
time: SystemTime::now(),
299276
value: 1u64,
300277
exemplars: vec![],
301278
},
302279
DataPoint {
303280
attributes: vec![KeyValue::new("a2", 1)],
304-
start_time: SystemTime::now(),
305-
time: SystemTime::now(),
306281
value: 2u64,
307282
exemplars: vec![],
308283
},
309284
],
285+
start_time: SystemTime::now(),
286+
time: SystemTime::now(),
310287
temporality: if temporality == Temporality::Delta {
311288
Temporality::Cumulative
312289
} else {

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

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,15 +1440,14 @@ mod tests {
14401440
count = out_fn.call(Some(got.as_mut())).0
14411441
}
14421442

1443-
assert_aggregation_eq::<T>(Box::new(test.want), got, true, test.name);
1443+
assert_aggregation_eq::<T>(Box::new(test.want), got, test.name);
14441444
assert_eq!(test.want_count, count, "{}", test.name);
14451445
}
14461446
}
14471447

14481448
fn assert_aggregation_eq<T: Number + PartialEq>(
14491449
a: Box<dyn Aggregation>,
14501450
b: Box<dyn Aggregation>,
1451-
ignore_timestamp: bool,
14521451
test_name: &'static str,
14531452
) {
14541453
assert_eq!(
@@ -1467,13 +1466,7 @@ mod tests {
14671466
test_name
14681467
);
14691468
for (a, b) in a.data_points.iter().zip(b.data_points.iter()) {
1470-
assert_data_points_eq(
1471-
a,
1472-
b,
1473-
ignore_timestamp,
1474-
"mismatching gauge data points",
1475-
test_name,
1476-
);
1469+
assert_data_points_eq(a, b, "mismatching gauge data points", test_name);
14771470
}
14781471
} else if let Some(a) = a.as_any().downcast_ref::<data::Sum<T>>() {
14791472
let b = b.as_any().downcast_ref::<data::Sum<T>>().unwrap();
@@ -1494,13 +1487,7 @@ mod tests {
14941487
test_name
14951488
);
14961489
for (a, b) in a.data_points.iter().zip(b.data_points.iter()) {
1497-
assert_data_points_eq(
1498-
a,
1499-
b,
1500-
ignore_timestamp,
1501-
"mismatching sum data points",
1502-
test_name,
1503-
);
1490+
assert_data_points_eq(a, b, "mismatching sum data points", test_name);
15041491
}
15051492
} else if let Some(a) = a.as_any().downcast_ref::<data::Histogram<T>>() {
15061493
let b = b.as_any().downcast_ref::<data::Histogram<T>>().unwrap();
@@ -1516,13 +1503,7 @@ mod tests {
15161503
test_name
15171504
);
15181505
for (a, b) in a.data_points.iter().zip(b.data_points.iter()) {
1519-
assert_hist_data_points_eq(
1520-
a,
1521-
b,
1522-
ignore_timestamp,
1523-
"mismatching hist data points",
1524-
test_name,
1525-
);
1506+
assert_hist_data_points_eq(a, b, "mismatching hist data points", test_name);
15261507
}
15271508
} else if let Some(a) = a.as_any().downcast_ref::<data::ExponentialHistogram<T>>() {
15281509
let b = b
@@ -1544,7 +1525,6 @@ mod tests {
15441525
assert_exponential_hist_data_points_eq(
15451526
a,
15461527
b,
1547-
ignore_timestamp,
15481528
"mismatching hist data points",
15491529
test_name,
15501530
);
@@ -1557,7 +1537,6 @@ mod tests {
15571537
fn assert_data_points_eq<T: Number>(
15581538
a: &data::DataPoint<T>,
15591539
b: &data::DataPoint<T>,
1560-
ignore_timestamp: bool,
15611540
message: &'static str,
15621541
test_name: &'static str,
15631542
) {
@@ -1567,21 +1546,11 @@ mod tests {
15671546
test_name, message
15681547
);
15691548
assert_eq!(a.value, b.value, "{}: {} value", test_name, message);
1570-
1571-
if !ignore_timestamp {
1572-
assert_eq!(
1573-
a.start_time, b.start_time,
1574-
"{}: {} start time",
1575-
test_name, message
1576-
);
1577-
assert_eq!(a.time, b.time, "{}: {} time", test_name, message);
1578-
}
15791549
}
15801550

15811551
fn assert_hist_data_points_eq<T: Number>(
15821552
a: &data::HistogramDataPoint<T>,
15831553
b: &data::HistogramDataPoint<T>,
1584-
ignore_timestamp: bool,
15851554
message: &'static str,
15861555
test_name: &'static str,
15871556
) {
@@ -1600,21 +1569,11 @@ mod tests {
16001569
assert_eq!(a.min, b.min, "{}: {} min", test_name, message);
16011570
assert_eq!(a.max, b.max, "{}: {} max", test_name, message);
16021571
assert_eq!(a.sum, b.sum, "{}: {} sum", test_name, message);
1603-
1604-
if !ignore_timestamp {
1605-
assert_eq!(
1606-
a.start_time, b.start_time,
1607-
"{}: {} start time",
1608-
test_name, message
1609-
);
1610-
assert_eq!(a.time, b.time, "{}: {} time", test_name, message);
1611-
}
16121572
}
16131573

16141574
fn assert_exponential_hist_data_points_eq<T: Number>(
16151575
a: &data::ExponentialHistogramDataPoint<T>,
16161576
b: &data::ExponentialHistogramDataPoint<T>,
1617-
ignore_timestamp: bool,
16181577
message: &'static str,
16191578
test_name: &'static str,
16201579
) {
@@ -1645,14 +1604,5 @@ mod tests {
16451604
"{}: {} neg",
16461605
test_name, message
16471606
);
1648-
1649-
if !ignore_timestamp {
1650-
assert_eq!(
1651-
a.start_time, b.start_time,
1652-
"{}: {} start time",
1653-
test_name, message
1654-
);
1655-
assert_eq!(a.time, b.time, "{}: {} time", test_name, message);
1656-
}
16571607
}
16581608
}

0 commit comments

Comments
 (0)