Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions opentelemetry-proto/src/transform/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
ExportMetricsServiceRequest {
resource_metrics: vec![TonicResourceMetrics {
resource: Some((&rm.resource).into()),
scope_metrics: rm.scope_metrics.iter().map(Into::into).collect(),
scope_metrics: rm.scope_metrics().map(Into::into).collect(),

Check warning on line 118 in opentelemetry-proto/src/transform/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-proto/src/transform/metrics.rs#L118

Added line #L118 was not covered by tests
schema_url: rm.resource.schema_url().map(Into::into).unwrap_or_default(),
}],
}
Expand All @@ -136,7 +136,7 @@
fn from(sm: &SdkScopeMetrics) -> Self {
TonicScopeMetrics {
scope: Some((&sm.scope, None).into()),
metrics: sm.metrics.iter().map(Into::into).collect(),
metrics: sm.metrics().map(Into::into).collect(),

Check warning on line 139 in opentelemetry-proto/src/transform/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-proto/src/transform/metrics.rs#L139

Added line #L139 was not covered by tests
schema_url: sm
.scope
.schema_url()
Expand Down Expand Up @@ -273,8 +273,7 @@
fn from(sum: &SdkSum<T>) -> Self {
TonicSum {
data_points: sum
.data_points
.iter()
.data_points()

Check warning on line 276 in opentelemetry-proto/src/transform/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-proto/src/transform/metrics.rs#L276

Added line #L276 was not covered by tests
.map(|dp| TonicNumberDataPoint {
attributes: dp.attributes.iter().map(Into::into).collect(),
start_time_unix_nano: to_nanos(sum.start_time),
Expand Down
11 changes: 2 additions & 9 deletions opentelemetry-sdk/benches/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use opentelemetry_sdk::{
data::ResourceMetrics, new_view, reader::MetricReader, Aggregation, Instrument,
InstrumentKind, ManualReader, Pipeline, SdkMeterProvider, Stream, Temporality, View,
},
Resource,
};
use rand::Rng;
use std::sync::{Arc, Weak};
Expand Down Expand Up @@ -240,10 +239,7 @@ fn counters(c: &mut Criterion) {
});

let (rdr, cntr) = bench_counter(None, "cumulative");
let mut rm = ResourceMetrics {
resource: Resource::builder_empty().build(),
scope_metrics: Vec::new(),
};
let mut rm = ResourceMetrics::default();

group.bench_function("CollectOneAttr", |b| {
let mut v = 0;
Expand Down Expand Up @@ -337,10 +333,7 @@ fn benchmark_collect_histogram(b: &mut Bencher, n: usize) {
h.record(1, &[]);
}

let mut rm = ResourceMetrics {
resource: Resource::builder_empty().build(),
scope_metrics: Vec::new(),
};
let mut rm = ResourceMetrics::default();

b.iter(|| {
let _ = r.collect(&mut rm);
Expand Down
6 changes: 2 additions & 4 deletions opentelemetry-sdk/src/metrics/aggregation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,8 @@ impl Aggregation {
#[cfg(test)]
mod tests {
use crate::metrics::error::{MetricError, MetricResult};
use crate::metrics::{
internal::{EXPO_MAX_SCALE, EXPO_MIN_SCALE},
Aggregation,
};
use crate::metrics::internal::{EXPO_MAX_SCALE, EXPO_MIN_SCALE};
use crate::metrics::Aggregation;

#[test]
fn validate_aggregation() {
Expand Down
36 changes: 33 additions & 3 deletions opentelemetry-sdk/src/metrics/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,23 @@
/// The entity that collected the metrics.
pub resource: Resource,
/// The collection of metrics with unique [InstrumentationScope]s.
pub scope_metrics: Vec<ScopeMetrics>,
pub(crate) scope_metrics: Vec<ScopeMetrics>,
}

impl Default for ResourceMetrics {
fn default() -> Self {
Self {
resource: Resource::empty(),
scope_metrics: Vec::new(),
}
}

Check warning on line 26 in opentelemetry-sdk/src/metrics/data/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/data/mod.rs#L21-L26

Added lines #L21 - L26 were not covered by tests
}

impl ResourceMetrics {
/// Returns an iterator over the [ScopeMetrics] in [ResourceMetrics].
pub fn scope_metrics(&self) -> impl Iterator<Item = &ScopeMetrics> {
self.scope_metrics.iter()
}

Check warning on line 33 in opentelemetry-sdk/src/metrics/data/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/data/mod.rs#L31-L33

Added lines #L31 - L33 were not covered by tests
}

/// A collection of metrics produced by a meter.
Expand All @@ -23,7 +39,14 @@
/// The [InstrumentationScope] that the meter was created with.
pub scope: InstrumentationScope,
/// The list of aggregations created by the meter.
pub metrics: Vec<Metric>,
pub(crate) metrics: Vec<Metric>,
}

impl ScopeMetrics {
/// Returns an iterator over the [Metric]s in [ScopeMetrics].
pub fn metrics(&self) -> impl Iterator<Item = &Metric> {
self.metrics.iter()
}

Check warning on line 49 in opentelemetry-sdk/src/metrics/data/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/data/mod.rs#L47-L49

Added lines #L47 - L49 were not covered by tests
}

/// A collection of one or more aggregated time series from an [Instrument].
Expand Down Expand Up @@ -146,7 +169,7 @@
#[derive(Debug, Clone)]
pub struct Sum<T> {
/// Represents individual aggregated measurements with unique attributes.
pub data_points: Vec<SumDataPoint<T>>,
pub(crate) data_points: Vec<SumDataPoint<T>>,
/// The time when the time series was started.
pub start_time: SystemTime,
/// The time when the time series was recorded.
Expand All @@ -158,6 +181,13 @@
pub is_monotonic: bool,
}

impl<T> Sum<T> {
/// Returns an iterator over the [SumDataPoint]s in [Sum].
pub fn data_points(&self) -> impl Iterator<Item = &SumDataPoint<T>> {
self.data_points.iter()
}

Check warning on line 188 in opentelemetry-sdk/src/metrics/data/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/data/mod.rs#L186-L188

Added lines #L186 - L188 were not covered by tests
}

/// Represents the histogram of all measurements of values from an instrument.
#[derive(Debug, Clone)]
pub struct Histogram<T> {
Expand Down
16 changes: 9 additions & 7 deletions opentelemetry-stdout/src/metrics/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
metrics.resource.iter().for_each(|(k, v)| {
println!("\t -> {}={:?}", k, v);
});
print_metrics(&metrics.scope_metrics);
print_metrics(metrics.scope_metrics());

Check warning on line 58 in opentelemetry-stdout/src/metrics/exporter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-stdout/src/metrics/exporter.rs#L58

Added line #L58 was not covered by tests
Ok(())
}
}
Expand All @@ -79,8 +79,8 @@
}
}

fn print_metrics(metrics: &[ScopeMetrics]) {
for (i, metric) in metrics.iter().enumerate() {
fn print_metrics<'a>(metrics: impl Iterator<Item = &'a ScopeMetrics>) {
for (i, metric) in metrics.enumerate() {

Check warning on line 83 in opentelemetry-stdout/src/metrics/exporter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-stdout/src/metrics/exporter.rs#L82-L83

Added lines #L82 - L83 were not covered by tests
println!("\tInstrumentation Scope #{}", i);
println!("\t\tName : {}", &metric.scope.name());
if let Some(version) = &metric.scope.version() {
Expand All @@ -100,7 +100,7 @@
println!("\t\t\t -> {}: {}", kv.key, kv.value);
});

metric.metrics.iter().enumerate().for_each(|(i, metric)| {
metric.metrics().enumerate().for_each(|(i, metric)| {

Check warning on line 103 in opentelemetry-stdout/src/metrics/exporter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-stdout/src/metrics/exporter.rs#L103

Added line #L103 was not covered by tests
println!("Metric #{}", i);
println!("\t\tName : {}", &metric.name);
println!("\t\tDescription : {}", &metric.description);
Expand Down Expand Up @@ -155,7 +155,7 @@
"\t\tEndTime : {}",
datetime.format("%Y-%m-%d %H:%M:%S%.6f")
);
print_sum_data_points(&sum.data_points);
print_sum_data_points(sum.data_points());

Check warning on line 158 in opentelemetry-stdout/src/metrics/exporter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-stdout/src/metrics/exporter.rs#L158

Added line #L158 was not covered by tests
}

fn print_gauge<T: Debug>(gauge: &Gauge<T>) {
Expand Down Expand Up @@ -195,8 +195,10 @@
print_hist_data_points(&histogram.data_points);
}

fn print_sum_data_points<T: Debug>(data_points: &[SumDataPoint<T>]) {
for (i, data_point) in data_points.iter().enumerate() {
fn print_sum_data_points<'a, T: Debug + 'a>(
data_points: impl Iterator<Item = &'a SumDataPoint<T>>,
) {
for (i, data_point) in data_points.enumerate() {

Check warning on line 201 in opentelemetry-stdout/src/metrics/exporter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-stdout/src/metrics/exporter.rs#L198-L201

Added lines #L198 - L201 were not covered by tests
println!("\t\tDataPoint #{}", i);
println!("\t\t\tValue : {:#?}", data_point.value);
println!("\t\t\tAttributes :");
Expand Down