Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide default implementation for MetricExporter trait methods #2479

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 0 additions & 5 deletions opentelemetry-otlp/src/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
9 changes: 9 additions & 0 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions opentelemetry-sdk/src/metrics/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@
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(())
}

Check warning on line 26 in opentelemetry-sdk/src/metrics/exporter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/exporter.rs#L24-L26

Added lines #L24 - L26 were not covered by tests

/// 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
}
}
22 changes: 1 addition & 21 deletions opentelemetry-sdk/src/metrics/periodic_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)]
Expand All @@ -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]
Expand Down
8 changes: 0 additions & 8 deletions opentelemetry-sdk/src/testing/metrics/in_memory_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 0 additions & 5 deletions opentelemetry-stdout/src/metrics/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
Loading