diff --git a/opentelemetry-proto/src/transform/metrics.rs b/opentelemetry-proto/src/transform/metrics.rs index 391cb031d9..a9370d3d80 100644 --- a/opentelemetry-proto/src/transform/metrics.rs +++ b/opentelemetry-proto/src/transform/metrics.rs @@ -215,19 +215,19 @@ pub mod tonic { .data_points() .map(|dp| TonicHistogramDataPoint { attributes: dp.attributes().map(Into::into).collect(), - start_time_unix_nano: to_nanos(hist.start_time), - time_unix_nano: to_nanos(hist.time), - count: dp.count, - sum: Some(dp.sum.into_f64()), + start_time_unix_nano: to_nanos(hist.start_time()), + time_unix_nano: to_nanos(hist.time()), + count: dp.count(), + sum: Some(dp.sum().into_f64()), bucket_counts: dp.bucket_counts().collect(), explicit_bounds: dp.bounds().collect(), exemplars: dp.exemplars().map(Into::into).collect(), flags: TonicDataPointFlags::default() as u32, - min: dp.min.map(Numeric::into_f64), - max: dp.max.map(Numeric::into_f64), + min: dp.min().map(Numeric::into_f64), + max: dp.max().map(Numeric::into_f64), }) .collect(), - aggregation_temporality: TonicTemporality::from(hist.temporality).into(), + aggregation_temporality: TonicTemporality::from(hist.temporality()).into(), } } } @@ -242,28 +242,28 @@ pub mod tonic { .data_points() .map(|dp| TonicExponentialHistogramDataPoint { attributes: dp.attributes().map(Into::into).collect(), - start_time_unix_nano: to_nanos(hist.start_time), - time_unix_nano: to_nanos(hist.time), - count: dp.count as u64, - sum: Some(dp.sum.into_f64()), - scale: dp.scale.into(), - zero_count: dp.zero_count, + start_time_unix_nano: to_nanos(hist.start_time()), + time_unix_nano: to_nanos(hist.time()), + count: dp.count() as u64, + sum: Some(dp.sum().into_f64()), + scale: dp.scale().into(), + zero_count: dp.zero_count(), positive: Some(TonicBuckets { - offset: dp.positive_bucket.offset, - bucket_counts: dp.positive_bucket.counts.clone(), + offset: dp.positive_bucket().offset(), + bucket_counts: dp.positive_bucket().counts().collect(), }), negative: Some(TonicBuckets { - offset: dp.negative_bucket.offset, - bucket_counts: dp.negative_bucket.counts.clone(), + offset: dp.negative_bucket().offset(), + bucket_counts: dp.negative_bucket().counts().collect(), }), flags: TonicDataPointFlags::default() as u32, exemplars: dp.exemplars().map(Into::into).collect(), - min: dp.min.map(Numeric::into_f64), - max: dp.max.map(Numeric::into_f64), - zero_threshold: dp.zero_threshold, + min: dp.min().map(Numeric::into_f64), + max: dp.max().map(Numeric::into_f64), + zero_threshold: dp.zero_threshold(), }) .collect(), - aggregation_temporality: TonicTemporality::from(hist.temporality).into(), + aggregation_temporality: TonicTemporality::from(hist.temporality()).into(), } } } @@ -278,15 +278,15 @@ pub mod tonic { .data_points() .map(|dp| TonicNumberDataPoint { attributes: dp.attributes().map(Into::into).collect(), - start_time_unix_nano: to_nanos(sum.start_time), - time_unix_nano: to_nanos(sum.time), + start_time_unix_nano: to_nanos(sum.start_time()), + time_unix_nano: to_nanos(sum.time()), exemplars: dp.exemplars().map(Into::into).collect(), flags: TonicDataPointFlags::default() as u32, - value: Some(dp.value.into()), + value: Some(dp.value().into()), }) .collect(), - aggregation_temporality: TonicTemporality::from(sum.temporality).into(), - is_monotonic: sum.is_monotonic, + aggregation_temporality: TonicTemporality::from(sum.temporality()).into(), + is_monotonic: sum.is_monotonic(), } } } @@ -301,11 +301,11 @@ pub mod tonic { .data_points() .map(|dp| TonicNumberDataPoint { attributes: dp.attributes().map(Into::into).collect(), - start_time_unix_nano: gauge.start_time.map(to_nanos).unwrap_or_default(), - time_unix_nano: to_nanos(gauge.time), + start_time_unix_nano: gauge.start_time().map(to_nanos).unwrap_or_default(), + time_unix_nano: to_nanos(gauge.time()), exemplars: dp.exemplars().map(Into::into).collect(), flags: TonicDataPointFlags::default() as u32, - value: Some(dp.value.into()), + value: Some(dp.value().into()), }) .collect(), } @@ -322,9 +322,9 @@ pub mod tonic { .filtered_attributes() .map(|kv| (&kv.key, &kv.value).into()) .collect(), - time_unix_nano: to_nanos(ex.time), - span_id: ex.span_id.into(), - trace_id: ex.trace_id.into(), + time_unix_nano: to_nanos(ex.time()), + span_id: ex.span_id().into(), + trace_id: ex.trace_id().into(), value: Some(ex.value.into()), } } diff --git a/opentelemetry-sdk/src/metrics/data/mod.rs b/opentelemetry-sdk/src/metrics/data/mod.rs index 10899cfed6..9c63c9ada8 100644 --- a/opentelemetry-sdk/src/metrics/data/mod.rs +++ b/opentelemetry-sdk/src/metrics/data/mod.rs @@ -169,7 +169,7 @@ pub struct GaugeDataPoint { /// time series. pub(crate) attributes: Vec, /// The value of this data point. - pub value: T, + pub(crate) value: T, /// The sampled [Exemplar]s collected during the time series. pub(crate) exemplars: Vec>, } @@ -186,15 +186,22 @@ impl GaugeDataPoint { } } +impl GaugeDataPoint { + /// Returns the value of this data point. + pub fn value(&self) -> T { + self.value + } +} + /// A measurement of the current value of an instrument. #[derive(Debug, Clone)] pub struct Gauge { /// Represents individual aggregated measurements with unique attributes. pub(crate) data_points: Vec>, /// The time when the time series was started. - pub start_time: Option, + pub(crate) start_time: Option, /// The time when the time series was recorded. - pub time: SystemTime, + pub(crate) time: SystemTime, } impl Gauge { @@ -202,6 +209,16 @@ impl Gauge { pub fn data_points(&self) -> impl Iterator> { self.data_points.iter() } + + /// Returns the time when the time series was started. + pub fn start_time(&self) -> Option { + self.start_time + } + + /// Returns the time when the time series was recorded. + pub fn time(&self) -> SystemTime { + self.time + } } /// DataPoint is a single data point in a time series. @@ -211,7 +228,7 @@ pub struct SumDataPoint { /// time series. pub(crate) attributes: Vec, /// The value of this data point. - pub value: T, + pub(crate) value: T, /// The sampled [Exemplar]s collected during the time series. pub(crate) exemplars: Vec>, } @@ -228,20 +245,27 @@ impl SumDataPoint { } } +impl SumDataPoint { + /// Returns the value of this data point. + pub fn value(&self) -> T { + self.value + } +} + /// Represents the sum of all measurements of values from an instrument. #[derive(Debug, Clone)] pub struct Sum { /// Represents individual aggregated measurements with unique attributes. pub(crate) data_points: Vec>, /// The time when the time series was started. - pub start_time: SystemTime, + pub(crate) start_time: SystemTime, /// The time when the time series was recorded. - pub time: SystemTime, + pub(crate) time: SystemTime, /// Describes if the aggregation is reported as the change from the last report /// time, or the cumulative changes since a fixed start time. - pub temporality: Temporality, + pub(crate) temporality: Temporality, /// Whether this aggregation only increases or decreases. - pub is_monotonic: bool, + pub(crate) is_monotonic: bool, } impl Sum { @@ -249,6 +273,27 @@ impl Sum { pub fn data_points(&self) -> impl Iterator> { self.data_points.iter() } + + /// Returns the time when the time series was started. + pub fn start_time(&self) -> SystemTime { + self.start_time + } + + /// Returns the time when the time series was recorded. + pub fn time(&self) -> SystemTime { + self.time + } + + /// Returns the temporality describing if the aggregation is reported as the change + /// from the last report time, or the cumulative changes since a fixed start time. + pub fn temporality(&self) -> Temporality { + self.temporality + } + + /// Returns whether this aggregation only increases or decreases. + pub fn is_monotonic(&self) -> bool { + self.is_monotonic + } } /// Represents the histogram of all measurements of values from an instrument. @@ -257,12 +302,12 @@ pub struct Histogram { /// Individual aggregated measurements with unique attributes. pub(crate) data_points: Vec>, /// The time when the time series was started. - pub start_time: SystemTime, + pub(crate) start_time: SystemTime, /// The time when the time series was recorded. - pub time: SystemTime, + pub(crate) time: SystemTime, /// Describes if the aggregation is reported as the change from the last report /// time, or the cumulative changes since a fixed start time. - pub temporality: Temporality, + pub(crate) temporality: Temporality, } impl Histogram { @@ -270,6 +315,22 @@ impl Histogram { pub fn data_points(&self) -> impl Iterator> { self.data_points.iter() } + + /// Returns the time when the time series was started. + pub fn start_time(&self) -> SystemTime { + self.start_time + } + + /// Returns the time when the time series was recorded. + pub fn time(&self) -> SystemTime { + self.time + } + + /// Returns the temporality describing if the aggregation is reported as the change + /// from the last report time, or the cumulative changes since a fixed start time. + pub fn temporality(&self) -> Temporality { + self.temporality + } } /// A single histogram data point in a time series. @@ -278,7 +339,7 @@ pub struct HistogramDataPoint { /// The set of key value pairs that uniquely identify the time series. pub(crate) attributes: Vec, /// The number of updates this histogram has been calculated with. - pub count: u64, + pub(crate) count: u64, /// The upper bounds of the buckets of the histogram. /// /// Because the last boundary is +infinity this one is implied. @@ -287,11 +348,11 @@ pub struct HistogramDataPoint { pub(crate) bucket_counts: Vec, /// The minimum value recorded. - pub min: Option, + pub(crate) min: Option, /// The maximum value recorded. - pub max: Option, + pub(crate) max: Option, /// The sum of the values recorded. - pub sum: T, + pub(crate) sum: T, /// The sampled [Exemplar]s collected during the time series. pub(crate) exemplars: Vec>, @@ -317,6 +378,28 @@ impl HistogramDataPoint { pub fn bucket_counts(&self) -> impl Iterator + '_ { self.bucket_counts.iter().copied() } + + /// Returns the number of updates this histogram has been calculated with. + pub fn count(&self) -> u64 { + self.count + } +} + +impl HistogramDataPoint { + /// Returns the minimum value recorded. + pub fn min(&self) -> Option { + self.min + } + + /// Returns the maximum value recorded. + pub fn max(&self) -> Option { + self.max + } + + /// Returns the sum of the values recorded. + pub fn sum(&self) -> T { + self.sum + } } /// The histogram of all measurements of values from an instrument. @@ -325,12 +408,12 @@ pub struct ExponentialHistogram { /// The individual aggregated measurements with unique attributes. pub(crate) data_points: Vec>, /// When the time series was started. - pub start_time: SystemTime, + pub(crate) start_time: SystemTime, /// The time when the time series was recorded. - pub time: SystemTime, + pub(crate) time: SystemTime, /// Describes if the aggregation is reported as the change from the last report /// time, or the cumulative changes since a fixed start time. - pub temporality: Temporality, + pub(crate) temporality: Temporality, } impl ExponentialHistogram { @@ -338,6 +421,22 @@ impl ExponentialHistogram { pub fn data_points(&self) -> impl Iterator> { self.data_points.iter() } + + /// Returns the time when the time series was started. + pub fn start_time(&self) -> SystemTime { + self.start_time + } + + /// Returns the time when the time series was recorded. + pub fn time(&self) -> SystemTime { + self.time + } + + /// Returns the temporality describing if the aggregation is reported as the change + /// from the last report time, or the cumulative changes since a fixed start time. + pub fn temporality(&self) -> Temporality { + self.temporality + } } /// A single exponential histogram data point in a time series. @@ -347,20 +446,20 @@ pub struct ExponentialHistogramDataPoint { pub(crate) attributes: Vec, /// The number of updates this histogram has been calculated with. - pub count: usize, + pub(crate) count: usize, /// The minimum value recorded. - pub min: Option, + pub(crate) min: Option, /// The maximum value recorded. - pub max: Option, + pub(crate) max: Option, /// The sum of the values recorded. - pub sum: T, + pub(crate) sum: T, /// Describes the resolution of the histogram. /// /// Boundaries are located at powers of the base, where: /// /// base = 2 ^ (2 ^ -scale) - pub scale: i8, + pub(crate) scale: i8, /// The number of values whose absolute value is less than or equal to /// `zero_threshold`. @@ -368,18 +467,18 @@ pub struct ExponentialHistogramDataPoint { /// When `zero_threshold` is `0`, this is the number of values that cannot be /// expressed using the standard exponential formula as well as values that have /// been rounded to zero. - pub zero_count: u64, + pub(crate) zero_count: u64, /// The range of positive value bucket counts. - pub positive_bucket: ExponentialBucket, + pub(crate) positive_bucket: ExponentialBucket, /// The range of negative value bucket counts. - pub negative_bucket: ExponentialBucket, + pub(crate) negative_bucket: ExponentialBucket, /// The width of the zero region. /// /// Where the zero region is defined as the closed interval /// [-zero_threshold, zero_threshold]. - pub zero_threshold: f64, + pub(crate) zero_threshold: f64, /// The sampled exemplars collected during the time series. pub(crate) exemplars: Vec>, @@ -395,19 +494,78 @@ impl ExponentialHistogramDataPoint { pub fn exemplars(&self) -> impl Iterator> { self.exemplars.iter() } + + /// Returns the number of updates this histogram has been calculated with. + pub fn count(&self) -> usize { + self.count + } + + /// Returns the resolution of the histogram. + pub fn scale(&self) -> i8 { + self.scale + } + + /// Returns the number of values whose absolute value is less than or equal to zero_threshold. + pub fn zero_count(&self) -> u64 { + self.zero_count + } + + /// Returns the range of positive value bucket counts. + pub fn positive_bucket(&self) -> &ExponentialBucket { + &self.positive_bucket + } + + /// Returns the range of negative value bucket counts. + pub fn negative_bucket(&self) -> &ExponentialBucket { + &self.negative_bucket + } + + /// Returns the width of the zero region. + pub fn zero_threshold(&self) -> f64 { + self.zero_threshold + } +} + +impl ExponentialHistogramDataPoint { + /// Returns the minimum value recorded. + pub fn min(&self) -> Option { + self.min + } + + /// Returns the maximum value recorded. + pub fn max(&self) -> Option { + self.max + } + + /// Returns the sum of the values recorded. + pub fn sum(&self) -> T { + self.sum + } } /// A set of bucket counts, encoded in a contiguous array of counts. #[derive(Debug, Clone, PartialEq)] pub struct ExponentialBucket { /// The bucket index of the first entry in the `counts` vec. - pub offset: i32, + pub(crate) offset: i32, /// A vec where `counts[i]` carries the count of the bucket at index `offset + i`. /// /// `counts[i]` is the count of values greater than base^(offset+i) and less than /// or equal to base^(offset+i+1). - pub counts: Vec, + pub(crate) counts: Vec, +} + +impl ExponentialBucket { + /// Returns the bucket index of the first entry in the counts vec. + pub fn offset(&self) -> i32 { + self.offset + } + + /// Returns an iterator over the counts. + pub fn counts(&self) -> impl Iterator + '_ { + self.counts.iter().copied() + } } /// A measurement sampled from a time series providing a typical example. @@ -417,17 +575,17 @@ pub struct Exemplar { /// time series' aggregated data. pub(crate) filtered_attributes: Vec, /// The time when the measurement was recorded. - pub time: SystemTime, + pub(crate) time: SystemTime, /// The measured value. pub value: T, /// The ID of the span that was active during the measurement. /// /// If no span was active or the span was not sampled this will be empty. - pub span_id: [u8; 8], + pub(crate) span_id: [u8; 8], /// The ID of the trace the active span belonged to during the measurement. /// /// If no span was active or the span was not sampled this will be empty. - pub trace_id: [u8; 16], + pub(crate) trace_id: [u8; 16], } impl Exemplar { @@ -435,6 +593,21 @@ impl Exemplar { pub fn filtered_attributes(&self) -> impl Iterator { self.filtered_attributes.iter() } + + /// Returns the time when the measurement was recorded. + pub fn time(&self) -> SystemTime { + self.time + } + + /// Returns the ID of the span that was active during the measurement. + pub fn span_id(&self) -> &[u8; 8] { + &self.span_id + } + + /// Returns the ID of the trace the active span belonged to during the measurement. + pub fn trace_id(&self) -> &[u8; 16] { + &self.trace_id + } } #[cfg(test)] diff --git a/opentelemetry-stdout/src/metrics/exporter.rs b/opentelemetry-stdout/src/metrics/exporter.rs index 995d883e45..ad0df9cdf1 100644 --- a/opentelemetry-stdout/src/metrics/exporter.rs +++ b/opentelemetry-stdout/src/metrics/exporter.rs @@ -105,7 +105,7 @@ fn print_metrics<'a>(metrics: impl Iterator) { fn print_info(data: &MetricData) where - T: Debug, + T: Debug + Copy, { match data { MetricData::Gauge(gauge) => { @@ -135,20 +135,20 @@ fn print_metrics<'a>(metrics: impl Iterator) { } } -fn print_sum(sum: &Sum) { +fn print_sum(sum: &Sum) { println!("\t\tSum DataPoints"); - println!("\t\tMonotonic : {}", sum.is_monotonic); - if sum.temporality == Temporality::Cumulative { + println!("\t\tMonotonic : {}", sum.is_monotonic()); + if sum.temporality() == Temporality::Cumulative { println!("\t\tTemporality : Cumulative"); } else { println!("\t\tTemporality : Delta"); } - let datetime: DateTime = sum.start_time.into(); + let datetime: DateTime = sum.start_time().into(); println!( "\t\tStartTime : {}", datetime.format("%Y-%m-%d %H:%M:%S%.6f") ); - let datetime: DateTime = sum.time.into(); + let datetime: DateTime = sum.time().into(); println!( "\t\tEndTime : {}", datetime.format("%Y-%m-%d %H:%M:%S%.6f") @@ -156,16 +156,16 @@ fn print_sum(sum: &Sum) { print_sum_data_points(sum.data_points()); } -fn print_gauge(gauge: &Gauge) { +fn print_gauge(gauge: &Gauge) { println!("\t\tGauge DataPoints"); - if let Some(start_time) = gauge.start_time { + if let Some(start_time) = gauge.start_time() { let datetime: DateTime = start_time.into(); println!( "\t\tStartTime : {}", datetime.format("%Y-%m-%d %H:%M:%S%.6f") ); } - let datetime: DateTime = gauge.time.into(); + let datetime: DateTime = gauge.time().into(); println!( "\t\tEndTime : {}", datetime.format("%Y-%m-%d %H:%M:%S%.6f") @@ -173,18 +173,18 @@ fn print_gauge(gauge: &Gauge) { print_gauge_data_points(gauge.data_points()); } -fn print_histogram(histogram: &Histogram) { - if histogram.temporality == Temporality::Cumulative { +fn print_histogram(histogram: &Histogram) { + if histogram.temporality() == Temporality::Cumulative { println!("\t\tTemporality : Cumulative"); } else { println!("\t\tTemporality : Delta"); } - let datetime: DateTime = histogram.start_time.into(); + let datetime: DateTime = histogram.start_time().into(); println!( "\t\tStartTime : {}", datetime.format("%Y-%m-%d %H:%M:%S%.6f") ); - let datetime: DateTime = histogram.time.into(); + let datetime: DateTime = histogram.time().into(); println!( "\t\tEndTime : {}", datetime.format("%Y-%m-%d %H:%M:%S%.6f") @@ -193,12 +193,12 @@ fn print_histogram(histogram: &Histogram) { print_hist_data_points(histogram.data_points()); } -fn print_sum_data_points<'a, T: Debug + 'a>( +fn print_sum_data_points<'a, T: Debug + Copy + 'a>( data_points: impl Iterator>, ) { for (i, data_point) in data_points.enumerate() { println!("\t\tDataPoint #{}", i); - println!("\t\t\tValue : {:#?}", data_point.value); + println!("\t\t\tValue : {:#?}", data_point.value()); println!("\t\t\tAttributes :"); for kv in data_point.attributes() { println!("\t\t\t\t -> {}: {}", kv.key, kv.value.as_str()); @@ -206,12 +206,12 @@ fn print_sum_data_points<'a, T: Debug + 'a>( } } -fn print_gauge_data_points<'a, T: Debug + 'a>( +fn print_gauge_data_points<'a, T: Debug + Copy + 'a>( data_points: impl Iterator>, ) { for (i, data_point) in data_points.enumerate() { println!("\t\tDataPoint #{}", i); - println!("\t\t\tValue : {:#?}", data_point.value); + println!("\t\t\tValue : {:#?}", data_point.value()); println!("\t\t\tAttributes :"); for kv in data_point.attributes() { println!("\t\t\t\t -> {}: {}", kv.key, kv.value.as_str()); @@ -219,18 +219,18 @@ fn print_gauge_data_points<'a, T: Debug + 'a>( } } -fn print_hist_data_points<'a, T: Debug + 'a>( +fn print_hist_data_points<'a, T: Debug + Copy + 'a>( data_points: impl Iterator>, ) { for (i, data_point) in data_points.enumerate() { println!("\t\tDataPoint #{}", i); - println!("\t\t\tCount : {}", data_point.count); - println!("\t\t\tSum : {:?}", data_point.sum); - if let Some(min) = &data_point.min { + println!("\t\t\tCount : {}", data_point.count()); + println!("\t\t\tSum : {:?}", data_point.sum()); + if let Some(min) = &data_point.min() { println!("\t\t\tMin : {:?}", min); } - if let Some(max) = &data_point.max { + if let Some(max) = &data_point.max() { println!("\t\t\tMax : {:?}", max); }