Skip to content

Commit 16dc27b

Browse files
committed
Provide default implementation for MetricExporter trait methods
1 parent e378bc8 commit 16dc27b

File tree

6 files changed

+21
-41
lines changed

6 files changed

+21
-41
lines changed

opentelemetry-otlp/src/metric.rs

-5
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,6 @@ impl PushMetricExporter for MetricExporter {
144144
self.client.export(metrics).await
145145
}
146146

147-
async fn force_flush(&self) -> MetricResult<()> {
148-
// this component is stateless
149-
Ok(())
150-
}
151-
152147
fn shutdown(&self) -> MetricResult<()> {
153148
self.client.shutdown()
154149
}

opentelemetry-sdk/CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,15 @@ metadata, a feature introduced in version 0.1.40. [#2418](https://github.com/ope
211211
- Continue enabling one of the async runtime feature flags: `rt-tokio`,
212212
`rt-tokio-current-thread`, or `rt-async-std`.
213213

214+
`PushMetricExporter` now provides default implementation for the following methods.
215+
Custom exporter authors may rely on the defaults, if applicable.
216+
* `force_flush()`
217+
default implementation returns Ok(()) Result.
218+
* `shutdown()` default
219+
implementation returns Ok(()) Result.
220+
* `temporality()` default implementation
221+
returns `Temporality::Cumulative`.
222+
214223
## 0.27.1
215224

216225
Released 2024-Nov-27

opentelemetry-sdk/src/metrics/exporter.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,23 @@ pub trait PushMetricExporter: Send + Sync + 'static {
2121
async fn export(&self, metrics: &mut ResourceMetrics) -> MetricResult<()>;
2222

2323
/// Flushes any metric data held by an exporter.
24-
async fn force_flush(&self) -> MetricResult<()>;
24+
async fn force_flush(&self) -> MetricResult<()>
25+
{
26+
Ok(())
27+
}
2528

2629
/// Releases any held computational resources.
2730
///
2831
/// After Shutdown is called, calls to Export will perform no operation and
2932
/// instead will return an error indicating the shutdown state.
30-
fn shutdown(&self) -> MetricResult<()>;
33+
fn shutdown(&self) -> MetricResult<()>
34+
{
35+
Ok(())
36+
}
3137

3238
/// Access the [Temporality] of the MetricExporter.
33-
fn temporality(&self) -> Temporality;
39+
fn temporality(&self) -> Temporality
40+
{
41+
Temporality::Cumulative
42+
}
3443
}

opentelemetry-sdk/src/metrics/periodic_reader.rs

-20
Original file line numberDiff line numberDiff line change
@@ -518,18 +518,6 @@ mod tests {
518518
Ok(())
519519
}
520520
}
521-
522-
async fn force_flush(&self) -> MetricResult<()> {
523-
Ok(())
524-
}
525-
526-
fn shutdown(&self) -> MetricResult<()> {
527-
Ok(())
528-
}
529-
530-
fn temporality(&self) -> Temporality {
531-
Temporality::Cumulative
532-
}
533521
}
534522

535523
#[derive(Debug, Clone, Default)]
@@ -543,18 +531,10 @@ mod tests {
543531
Ok(())
544532
}
545533

546-
async fn force_flush(&self) -> MetricResult<()> {
547-
Ok(())
548-
}
549-
550534
fn shutdown(&self) -> MetricResult<()> {
551535
self.is_shutdown.store(true, Ordering::Relaxed);
552536
Ok(())
553537
}
554-
555-
fn temporality(&self) -> Temporality {
556-
Temporality::Cumulative
557-
}
558538
}
559539

560540
#[test]

opentelemetry-sdk/src/testing/metrics/in_memory_exporter.rs

-8
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,6 @@ impl PushMetricExporter for InMemoryMetricExporter {
273273
.map_err(MetricError::from)
274274
}
275275

276-
async fn force_flush(&self) -> MetricResult<()> {
277-
Ok(()) // In this implementation, flush does nothing
278-
}
279-
280-
fn shutdown(&self) -> MetricResult<()> {
281-
Ok(())
282-
}
283-
284276
fn temporality(&self) -> Temporality {
285277
self.temporality
286278
}

opentelemetry-stdout/src/metrics/exporter.rs

-5
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ impl PushMetricExporter for MetricExporter {
5454
}
5555
}
5656

57-
async fn force_flush(&self) -> MetricResult<()> {
58-
// exporter holds no state, nothing to flush
59-
Ok(())
60-
}
61-
6257
fn shutdown(&self) -> MetricResult<()> {
6358
self.is_shutdown.store(true, atomic::Ordering::SeqCst);
6459
Ok(())

0 commit comments

Comments
 (0)