Skip to content

Commit 970bb1e

Browse files
authored
fix: Avoid exposing implementation detail in public API for PushMetricExporter (#2968)
1 parent a4575af commit 970bb1e

File tree

4 files changed

+128
-37
lines changed

4 files changed

+128
-37
lines changed

opentelemetry-proto/src/transform/metrics.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,16 @@ pub mod tonic {
208208
fn from(hist: &SdkHistogram<T>) -> Self {
209209
TonicHistogram {
210210
data_points: hist
211-
.data_points
212-
.iter()
211+
.data_points()
213212
.map(|dp| TonicHistogramDataPoint {
214-
attributes: dp.attributes.iter().map(Into::into).collect(),
213+
attributes: dp.attributes().map(Into::into).collect(),
215214
start_time_unix_nano: to_nanos(hist.start_time),
216215
time_unix_nano: to_nanos(hist.time),
217216
count: dp.count,
218217
sum: Some(dp.sum.into_f64()),
219218
bucket_counts: dp.bucket_counts.clone(),
220219
explicit_bounds: dp.bounds.clone(),
221-
exemplars: dp.exemplars.iter().map(Into::into).collect(),
220+
exemplars: dp.exemplars().map(Into::into).collect(),
222221
flags: TonicDataPointFlags::default() as u32,
223222
min: dp.min.map(Numeric::into_f64),
224223
max: dp.max.map(Numeric::into_f64),
@@ -236,10 +235,9 @@ pub mod tonic {
236235
fn from(hist: &SdkExponentialHistogram<T>) -> Self {
237236
TonicExponentialHistogram {
238237
data_points: hist
239-
.data_points
240-
.iter()
238+
.data_points()
241239
.map(|dp| TonicExponentialHistogramDataPoint {
242-
attributes: dp.attributes.iter().map(Into::into).collect(),
240+
attributes: dp.attributes().map(Into::into).collect(),
243241
start_time_unix_nano: to_nanos(hist.start_time),
244242
time_unix_nano: to_nanos(hist.time),
245243
count: dp.count as u64,
@@ -255,7 +253,7 @@ pub mod tonic {
255253
bucket_counts: dp.negative_bucket.counts.clone(),
256254
}),
257255
flags: TonicDataPointFlags::default() as u32,
258-
exemplars: dp.exemplars.iter().map(Into::into).collect(),
256+
exemplars: dp.exemplars().map(Into::into).collect(),
259257
min: dp.min.map(Numeric::into_f64),
260258
max: dp.max.map(Numeric::into_f64),
261259
zero_threshold: dp.zero_threshold,
@@ -275,10 +273,10 @@ pub mod tonic {
275273
data_points: sum
276274
.data_points()
277275
.map(|dp| TonicNumberDataPoint {
278-
attributes: dp.attributes.iter().map(Into::into).collect(),
276+
attributes: dp.attributes().map(Into::into).collect(),
279277
start_time_unix_nano: to_nanos(sum.start_time),
280278
time_unix_nano: to_nanos(sum.time),
281-
exemplars: dp.exemplars.iter().map(Into::into).collect(),
279+
exemplars: dp.exemplars().map(Into::into).collect(),
282280
flags: TonicDataPointFlags::default() as u32,
283281
value: Some(dp.value.into()),
284282
})
@@ -296,13 +294,12 @@ pub mod tonic {
296294
fn from(gauge: &SdkGauge<T>) -> Self {
297295
TonicGauge {
298296
data_points: gauge
299-
.data_points
300-
.iter()
297+
.data_points()
301298
.map(|dp| TonicNumberDataPoint {
302-
attributes: dp.attributes.iter().map(Into::into).collect(),
299+
attributes: dp.attributes().map(Into::into).collect(),
303300
start_time_unix_nano: gauge.start_time.map(to_nanos).unwrap_or_default(),
304301
time_unix_nano: to_nanos(gauge.time),
305-
exemplars: dp.exemplars.iter().map(Into::into).collect(),
302+
exemplars: dp.exemplars().map(Into::into).collect(),
306303
flags: TonicDataPointFlags::default() as u32,
307304
value: Some(dp.value.into()),
308305
})
@@ -318,8 +315,7 @@ pub mod tonic {
318315
fn from(ex: &SdkExemplar<T>) -> Self {
319316
TonicExemplar {
320317
filtered_attributes: ex
321-
.filtered_attributes
322-
.iter()
318+
.filtered_attributes()
323319
.map(|kv| (&kv.key, &kv.value).into())
324320
.collect(),
325321
time_unix_nano: to_nanos(ex.time),

opentelemetry-sdk/CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ also modified to suppress telemetry before invoking exporters.
4747
- *Breaking* change, affecting custom `PushMetricExporter` authors:
4848
- The `export` method on `PushMetricExporter` now accepts `&ResourceMetrics`
4949
instead of `&mut ResourceMetrics`.
50+
- `ResourceMetrics` no longer exposes `scope_metrics` field, but instead
51+
offers `scope_metrics()` method that returns an iterator over the same.
52+
- `ScopeMetrics` no longer exposes `metrics` field, but instead offers
53+
`metrics()` method that returns an iterator over the same.
54+
- `Sum`, `Gauge`, `Histogram` & `ExponentialHistogram` no longer exposes
55+
`data_points` field, but instead offers `data_points()` method that returns
56+
an iterator over the same.
57+
- `SumDataPoint`, `GaugeDataPoint`, `HistogramDataPoint` &
58+
`ExponentialHistogramDataPoint` no longer exposes `attributes`, `exemplars`
59+
field, but instead offers `attributes()`, and `exemplars()` method that
60+
returns an iterator over the same.
61+
- `Exemplar` no longer exposes `filtered_attributes` field, but instead
62+
offers `filtered_attributes()` method that returns an iterator over
63+
the same.
5064

5165
## 0.29.0
5266

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

Lines changed: 88 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,34 +135,65 @@ impl<T> From<ExponentialHistogram<T>> for MetricData<T> {
135135
pub struct GaugeDataPoint<T> {
136136
/// Attributes is the set of key value pairs that uniquely identify the
137137
/// time series.
138-
pub attributes: Vec<KeyValue>,
138+
pub(crate) attributes: Vec<KeyValue>,
139139
/// The value of this data point.
140140
pub value: T,
141141
/// The sampled [Exemplar]s collected during the time series.
142-
pub exemplars: Vec<Exemplar<T>>,
142+
pub(crate) exemplars: Vec<Exemplar<T>>,
143+
}
144+
145+
impl<T> GaugeDataPoint<T> {
146+
/// Returns an iterator over the attributes in [GaugeDataPoint].
147+
pub fn attributes(&self) -> impl Iterator<Item = &KeyValue> {
148+
self.attributes.iter()
149+
}
150+
151+
/// Returns an iterator over the exemplars in [GaugeDataPoint].
152+
pub fn exemplars(&self) -> impl Iterator<Item = &Exemplar<T>> {
153+
self.exemplars.iter()
154+
}
143155
}
144156

145157
/// A measurement of the current value of an instrument.
146158
#[derive(Debug, Clone)]
147159
pub struct Gauge<T> {
148160
/// Represents individual aggregated measurements with unique attributes.
149-
pub data_points: Vec<GaugeDataPoint<T>>,
161+
pub(crate) data_points: Vec<GaugeDataPoint<T>>,
150162
/// The time when the time series was started.
151163
pub start_time: Option<SystemTime>,
152164
/// The time when the time series was recorded.
153165
pub time: SystemTime,
154166
}
155167

168+
impl<T> Gauge<T> {
169+
/// Returns an iterator over the [GaugeDataPoint]s in [Gauge].
170+
pub fn data_points(&self) -> impl Iterator<Item = &GaugeDataPoint<T>> {
171+
self.data_points.iter()
172+
}
173+
}
174+
156175
/// DataPoint is a single data point in a time series.
157176
#[derive(Debug, Clone, PartialEq)]
158177
pub struct SumDataPoint<T> {
159178
/// Attributes is the set of key value pairs that uniquely identify the
160179
/// time series.
161-
pub attributes: Vec<KeyValue>,
180+
pub(crate) attributes: Vec<KeyValue>,
162181
/// The value of this data point.
163182
pub value: T,
164183
/// The sampled [Exemplar]s collected during the time series.
165-
pub exemplars: Vec<Exemplar<T>>,
184+
pub(crate) exemplars: Vec<Exemplar<T>>,
185+
}
186+
187+
impl<T> SumDataPoint<T> {
188+
/// Returns an iterator over the attributes in [SumDataPoint].
189+
pub fn attributes(&self) -> impl Iterator<Item = &KeyValue> {
190+
self.attributes.iter()
191+
}
192+
193+
/// Returns an iterator over the exemplars in [SumDataPoint].
194+
pub fn exemplars(&self) -> impl Iterator<Item = &Exemplar<T>> {
195+
self.exemplars.iter()
196+
}
166197
}
167198

168199
/// Represents the sum of all measurements of values from an instrument.
@@ -192,7 +223,7 @@ impl<T> Sum<T> {
192223
#[derive(Debug, Clone)]
193224
pub struct Histogram<T> {
194225
/// Individual aggregated measurements with unique attributes.
195-
pub data_points: Vec<HistogramDataPoint<T>>,
226+
pub(crate) data_points: Vec<HistogramDataPoint<T>>,
196227
/// The time when the time series was started.
197228
pub start_time: SystemTime,
198229
/// The time when the time series was recorded.
@@ -202,11 +233,18 @@ pub struct Histogram<T> {
202233
pub temporality: Temporality,
203234
}
204235

236+
impl<T> Histogram<T> {
237+
/// Returns an iterator over the [HistogramDataPoint]s in [Histogram].
238+
pub fn data_points(&self) -> impl Iterator<Item = &HistogramDataPoint<T>> {
239+
self.data_points.iter()
240+
}
241+
}
242+
205243
/// A single histogram data point in a time series.
206244
#[derive(Debug, Clone, PartialEq)]
207245
pub struct HistogramDataPoint<T> {
208246
/// The set of key value pairs that uniquely identify the time series.
209-
pub attributes: Vec<KeyValue>,
247+
pub(crate) attributes: Vec<KeyValue>,
210248
/// The number of updates this histogram has been calculated with.
211249
pub count: u64,
212250
/// The upper bounds of the buckets of the histogram.
@@ -224,14 +262,26 @@ pub struct HistogramDataPoint<T> {
224262
pub sum: T,
225263

226264
/// The sampled [Exemplar]s collected during the time series.
227-
pub exemplars: Vec<Exemplar<T>>,
265+
pub(crate) exemplars: Vec<Exemplar<T>>,
266+
}
267+
268+
impl<T> HistogramDataPoint<T> {
269+
/// Returns an iterator over the attributes in [HistogramDataPoint].
270+
pub fn attributes(&self) -> impl Iterator<Item = &KeyValue> {
271+
self.attributes.iter()
272+
}
273+
274+
/// Returns an iterator over the exemplars in [HistogramDataPoint].
275+
pub fn exemplars(&self) -> impl Iterator<Item = &Exemplar<T>> {
276+
self.exemplars.iter()
277+
}
228278
}
229279

230280
/// The histogram of all measurements of values from an instrument.
231281
#[derive(Debug, Clone)]
232282
pub struct ExponentialHistogram<T> {
233283
/// The individual aggregated measurements with unique attributes.
234-
pub data_points: Vec<ExponentialHistogramDataPoint<T>>,
284+
pub(crate) data_points: Vec<ExponentialHistogramDataPoint<T>>,
235285
/// When the time series was started.
236286
pub start_time: SystemTime,
237287
/// The time when the time series was recorded.
@@ -241,11 +291,18 @@ pub struct ExponentialHistogram<T> {
241291
pub temporality: Temporality,
242292
}
243293

294+
impl<T> ExponentialHistogram<T> {
295+
/// Returns an iterator over the [ExponentialHistogramDataPoint]s in [ExponentialHistogram].
296+
pub fn data_points(&self) -> impl Iterator<Item = &ExponentialHistogramDataPoint<T>> {
297+
self.data_points.iter()
298+
}
299+
}
300+
244301
/// A single exponential histogram data point in a time series.
245302
#[derive(Debug, Clone, PartialEq)]
246303
pub struct ExponentialHistogramDataPoint<T> {
247304
/// The set of key value pairs that uniquely identify the time series.
248-
pub attributes: Vec<KeyValue>,
305+
pub(crate) attributes: Vec<KeyValue>,
249306

250307
/// The number of updates this histogram has been calculated with.
251308
pub count: usize,
@@ -283,7 +340,19 @@ pub struct ExponentialHistogramDataPoint<T> {
283340
pub zero_threshold: f64,
284341

285342
/// The sampled exemplars collected during the time series.
286-
pub exemplars: Vec<Exemplar<T>>,
343+
pub(crate) exemplars: Vec<Exemplar<T>>,
344+
}
345+
346+
impl<T> ExponentialHistogramDataPoint<T> {
347+
/// Returns an iterator over the attributes in [ExponentialHistogramDataPoint].
348+
pub fn attributes(&self) -> impl Iterator<Item = &KeyValue> {
349+
self.attributes.iter()
350+
}
351+
352+
/// Returns an iterator over the exemplars in [ExponentialHistogramDataPoint].
353+
pub fn exemplars(&self) -> impl Iterator<Item = &Exemplar<T>> {
354+
self.exemplars.iter()
355+
}
287356
}
288357

289358
/// A set of bucket counts, encoded in a contiguous array of counts.
@@ -304,7 +373,7 @@ pub struct ExponentialBucket {
304373
pub struct Exemplar<T> {
305374
/// The attributes recorded with the measurement but filtered out of the
306375
/// time series' aggregated data.
307-
pub filtered_attributes: Vec<KeyValue>,
376+
pub(crate) filtered_attributes: Vec<KeyValue>,
308377
/// The time when the measurement was recorded.
309378
pub time: SystemTime,
310379
/// The measured value.
@@ -319,6 +388,13 @@ pub struct Exemplar<T> {
319388
pub trace_id: [u8; 16],
320389
}
321390

391+
impl<T> Exemplar<T> {
392+
/// Returns an iterator over the filtered attributes in [Exemplar].
393+
pub fn filtered_attributes(&self) -> impl Iterator<Item = &KeyValue> {
394+
self.filtered_attributes.iter()
395+
}
396+
}
397+
322398
#[cfg(test)]
323399
mod tests {
324400

opentelemetry-stdout/src/metrics/exporter.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ fn print_metrics<'a>(metrics: impl Iterator<Item = &'a ScopeMetrics>) {
125125
}
126126
MetricData::ExponentialHistogram(_) => {
127127
println!("\t\tType : Exponential Histogram");
128+
// TODO: add support for ExponentialHistogram
128129
}
129130
}
130131
}
@@ -172,7 +173,7 @@ fn print_gauge<T: Debug>(gauge: &Gauge<T>) {
172173
"\t\tEndTime : {}",
173174
datetime.format("%Y-%m-%d %H:%M:%S%.6f")
174175
);
175-
print_gauge_data_points(&gauge.data_points);
176+
print_gauge_data_points(gauge.data_points());
176177
}
177178

178179
fn print_histogram<T: Debug>(histogram: &Histogram<T>) {
@@ -192,7 +193,7 @@ fn print_histogram<T: Debug>(histogram: &Histogram<T>) {
192193
datetime.format("%Y-%m-%d %H:%M:%S%.6f")
193194
);
194195
println!("\t\tHistogram DataPoints");
195-
print_hist_data_points(&histogram.data_points);
196+
print_hist_data_points(histogram.data_points());
196197
}
197198

198199
fn print_sum_data_points<'a, T: Debug + 'a>(
@@ -202,25 +203,29 @@ fn print_sum_data_points<'a, T: Debug + 'a>(
202203
println!("\t\tDataPoint #{}", i);
203204
println!("\t\t\tValue : {:#?}", data_point.value);
204205
println!("\t\t\tAttributes :");
205-
for kv in data_point.attributes.iter() {
206+
for kv in data_point.attributes() {
206207
println!("\t\t\t\t -> {}: {}", kv.key, kv.value.as_str());
207208
}
208209
}
209210
}
210211

211-
fn print_gauge_data_points<T: Debug>(data_points: &[GaugeDataPoint<T>]) {
212-
for (i, data_point) in data_points.iter().enumerate() {
212+
fn print_gauge_data_points<'a, T: Debug + 'a>(
213+
data_points: impl Iterator<Item = &'a GaugeDataPoint<T>>,
214+
) {
215+
for (i, data_point) in data_points.enumerate() {
213216
println!("\t\tDataPoint #{}", i);
214217
println!("\t\t\tValue : {:#?}", data_point.value);
215218
println!("\t\t\tAttributes :");
216-
for kv in data_point.attributes.iter() {
219+
for kv in data_point.attributes() {
217220
println!("\t\t\t\t -> {}: {}", kv.key, kv.value.as_str());
218221
}
219222
}
220223
}
221224

222-
fn print_hist_data_points<T: Debug>(data_points: &[HistogramDataPoint<T>]) {
223-
for (i, data_point) in data_points.iter().enumerate() {
225+
fn print_hist_data_points<'a, T: Debug + 'a>(
226+
data_points: impl Iterator<Item = &'a HistogramDataPoint<T>>,
227+
) {
228+
for (i, data_point) in data_points.enumerate() {
224229
println!("\t\tDataPoint #{}", i);
225230
println!("\t\t\tCount : {}", data_point.count);
226231
println!("\t\t\tSum : {:?}", data_point.sum);
@@ -233,7 +238,7 @@ fn print_hist_data_points<T: Debug>(data_points: &[HistogramDataPoint<T>]) {
233238
}
234239

235240
println!("\t\t\tAttributes :");
236-
for kv in data_point.attributes.iter() {
241+
for kv in data_point.attributes() {
237242
println!("\t\t\t\t -> {}: {}", kv.key, kv.value.as_str());
238243
}
239244

0 commit comments

Comments
 (0)