From 16dc27b1b04b7d187458939197bb473d51c20761 Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Thu, 26 Dec 2024 16:49:08 -0800 Subject: [PATCH 1/3] Provide default implementation for MetricExporter trait methods --- opentelemetry-otlp/src/metric.rs | 5 ----- opentelemetry-sdk/CHANGELOG.md | 9 +++++++++ opentelemetry-sdk/src/metrics/exporter.rs | 15 +++++++++++--- .../src/metrics/periodic_reader.rs | 20 ------------------- .../src/testing/metrics/in_memory_exporter.rs | 8 -------- opentelemetry-stdout/src/metrics/exporter.rs | 5 ----- 6 files changed, 21 insertions(+), 41 deletions(-) diff --git a/opentelemetry-otlp/src/metric.rs b/opentelemetry-otlp/src/metric.rs index c8bdb38d9e..207e822d10 100644 --- a/opentelemetry-otlp/src/metric.rs +++ b/opentelemetry-otlp/src/metric.rs @@ -144,11 +144,6 @@ impl PushMetricExporter for MetricExporter { self.client.export(metrics).await } - async fn force_flush(&self) -> MetricResult<()> { - // this component is stateless - Ok(()) - } - fn shutdown(&self) -> MetricResult<()> { self.client.shutdown() } diff --git a/opentelemetry-sdk/CHANGELOG.md b/opentelemetry-sdk/CHANGELOG.md index 937e3b0c6b..191a6ef8f1 100644 --- a/opentelemetry-sdk/CHANGELOG.md +++ b/opentelemetry-sdk/CHANGELOG.md @@ -211,6 +211,15 @@ metadata, a feature introduced in version 0.1.40. [#2418](https://github.com/ope - Continue enabling one of the async runtime feature flags: `rt-tokio`, `rt-tokio-current-thread`, or `rt-async-std`. +`PushMetricExporter` now provides default implementation for the following methods. +Custom exporter authors may rely on the defaults, if applicable. + * `force_flush()` +default implementation returns Ok(()) Result. + * `shutdown()` default +implementation returns Ok(()) Result. + * `temporality()` default implementation +returns `Temporality::Cumulative`. + ## 0.27.1 Released 2024-Nov-27 diff --git a/opentelemetry-sdk/src/metrics/exporter.rs b/opentelemetry-sdk/src/metrics/exporter.rs index 33c1fcb6be..c896f6cc1f 100644 --- a/opentelemetry-sdk/src/metrics/exporter.rs +++ b/opentelemetry-sdk/src/metrics/exporter.rs @@ -21,14 +21,23 @@ pub trait PushMetricExporter: Send + Sync + 'static { async fn export(&self, metrics: &mut ResourceMetrics) -> MetricResult<()>; /// Flushes any metric data held by an exporter. - async fn force_flush(&self) -> MetricResult<()>; + async fn force_flush(&self) -> MetricResult<()> + { + Ok(()) + } /// Releases any held computational resources. /// /// After Shutdown is called, calls to Export will perform no operation and /// instead will return an error indicating the shutdown state. - fn shutdown(&self) -> MetricResult<()>; + fn shutdown(&self) -> MetricResult<()> + { + Ok(()) + } /// Access the [Temporality] of the MetricExporter. - fn temporality(&self) -> Temporality; + fn temporality(&self) -> Temporality + { + Temporality::Cumulative + } } diff --git a/opentelemetry-sdk/src/metrics/periodic_reader.rs b/opentelemetry-sdk/src/metrics/periodic_reader.rs index 2cee6c4d0d..172713d1cb 100644 --- a/opentelemetry-sdk/src/metrics/periodic_reader.rs +++ b/opentelemetry-sdk/src/metrics/periodic_reader.rs @@ -518,18 +518,6 @@ mod tests { Ok(()) } } - - async fn force_flush(&self) -> MetricResult<()> { - Ok(()) - } - - fn shutdown(&self) -> MetricResult<()> { - Ok(()) - } - - fn temporality(&self) -> Temporality { - Temporality::Cumulative - } } #[derive(Debug, Clone, Default)] @@ -543,18 +531,10 @@ mod tests { Ok(()) } - async fn force_flush(&self) -> MetricResult<()> { - Ok(()) - } - fn shutdown(&self) -> MetricResult<()> { self.is_shutdown.store(true, Ordering::Relaxed); Ok(()) } - - fn temporality(&self) -> Temporality { - Temporality::Cumulative - } } #[test] diff --git a/opentelemetry-sdk/src/testing/metrics/in_memory_exporter.rs b/opentelemetry-sdk/src/testing/metrics/in_memory_exporter.rs index 1d6f9c2754..170a2c4c93 100644 --- a/opentelemetry-sdk/src/testing/metrics/in_memory_exporter.rs +++ b/opentelemetry-sdk/src/testing/metrics/in_memory_exporter.rs @@ -273,14 +273,6 @@ impl PushMetricExporter for InMemoryMetricExporter { .map_err(MetricError::from) } - async fn force_flush(&self) -> MetricResult<()> { - Ok(()) // In this implementation, flush does nothing - } - - fn shutdown(&self) -> MetricResult<()> { - Ok(()) - } - fn temporality(&self) -> Temporality { self.temporality } diff --git a/opentelemetry-stdout/src/metrics/exporter.rs b/opentelemetry-stdout/src/metrics/exporter.rs index 54feb33c41..d939a35bc9 100644 --- a/opentelemetry-stdout/src/metrics/exporter.rs +++ b/opentelemetry-stdout/src/metrics/exporter.rs @@ -54,11 +54,6 @@ impl PushMetricExporter for MetricExporter { } } - async fn force_flush(&self) -> MetricResult<()> { - // exporter holds no state, nothing to flush - Ok(()) - } - fn shutdown(&self) -> MetricResult<()> { self.is_shutdown.store(true, atomic::Ordering::SeqCst); Ok(()) From 8f25952ecea55528ef6fba391b3c787a3c05d970 Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Thu, 26 Dec 2024 16:49:55 -0800 Subject: [PATCH 2/3] fmt --- opentelemetry-sdk/src/metrics/exporter.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/opentelemetry-sdk/src/metrics/exporter.rs b/opentelemetry-sdk/src/metrics/exporter.rs index c896f6cc1f..dd9136eb82 100644 --- a/opentelemetry-sdk/src/metrics/exporter.rs +++ b/opentelemetry-sdk/src/metrics/exporter.rs @@ -21,8 +21,7 @@ pub trait PushMetricExporter: Send + Sync + 'static { async fn export(&self, metrics: &mut ResourceMetrics) -> MetricResult<()>; /// Flushes any metric data held by an exporter. - async fn force_flush(&self) -> MetricResult<()> - { + async fn force_flush(&self) -> MetricResult<()> { Ok(()) } @@ -30,14 +29,12 @@ pub trait PushMetricExporter: Send + Sync + 'static { /// /// After Shutdown is called, calls to Export will perform no operation and /// instead will return an error indicating the shutdown state. - fn shutdown(&self) -> MetricResult<()> - { + fn shutdown(&self) -> MetricResult<()> { Ok(()) } /// Access the [Temporality] of the MetricExporter. - fn temporality(&self) -> Temporality - { + fn temporality(&self) -> Temporality { Temporality::Cumulative } } From d34551423646897b2b567e853d6f1c9139718237 Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Thu, 26 Dec 2024 17:42:18 -0800 Subject: [PATCH 3/3] unused --- opentelemetry-sdk/src/metrics/periodic_reader.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-sdk/src/metrics/periodic_reader.rs b/opentelemetry-sdk/src/metrics/periodic_reader.rs index 172713d1cb..efe51731e2 100644 --- a/opentelemetry-sdk/src/metrics/periodic_reader.rs +++ b/opentelemetry-sdk/src/metrics/periodic_reader.rs @@ -472,7 +472,7 @@ mod tests { use crate::{ metrics::{ data::ResourceMetrics, exporter::PushMetricExporter, reader::MetricReader, MetricError, - MetricResult, SdkMeterProvider, Temporality, + MetricResult, SdkMeterProvider, }, testing::metrics::InMemoryMetricExporter, Resource,