Skip to content
Merged
Changes from 3 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
26 changes: 12 additions & 14 deletions opentelemetry-sdk/src/metrics/periodic_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ impl<E: PushMetricExporter> PeriodicReader<E> {
};
let cloned_reader = reader.clone();

let mut rm = ResourceMetrics {
resource: Resource::empty(),
scope_metrics: Vec::new(),
};

let result_thread_creation = thread::Builder::new()
.name("OpenTelemetry.Metrics.PeriodicReader".to_string())
.spawn(move || {
Expand All @@ -175,7 +180,7 @@ impl<E: PushMetricExporter> PeriodicReader<E> {
otel_debug!(
name: "PeriodReaderThreadExportingDueToFlush"
);
let export_result = cloned_reader.collect_and_export();
let export_result = cloned_reader.collect_and_export(&mut rm);
otel_debug!(
name: "PeriodReaderInvokedExport",
export_result = format!("{:?}", export_result)
Expand Down Expand Up @@ -231,7 +236,7 @@ impl<E: PushMetricExporter> PeriodicReader<E> {
Ok(Message::Shutdown(response_sender)) => {
// Perform final export and break out of loop and exit the thread
otel_debug!(name: "PeriodReaderThreadExportingDueToShutdown");
let export_result = cloned_reader.collect_and_export();
let export_result = cloned_reader.collect_and_export(&mut rm);
otel_debug!(
name: "PeriodReaderInvokedExport",
export_result = format!("{:?}", export_result)
Expand Down Expand Up @@ -279,7 +284,7 @@ impl<E: PushMetricExporter> PeriodicReader<E> {
name: "PeriodReaderThreadExportingDueToTimer"
);

let export_result = cloned_reader.collect_and_export();
let export_result = cloned_reader.collect_and_export(&mut rm);
otel_debug!(
name: "PeriodReaderInvokedExport",
export_result = format!("{:?}", export_result)
Expand Down Expand Up @@ -331,8 +336,8 @@ impl<E: PushMetricExporter> PeriodicReader<E> {
reader
}

fn collect_and_export(&self) -> OTelSdkResult {
self.inner.collect_and_export()
fn collect_and_export(&self, rm: &mut ResourceMetrics) -> OTelSdkResult {
self.inner.collect_and_export(rm)
}
}

Expand Down Expand Up @@ -378,16 +383,9 @@ impl<E: PushMetricExporter> PeriodicReaderInner<E> {
}
}

fn collect_and_export(&self) -> OTelSdkResult {
// TODO: Reuse the internal vectors. Or refactor to avoid needing any
// owned data structures to be passed to exporters.
let mut rm = ResourceMetrics {
resource: Resource::empty(),
scope_metrics: Vec::new(),
};

fn collect_and_export(&self, rm: &mut ResourceMetrics) -> OTelSdkResult {
let current_time = Instant::now();
let collect_result = self.collect(&mut rm);
let collect_result = self.collect(rm);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this still works without the need to clear the scope_metrics vec within the ResourceMetrics. I'm not sure if we have tests for this already but we need to ensure that clearing is not required by verifying that subsequent collects don't continue to show the old data from the previous collect (in case of Delta).

let time_taken_for_collect = current_time.elapsed();

#[allow(clippy::question_mark)]
Expand Down
Loading