Skip to content

Commit 82e5ed4

Browse files
authored
fix: PeriodicReader to reuse data structures across collect (#2963)
1 parent 8fe3dcc commit 82e5ed4

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

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 {

0 commit comments

Comments
 (0)