Skip to content

Commit a4575af

Browse files
utpillacijothomas
andauthored
fix: Update ResourceMetrics public API (#2965)
Co-authored-by: Cijo Thomas <[email protected]>
1 parent 82e5ed4 commit a4575af

File tree

5 files changed

+49
-27
lines changed

5 files changed

+49
-27
lines changed

opentelemetry-proto/src/transform/metrics.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub mod tonic {
115115
ExportMetricsServiceRequest {
116116
resource_metrics: vec![TonicResourceMetrics {
117117
resource: Some((&rm.resource).into()),
118-
scope_metrics: rm.scope_metrics.iter().map(Into::into).collect(),
118+
scope_metrics: rm.scope_metrics().map(Into::into).collect(),
119119
schema_url: rm.resource.schema_url().map(Into::into).unwrap_or_default(),
120120
}],
121121
}
@@ -136,7 +136,7 @@ pub mod tonic {
136136
fn from(sm: &SdkScopeMetrics) -> Self {
137137
TonicScopeMetrics {
138138
scope: Some((&sm.scope, None).into()),
139-
metrics: sm.metrics.iter().map(Into::into).collect(),
139+
metrics: sm.metrics().map(Into::into).collect(),
140140
schema_url: sm
141141
.scope
142142
.schema_url()
@@ -273,8 +273,7 @@ pub mod tonic {
273273
fn from(sum: &SdkSum<T>) -> Self {
274274
TonicSum {
275275
data_points: sum
276-
.data_points
277-
.iter()
276+
.data_points()
278277
.map(|dp| TonicNumberDataPoint {
279278
attributes: dp.attributes.iter().map(Into::into).collect(),
280279
start_time_unix_nano: to_nanos(sum.start_time),

opentelemetry-sdk/benches/metric.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use opentelemetry_sdk::{
99
data::ResourceMetrics, new_view, reader::MetricReader, Aggregation, Instrument,
1010
InstrumentKind, ManualReader, Pipeline, SdkMeterProvider, Stream, Temporality, View,
1111
},
12-
Resource,
1312
};
1413
use rand::Rng;
1514
use std::sync::{Arc, Weak};
@@ -240,10 +239,7 @@ fn counters(c: &mut Criterion) {
240239
});
241240

242241
let (rdr, cntr) = bench_counter(None, "cumulative");
243-
let mut rm = ResourceMetrics {
244-
resource: Resource::builder_empty().build(),
245-
scope_metrics: Vec::new(),
246-
};
242+
let mut rm = ResourceMetrics::default();
247243

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

340-
let mut rm = ResourceMetrics {
341-
resource: Resource::builder_empty().build(),
342-
scope_metrics: Vec::new(),
343-
};
336+
let mut rm = ResourceMetrics::default();
344337

345338
b.iter(|| {
346339
let _ = r.collect(&mut rm);

opentelemetry-sdk/src/metrics/aggregation.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,8 @@ impl Aggregation {
151151
#[cfg(test)]
152152
mod tests {
153153
use crate::metrics::error::{MetricError, MetricResult};
154-
use crate::metrics::{
155-
internal::{EXPO_MAX_SCALE, EXPO_MIN_SCALE},
156-
Aggregation,
157-
};
154+
use crate::metrics::internal::{EXPO_MAX_SCALE, EXPO_MIN_SCALE};
155+
use crate::metrics::Aggregation;
158156

159157
#[test]
160158
fn validate_aggregation() {

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,23 @@ pub struct ResourceMetrics {
1414
/// The entity that collected the metrics.
1515
pub resource: Resource,
1616
/// The collection of metrics with unique [InstrumentationScope]s.
17-
pub scope_metrics: Vec<ScopeMetrics>,
17+
pub(crate) scope_metrics: Vec<ScopeMetrics>,
18+
}
19+
20+
impl Default for ResourceMetrics {
21+
fn default() -> Self {
22+
Self {
23+
resource: Resource::empty(),
24+
scope_metrics: Vec::new(),
25+
}
26+
}
27+
}
28+
29+
impl ResourceMetrics {
30+
/// Returns an iterator over the [ScopeMetrics] in [ResourceMetrics].
31+
pub fn scope_metrics(&self) -> impl Iterator<Item = &ScopeMetrics> {
32+
self.scope_metrics.iter()
33+
}
1834
}
1935

2036
/// A collection of metrics produced by a meter.
@@ -23,7 +39,14 @@ pub struct ScopeMetrics {
2339
/// The [InstrumentationScope] that the meter was created with.
2440
pub scope: InstrumentationScope,
2541
/// The list of aggregations created by the meter.
26-
pub metrics: Vec<Metric>,
42+
pub(crate) metrics: Vec<Metric>,
43+
}
44+
45+
impl ScopeMetrics {
46+
/// Returns an iterator over the [Metric]s in [ScopeMetrics].
47+
pub fn metrics(&self) -> impl Iterator<Item = &Metric> {
48+
self.metrics.iter()
49+
}
2750
}
2851

2952
/// A collection of one or more aggregated time series from an [Instrument].
@@ -146,7 +169,7 @@ pub struct SumDataPoint<T> {
146169
#[derive(Debug, Clone)]
147170
pub struct Sum<T> {
148171
/// Represents individual aggregated measurements with unique attributes.
149-
pub data_points: Vec<SumDataPoint<T>>,
172+
pub(crate) data_points: Vec<SumDataPoint<T>>,
150173
/// The time when the time series was started.
151174
pub start_time: SystemTime,
152175
/// The time when the time series was recorded.
@@ -158,6 +181,13 @@ pub struct Sum<T> {
158181
pub is_monotonic: bool,
159182
}
160183

184+
impl<T> Sum<T> {
185+
/// Returns an iterator over the [SumDataPoint]s in [Sum].
186+
pub fn data_points(&self) -> impl Iterator<Item = &SumDataPoint<T>> {
187+
self.data_points.iter()
188+
}
189+
}
190+
161191
/// Represents the histogram of all measurements of values from an instrument.
162192
#[derive(Debug, Clone)]
163193
pub struct Histogram<T> {

opentelemetry-stdout/src/metrics/exporter.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl PushMetricExporter for MetricExporter {
5555
metrics.resource.iter().for_each(|(k, v)| {
5656
println!("\t -> {}={:?}", k, v);
5757
});
58-
print_metrics(&metrics.scope_metrics);
58+
print_metrics(metrics.scope_metrics());
5959
Ok(())
6060
}
6161
}
@@ -79,8 +79,8 @@ impl PushMetricExporter for MetricExporter {
7979
}
8080
}
8181

82-
fn print_metrics(metrics: &[ScopeMetrics]) {
83-
for (i, metric) in metrics.iter().enumerate() {
82+
fn print_metrics<'a>(metrics: impl Iterator<Item = &'a ScopeMetrics>) {
83+
for (i, metric) in metrics.enumerate() {
8484
println!("\tInstrumentation Scope #{}", i);
8585
println!("\t\tName : {}", &metric.scope.name());
8686
if let Some(version) = &metric.scope.version() {
@@ -100,7 +100,7 @@ fn print_metrics(metrics: &[ScopeMetrics]) {
100100
println!("\t\t\t -> {}: {}", kv.key, kv.value);
101101
});
102102

103-
metric.metrics.iter().enumerate().for_each(|(i, metric)| {
103+
metric.metrics().enumerate().for_each(|(i, metric)| {
104104
println!("Metric #{}", i);
105105
println!("\t\tName : {}", &metric.name);
106106
println!("\t\tDescription : {}", &metric.description);
@@ -155,7 +155,7 @@ fn print_sum<T: Debug>(sum: &Sum<T>) {
155155
"\t\tEndTime : {}",
156156
datetime.format("%Y-%m-%d %H:%M:%S%.6f")
157157
);
158-
print_sum_data_points(&sum.data_points);
158+
print_sum_data_points(sum.data_points());
159159
}
160160

161161
fn print_gauge<T: Debug>(gauge: &Gauge<T>) {
@@ -195,8 +195,10 @@ fn print_histogram<T: Debug>(histogram: &Histogram<T>) {
195195
print_hist_data_points(&histogram.data_points);
196196
}
197197

198-
fn print_sum_data_points<T: Debug>(data_points: &[SumDataPoint<T>]) {
199-
for (i, data_point) in data_points.iter().enumerate() {
198+
fn print_sum_data_points<'a, T: Debug + 'a>(
199+
data_points: impl Iterator<Item = &'a SumDataPoint<T>>,
200+
) {
201+
for (i, data_point) in data_points.enumerate() {
200202
println!("\t\tDataPoint #{}", i);
201203
println!("\t\t\tValue : {:#?}", data_point.value);
202204
println!("\t\t\tAttributes :");

0 commit comments

Comments
 (0)