Skip to content

Commit 902baa9

Browse files
frailltcijothomas
andauthored
Gauge start-time is optional (#2389)
Co-authored-by: Cijo Thomas <[email protected]>
1 parent de3ea4e commit 902baa9

File tree

9 files changed

+217
-104
lines changed

9 files changed

+217
-104
lines changed

opentelemetry-proto/src/transform/metrics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ 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),
322+
start_time_unix_nano: dp.start_time.map(to_nanos).unwrap_or_default(),
323323
time_unix_nano: to_nanos(dp.time),
324324
exemplars: dp.exemplars.iter().map(Into::into).collect(),
325325
flags: TonicDataPointFlags::default() as u32,

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

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,35 +53,42 @@ pub trait Aggregation: fmt::Debug + any::Any + Send + Sync {
5353
fn as_mut(&mut self) -> &mut dyn any::Any;
5454
}
5555

56-
/// A measurement of the current value of an instrument.
57-
#[derive(Debug)]
58-
pub struct Gauge<T> {
59-
/// Represents individual aggregated measurements with unique attributes.
60-
pub data_points: Vec<DataPoint<T>>,
56+
/// DataPoint is a single data point in a time series.
57+
#[derive(Debug, PartialEq)]
58+
pub struct GaugeDataPoint<T> {
59+
/// Attributes is the set of key value pairs that uniquely identify the
60+
/// time series.
61+
pub attributes: Vec<KeyValue>,
62+
/// The time when the time series was started.
63+
pub start_time: Option<SystemTime>,
64+
/// The time when the time series was recorded.
65+
pub time: SystemTime,
66+
/// The value of this data point.
67+
pub value: T,
68+
/// The sampled [Exemplar]s collected during the time series.
69+
pub exemplars: Vec<Exemplar<T>>,
6170
}
6271

63-
impl<T: fmt::Debug + Send + Sync + 'static> Aggregation for Gauge<T> {
64-
fn as_any(&self) -> &dyn any::Any {
65-
self
66-
}
67-
fn as_mut(&mut self) -> &mut dyn any::Any {
68-
self
72+
impl<T: Copy> Clone for GaugeDataPoint<T> {
73+
fn clone(&self) -> Self {
74+
Self {
75+
attributes: self.attributes.clone(),
76+
start_time: self.start_time,
77+
time: self.time,
78+
value: self.value,
79+
exemplars: self.exemplars.clone(),
80+
}
6981
}
7082
}
7183

72-
/// Represents the sum of all measurements of values from an instrument.
84+
/// A measurement of the current value of an instrument.
7385
#[derive(Debug)]
74-
pub struct Sum<T> {
86+
pub struct Gauge<T> {
7587
/// Represents individual aggregated measurements with unique attributes.
76-
pub data_points: Vec<DataPoint<T>>,
77-
/// Describes if the aggregation is reported as the change from the last report
78-
/// time, or the cumulative changes since a fixed start time.
79-
pub temporality: Temporality,
80-
/// Whether this aggregation only increases or decreases.
81-
pub is_monotonic: bool,
88+
pub data_points: Vec<GaugeDataPoint<T>>,
8289
}
8390

84-
impl<T: fmt::Debug + Send + Sync + 'static> Aggregation for Sum<T> {
91+
impl<T: fmt::Debug + Send + Sync + 'static> Aggregation for Gauge<T> {
8592
fn as_any(&self) -> &dyn any::Any {
8693
self
8794
}
@@ -92,7 +99,7 @@ impl<T: fmt::Debug + Send + Sync + 'static> Aggregation for Sum<T> {
9299

93100
/// DataPoint is a single data point in a time series.
94101
#[derive(Debug, PartialEq)]
95-
pub struct DataPoint<T> {
102+
pub struct SumDataPoint<T> {
96103
/// Attributes is the set of key value pairs that uniquely identify the
97104
/// time series.
98105
pub attributes: Vec<KeyValue>,
@@ -106,7 +113,7 @@ pub struct DataPoint<T> {
106113
pub exemplars: Vec<Exemplar<T>>,
107114
}
108115

109-
impl<T: Copy> Clone for DataPoint<T> {
116+
impl<T: Copy> Clone for SumDataPoint<T> {
110117
fn clone(&self) -> Self {
111118
Self {
112119
attributes: self.attributes.clone(),
@@ -118,6 +125,27 @@ impl<T: Copy> Clone for DataPoint<T> {
118125
}
119126
}
120127

128+
/// Represents the sum of all measurements of values from an instrument.
129+
#[derive(Debug)]
130+
pub struct Sum<T> {
131+
/// Represents individual aggregated measurements with unique attributes.
132+
pub data_points: Vec<SumDataPoint<T>>,
133+
/// Describes if the aggregation is reported as the change from the last report
134+
/// time, or the cumulative changes since a fixed start time.
135+
pub temporality: Temporality,
136+
/// Whether this aggregation only increases or decreases.
137+
pub is_monotonic: bool,
138+
}
139+
140+
impl<T: fmt::Debug + Send + Sync + 'static> Aggregation for Sum<T> {
141+
fn as_any(&self) -> &dyn any::Any {
142+
self
143+
}
144+
fn as_mut(&mut self) -> &mut dyn any::Any {
145+
self
146+
}
147+
}
148+
121149
/// Represents the histogram of all measurements of values from an instrument.
122150
#[derive(Debug)]
123151
pub struct Histogram<T> {
@@ -330,13 +358,13 @@ impl<T: Copy> Clone for Exemplar<T> {
330358
#[cfg(test)]
331359
mod tests {
332360

333-
use super::{DataPoint, Exemplar, ExponentialHistogramDataPoint, HistogramDataPoint};
361+
use super::{Exemplar, ExponentialHistogramDataPoint, HistogramDataPoint, SumDataPoint};
334362

335363
use opentelemetry::KeyValue;
336364

337365
#[test]
338366
fn validate_cloning_data_points() {
339-
let data_type = DataPoint {
367+
let data_type = SumDataPoint {
340368
attributes: vec![KeyValue::new("key", "value")],
341369
start_time: std::time::SystemTime::now(),
342370
time: std::time::SystemTime::now(),

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ impl<T: Number> AggregateBuilder<T> {
211211
#[cfg(test)]
212212
mod tests {
213213
use crate::metrics::data::{
214-
DataPoint, ExponentialBucket, ExponentialHistogram, ExponentialHistogramDataPoint,
215-
Histogram, HistogramDataPoint, Sum,
214+
ExponentialBucket, ExponentialHistogram, ExponentialHistogramDataPoint, GaugeDataPoint,
215+
Histogram, HistogramDataPoint, Sum, SumDataPoint,
216216
};
217217
use std::{time::SystemTime, vec};
218218

@@ -222,9 +222,9 @@ mod tests {
222222
fn last_value_aggregation() {
223223
let (measure, agg) = AggregateBuilder::<u64>::new(None, None).last_value();
224224
let mut a = Gauge {
225-
data_points: vec![DataPoint {
225+
data_points: vec![GaugeDataPoint {
226226
attributes: vec![KeyValue::new("a", 1)],
227-
start_time: SystemTime::now(),
227+
start_time: Some(SystemTime::now()),
228228
time: SystemTime::now(),
229229
value: 1u64,
230230
exemplars: vec![],
@@ -249,14 +249,14 @@ mod tests {
249249
AggregateBuilder::<u64>::new(Some(temporality), None).precomputed_sum(true);
250250
let mut a = Sum {
251251
data_points: vec![
252-
DataPoint {
252+
SumDataPoint {
253253
attributes: vec![KeyValue::new("a1", 1)],
254254
start_time: SystemTime::now(),
255255
time: SystemTime::now(),
256256
value: 1u64,
257257
exemplars: vec![],
258258
},
259-
DataPoint {
259+
SumDataPoint {
260260
attributes: vec![KeyValue::new("a2", 1)],
261261
start_time: SystemTime::now(),
262262
time: SystemTime::now(),
@@ -292,14 +292,14 @@ mod tests {
292292
let (measure, agg) = AggregateBuilder::<u64>::new(Some(temporality), None).sum(true);
293293
let mut a = Sum {
294294
data_points: vec![
295-
DataPoint {
295+
SumDataPoint {
296296
attributes: vec![KeyValue::new("a1", 1)],
297297
start_time: SystemTime::now(),
298298
time: SystemTime::now(),
299299
value: 1u64,
300300
exemplars: vec![],
301301
},
302-
DataPoint {
302+
SumDataPoint {
303303
attributes: vec![KeyValue::new("a2", 1)],
304304
start_time: SystemTime::now(),
305305
time: SystemTime::now(),

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,7 @@ mod tests {
14671467
test_name
14681468
);
14691469
for (a, b) in a.data_points.iter().zip(b.data_points.iter()) {
1470-
assert_data_points_eq(
1470+
assert_gauge_data_points_eq(
14711471
a,
14721472
b,
14731473
ignore_timestamp,
@@ -1494,7 +1494,7 @@ mod tests {
14941494
test_name
14951495
);
14961496
for (a, b) in a.data_points.iter().zip(b.data_points.iter()) {
1497-
assert_data_points_eq(
1497+
assert_sum_data_points_eq(
14981498
a,
14991499
b,
15001500
ignore_timestamp,
@@ -1554,9 +1554,33 @@ mod tests {
15541554
}
15551555
}
15561556

1557-
fn assert_data_points_eq<T: Number>(
1558-
a: &data::DataPoint<T>,
1559-
b: &data::DataPoint<T>,
1557+
fn assert_sum_data_points_eq<T: Number>(
1558+
a: &data::SumDataPoint<T>,
1559+
b: &data::SumDataPoint<T>,
1560+
ignore_timestamp: bool,
1561+
message: &'static str,
1562+
test_name: &'static str,
1563+
) {
1564+
assert_eq!(
1565+
a.attributes, b.attributes,
1566+
"{}: {} attributes",
1567+
test_name, message
1568+
);
1569+
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+
}
1579+
}
1580+
1581+
fn assert_gauge_data_points_eq<T: Number>(
1582+
a: &data::GaugeDataPoint<T>,
1583+
b: &data::GaugeDataPoint<T>,
15601584
ignore_timestamp: bool,
15611585
message: &'static str,
15621586
test_name: &'static str,

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{mem::replace, ops::DerefMut, sync::Mutex, time::SystemTime};
22

3-
use crate::metrics::data::DataPoint;
3+
use crate::metrics::data::GaugeDataPoint;
44
use opentelemetry::KeyValue;
55

66
use super::{Aggregator, AtomicTracker, AtomicallyUpdate, Number, ValueMap};
@@ -56,30 +56,30 @@ impl<T: Number> LastValue<T> {
5656
self.value_map.measure(measurement, attrs);
5757
}
5858

59-
pub(crate) fn compute_aggregation_delta(&self, dest: &mut Vec<DataPoint<T>>) {
59+
pub(crate) fn compute_aggregation_delta(&self, dest: &mut Vec<GaugeDataPoint<T>>) {
6060
let t = SystemTime::now();
6161
let prev_start = self
6262
.start
6363
.lock()
6464
.map(|mut start| replace(start.deref_mut(), t))
6565
.unwrap_or(t);
6666
self.value_map
67-
.collect_and_reset(dest, |attributes, aggr| DataPoint {
67+
.collect_and_reset(dest, |attributes, aggr| GaugeDataPoint {
6868
attributes,
69-
start_time: prev_start,
69+
start_time: Some(prev_start),
7070
time: t,
7171
value: aggr.value.get_value(),
7272
exemplars: vec![],
7373
});
7474
}
7575

76-
pub(crate) fn compute_aggregation_cumulative(&self, dest: &mut Vec<DataPoint<T>>) {
76+
pub(crate) fn compute_aggregation_cumulative(&self, dest: &mut Vec<GaugeDataPoint<T>>) {
7777
let t = SystemTime::now();
7878
let prev_start = self.start.lock().map(|start| *start).unwrap_or(t);
7979
self.value_map
80-
.collect_readonly(dest, |attributes, aggr| DataPoint {
80+
.collect_readonly(dest, |attributes, aggr| GaugeDataPoint {
8181
attributes,
82-
start_time: prev_start,
82+
start_time: Some(prev_start),
8383
time: t,
8484
value: aggr.value.get_value(),
8585
exemplars: vec![],

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use opentelemetry::KeyValue;
22

3-
use crate::metrics::data::{self, Aggregation, DataPoint};
3+
use crate::metrics::data::{self, Aggregation, SumDataPoint};
44
use crate::metrics::Temporality;
55

66
use super::{last_value::Assign, AtomicTracker, Number, ValueMap};
@@ -66,7 +66,7 @@ impl<T: Number> PrecomputedSum<T> {
6666
let value = aggr.value.get_value();
6767
new_reported.insert(attributes.clone(), value);
6868
let delta = value - *reported.get(&attributes).unwrap_or(&T::default());
69-
DataPoint {
69+
SumDataPoint {
7070
attributes,
7171
start_time: prev_start,
7272
time: t,
@@ -107,7 +107,7 @@ impl<T: Number> PrecomputedSum<T> {
107107
let prev_start = self.start.lock().map(|start| *start).unwrap_or(t);
108108

109109
self.value_map
110-
.collect_readonly(&mut s_data.data_points, |attributes, aggr| DataPoint {
110+
.collect_readonly(&mut s_data.data_points, |attributes, aggr| SumDataPoint {
111111
attributes,
112112
start_time: prev_start,
113113
time: t,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ops::DerefMut;
33
use std::vec;
44
use std::{sync::Mutex, time::SystemTime};
55

6-
use crate::metrics::data::{self, Aggregation, DataPoint};
6+
use crate::metrics::data::{self, Aggregation, SumDataPoint};
77
use crate::metrics::Temporality;
88
use opentelemetry::KeyValue;
99

@@ -93,7 +93,7 @@ impl<T: Number> Sum<T> {
9393
.map(|mut start| replace(start.deref_mut(), t))
9494
.unwrap_or(t);
9595
self.value_map
96-
.collect_and_reset(&mut s_data.data_points, |attributes, aggr| DataPoint {
96+
.collect_and_reset(&mut s_data.data_points, |attributes, aggr| SumDataPoint {
9797
attributes,
9898
start_time: prev_start,
9999
time: t,
@@ -130,7 +130,7 @@ impl<T: Number> Sum<T> {
130130
let prev_start = self.start.lock().map(|start| *start).unwrap_or(t);
131131

132132
self.value_map
133-
.collect_readonly(&mut s_data.data_points, |attributes, aggr| DataPoint {
133+
.collect_readonly(&mut s_data.data_points, |attributes, aggr| SumDataPoint {
134134
attributes,
135135
start_time: prev_start,
136136
time: t,

0 commit comments

Comments
 (0)