Skip to content

Commit 26b3320

Browse files
mattboddcijothomas
andauthored
Implement clone for ExponentialBucket and ExponentialHistogramDataPoint (#2126)
Co-authored-by: Cijo Thomas <[email protected]>
1 parent 1a4e245 commit 26b3320

File tree

1 file changed

+111
-4
lines changed
  • opentelemetry-sdk/src/metrics/data

1 file changed

+111
-4
lines changed

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

Lines changed: 111 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl<T: fmt::Debug + Send + Sync + 'static> Aggregation for Sum<T> {
9393
}
9494

9595
/// DataPoint is a single data point in a time series.
96-
#[derive(Debug)]
96+
#[derive(Debug, PartialEq)]
9797
pub struct DataPoint<T> {
9898
/// Attributes is the set of key value pairs that uniquely identify the
9999
/// time series.
@@ -140,7 +140,7 @@ impl<T: fmt::Debug + Send + Sync + 'static> Aggregation for Histogram<T> {
140140
}
141141

142142
/// A single histogram data point in a time series.
143-
#[derive(Debug)]
143+
#[derive(Debug, PartialEq)]
144144
pub struct HistogramDataPoint<T> {
145145
/// The set of key value pairs that uniquely identify the time series.
146146
pub attributes: Vec<KeyValue>,
@@ -207,7 +207,7 @@ impl<T: fmt::Debug + Send + Sync + 'static> Aggregation for ExponentialHistogram
207207
}
208208

209209
/// A single exponential histogram data point in a time series.
210-
#[derive(Debug)]
210+
#[derive(Debug, PartialEq)]
211211
pub struct ExponentialHistogramDataPoint<T> {
212212
/// The set of key value pairs that uniquely identify the time series.
213213
pub attributes: Vec<KeyValue>,
@@ -255,6 +255,26 @@ pub struct ExponentialHistogramDataPoint<T> {
255255
pub exemplars: Vec<Exemplar<T>>,
256256
}
257257

258+
impl<T: Copy> Clone for ExponentialHistogramDataPoint<T> {
259+
fn clone(&self) -> Self {
260+
Self {
261+
attributes: self.attributes.clone(),
262+
start_time: self.start_time,
263+
time: self.time,
264+
count: self.count,
265+
min: self.min,
266+
max: self.max,
267+
sum: self.sum,
268+
scale: self.scale,
269+
zero_count: self.zero_count,
270+
positive_bucket: self.positive_bucket.clone(),
271+
negative_bucket: self.negative_bucket.clone(),
272+
zero_threshold: self.zero_threshold,
273+
exemplars: self.exemplars.clone(),
274+
}
275+
}
276+
}
277+
258278
/// A set of bucket counts, encoded in a contiguous array of counts.
259279
#[derive(Debug, PartialEq)]
260280
pub struct ExponentialBucket {
@@ -268,8 +288,17 @@ pub struct ExponentialBucket {
268288
pub counts: Vec<u64>,
269289
}
270290

291+
impl Clone for ExponentialBucket {
292+
fn clone(&self) -> Self {
293+
Self {
294+
offset: self.offset,
295+
counts: self.counts.clone(),
296+
}
297+
}
298+
}
299+
271300
/// A measurement sampled from a time series providing a typical example.
272-
#[derive(Debug)]
301+
#[derive(Debug, PartialEq)]
273302
pub struct Exemplar<T> {
274303
/// The attributes recorded with the measurement but filtered out of the
275304
/// time series' aggregated data.
@@ -299,3 +328,81 @@ impl<T: Copy> Clone for Exemplar<T> {
299328
}
300329
}
301330
}
331+
332+
#[cfg(test)]
333+
mod tests {
334+
335+
use super::{DataPoint, Exemplar, ExponentialHistogramDataPoint, HistogramDataPoint};
336+
337+
use opentelemetry::KeyValue;
338+
339+
#[test]
340+
fn validate_cloning_data_points() {
341+
let data_type = DataPoint {
342+
attributes: vec![KeyValue::new("key", "value")],
343+
start_time: Some(std::time::SystemTime::now()),
344+
time: Some(std::time::SystemTime::now()),
345+
value: 0u32,
346+
exemplars: vec![Exemplar {
347+
filtered_attributes: vec![],
348+
time: std::time::SystemTime::now(),
349+
value: 0u32,
350+
span_id: [0; 8],
351+
trace_id: [0; 16],
352+
}],
353+
};
354+
assert_eq!(data_type.clone(), data_type);
355+
356+
let histogram_data_point = HistogramDataPoint {
357+
attributes: vec![KeyValue::new("key", "value")],
358+
start_time: std::time::SystemTime::now(),
359+
time: std::time::SystemTime::now(),
360+
count: 0,
361+
bounds: vec![],
362+
bucket_counts: vec![],
363+
min: None,
364+
max: None,
365+
sum: 0u32,
366+
exemplars: vec![Exemplar {
367+
filtered_attributes: vec![],
368+
time: std::time::SystemTime::now(),
369+
value: 0u32,
370+
span_id: [0; 8],
371+
trace_id: [0; 16],
372+
}],
373+
};
374+
assert_eq!(histogram_data_point.clone(), histogram_data_point);
375+
376+
let exponential_histogram_data_point = ExponentialHistogramDataPoint {
377+
attributes: vec![KeyValue::new("key", "value")],
378+
start_time: std::time::SystemTime::now(),
379+
time: std::time::SystemTime::now(),
380+
count: 0,
381+
min: None,
382+
max: None,
383+
sum: 0u32,
384+
scale: 0,
385+
zero_count: 0,
386+
positive_bucket: super::ExponentialBucket {
387+
offset: 0,
388+
counts: vec![],
389+
},
390+
negative_bucket: super::ExponentialBucket {
391+
offset: 0,
392+
counts: vec![],
393+
},
394+
zero_threshold: 0.0,
395+
exemplars: vec![Exemplar {
396+
filtered_attributes: vec![],
397+
time: std::time::SystemTime::now(),
398+
value: 0u32,
399+
span_id: [0; 8],
400+
trace_id: [0; 16],
401+
}],
402+
};
403+
assert_eq!(
404+
exponential_histogram_data_point.clone(),
405+
exponential_histogram_data_point
406+
);
407+
}
408+
}

0 commit comments

Comments
 (0)