Skip to content

Commit da80ade

Browse files
authored
Merge branch 'main' into cijothomas/metric-doc1
2 parents b295f0f + a4575af commit da80ade

File tree

8 files changed

+67
-42
lines changed

8 files changed

+67
-42
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ jobs:
9898
with:
9999
toolchain: nightly-2024-06-30
100100
components: rustfmt
101+
- name: Patch dependencies versions
102+
run: bash ./scripts/patch_dependencies.sh
101103
- name: external-type-check
102104
run: |
103105
cargo install [email protected]

deny.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,22 @@ exceptions = [
1515
{ allow = ["Unicode-3.0"], crate = "icu_locid" }, # This crate gets used transitively by `reqwest`.
1616
{ allow = ["Unicode-3.0"], crate = "icu_locid_transform" }, # This crate gets used transitively by `reqwest`.
1717
{ allow = ["Unicode-3.0"], crate = "icu_locid_transform_data" }, # This crate gets used transitively by `reqwest`.
18+
{ allow = ["Unicode-3.0"], crate = "icu_locale_core" }, # This crate gets used transitively by `reqwest`.
1819
{ allow = ["Unicode-3.0"], crate = "icu_normalizer" }, # This crate gets used transitively by `reqwest`.
1920
{ allow = ["Unicode-3.0"], crate = "icu_normalizer_data" }, # This crate gets used transitively by `reqwest`.
2021
{ allow = ["Unicode-3.0"], crate = "icu_properties" }, # This crate gets used transitively by `reqwest`.
2122
{ allow = ["Unicode-3.0"], crate = "icu_properties_data" }, # This crate gets used transitively by `reqwest`.
2223
{ allow = ["Unicode-3.0"], crate = "icu_provider" }, # This crate gets used transitively by `reqwest`.
2324
{ allow = ["Unicode-3.0"], crate = "icu_provider_macros" }, # This crate gets used transitively by `reqwest`.
25+
{ allow = ["Unicode-3.0"], crate = "potential_utf" }, # This crate gets used transitively by `reqwest`.
2426
{ allow = ["Unicode-3.0"], crate = "litemap" }, # This crate gets used transitively by `reqwest`.
2527
{ allow = ["Unicode-3.0"], crate = "tinystr" }, # This crate gets used transitively by `reqwest`.
2628
{ allow = ["Unicode-3.0"], crate = "writeable" }, # This crate gets used transitively by `reqwest`.
2729
{ allow = ["Unicode-3.0"], crate = "unicode-ident" }, # This crate gets used transitively by `reqwest` and other crates.
2830
{ allow = ["Unicode-3.0"], crate = "yoke" }, # This crate gets used transitively by `reqwest`.
2931
{ allow = ["Unicode-3.0"], crate = "yoke-derive" }, # This crate gets used transitively by `reqwest`.
3032
{ allow = ["Unicode-3.0"], crate = "zerovec" }, # This crate gets used transitively by `reqwest`.
33+
{ allow = ["Unicode-3.0"], crate = "zerotrie" }, # This crate gets used transitively by `reqwest`.
3134
{ allow = ["Unicode-3.0"], crate = "zerovec-derive" }, # This crate gets used transitively by `reqwest`.
3235
{ allow = ["Unicode-3.0"], crate = "zerofrom" }, # This crate gets used transitively by `reqwest`.
3336
{ allow = ["Unicode-3.0"], crate = "zerofrom-derive" }, # This crate gets used transitively by `reqwest`.

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-sdk/src/metrics/periodic_reader.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ impl<E: PushMetricExporter> PeriodicReader<E> {
156156
};
157157
let cloned_reader = reader.clone();
158158

159+
let mut rm = ResourceMetrics {
160+
resource: Resource::empty(),
161+
scope_metrics: Vec::new(),
162+
};
163+
159164
let result_thread_creation = thread::Builder::new()
160165
.name("OpenTelemetry.Metrics.PeriodicReader".to_string())
161166
.spawn(move || {
@@ -175,7 +180,7 @@ impl<E: PushMetricExporter> PeriodicReader<E> {
175180
otel_debug!(
176181
name: "PeriodReaderThreadExportingDueToFlush"
177182
);
178-
let export_result = cloned_reader.collect_and_export();
183+
let export_result = cloned_reader.collect_and_export(&mut rm);
179184
otel_debug!(
180185
name: "PeriodReaderInvokedExport",
181186
export_result = format!("{:?}", export_result)
@@ -231,7 +236,7 @@ impl<E: PushMetricExporter> PeriodicReader<E> {
231236
Ok(Message::Shutdown(response_sender)) => {
232237
// Perform final export and break out of loop and exit the thread
233238
otel_debug!(name: "PeriodReaderThreadExportingDueToShutdown");
234-
let export_result = cloned_reader.collect_and_export();
239+
let export_result = cloned_reader.collect_and_export(&mut rm);
235240
otel_debug!(
236241
name: "PeriodReaderInvokedExport",
237242
export_result = format!("{:?}", export_result)
@@ -279,7 +284,7 @@ impl<E: PushMetricExporter> PeriodicReader<E> {
279284
name: "PeriodReaderThreadExportingDueToTimer"
280285
);
281286

282-
let export_result = cloned_reader.collect_and_export();
287+
let export_result = cloned_reader.collect_and_export(&mut rm);
283288
otel_debug!(
284289
name: "PeriodReaderInvokedExport",
285290
export_result = format!("{:?}", export_result)
@@ -331,8 +336,8 @@ impl<E: PushMetricExporter> PeriodicReader<E> {
331336
reader
332337
}
333338

334-
fn collect_and_export(&self) -> OTelSdkResult {
335-
self.inner.collect_and_export()
339+
fn collect_and_export(&self, rm: &mut ResourceMetrics) -> OTelSdkResult {
340+
self.inner.collect_and_export(rm)
336341
}
337342
}
338343

@@ -378,16 +383,9 @@ impl<E: PushMetricExporter> PeriodicReaderInner<E> {
378383
}
379384
}
380385

381-
fn collect_and_export(&self) -> OTelSdkResult {
382-
// TODO: Reuse the internal vectors. Or refactor to avoid needing any
383-
// owned data structures to be passed to exporters.
384-
let mut rm = ResourceMetrics {
385-
resource: Resource::empty(),
386-
scope_metrics: Vec::new(),
387-
};
388-
386+
fn collect_and_export(&self, rm: &mut ResourceMetrics) -> OTelSdkResult {
389387
let current_time = Instant::now();
390-
let collect_result = self.collect(&mut rm);
388+
let collect_result = self.collect(rm);
391389
let time_taken_for_collect = current_time.elapsed();
392390

393391
#[allow(clippy::question_mark)]
@@ -411,7 +409,7 @@ impl<E: PushMetricExporter> PeriodicReaderInner<E> {
411409

412410
// Relying on futures executor to execute async call.
413411
// TODO: Pass timeout to exporter
414-
futures_executor::block_on(self.exporter.export(&rm))
412+
futures_executor::block_on(self.exporter.export(rm))
415413
}
416414

417415
fn force_flush(&self) -> OTelSdkResult {

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)