Skip to content

Commit af3a33e

Browse files
mohammadVatandoostBSteffaniakcijothomas
authored
feat: Add shutdown with timeout for metric exporter (#2854)
Co-authored-by: Braden Steffaniak <[email protected]> Co-authored-by: Cijo Thomas <[email protected]> Co-authored-by: Cijo Thomas <[email protected]>
1 parent 37d7947 commit af3a33e

File tree

6 files changed

+32
-1
lines changed

6 files changed

+32
-1
lines changed

opentelemetry-otlp/src/metric.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use opentelemetry_sdk::metrics::{
2121
data::ResourceMetrics, exporter::PushMetricExporter, Temporality,
2222
};
2323
use std::fmt::{Debug, Formatter};
24+
use std::time::Duration;
2425

2526
/// Target to which the exporter is going to send metrics, defaults to https://localhost:4317/v1/metrics.
2627
/// Learn about the relationship between this constant and default/spans/logs at
@@ -163,6 +164,10 @@ impl PushMetricExporter for MetricExporter {
163164
}
164165

165166
fn shutdown(&self) -> OTelSdkResult {
167+
self.shutdown_with_timeout(Duration::from_secs(5))
168+
}
169+
170+
fn shutdown_with_timeout(&self, _timeout: std::time::Duration) -> OTelSdkResult {
166171
match &self.client {
167172
#[cfg(feature = "grpc-tonic")]
168173
SupportedTransportClient::Tonic(client) => client.shutdown(),

opentelemetry-sdk/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Released 2025-Mar-21
6868

6969
Custom exporters will need to internally synchronize any mutable state, if applicable.
7070

71+
- **Breaking** The `shutdown_with_timeout` method is added to MetricExporter trait. This is breaking change for custom `MetricExporter` authors.
7172
- Bug Fix: `BatchLogProcessor` now correctly calls `shutdown` on the exporter
7273
when its `shutdown` is invoked.
7374

opentelemetry-sdk/src/metrics/exporter.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Interfaces for exporting metrics
2+
23
use crate::error::OTelSdkResult;
4+
use std::time::Duration;
35

46
use crate::metrics::data::ResourceMetrics;
57

@@ -26,7 +28,12 @@ pub trait PushMetricExporter: Send + Sync + 'static {
2628
///
2729
/// After Shutdown is called, calls to Export will perform no operation and
2830
/// instead will return an error indicating the shutdown state.
29-
fn shutdown(&self) -> OTelSdkResult;
31+
fn shutdown_with_timeout(&self, timeout: Duration) -> OTelSdkResult;
32+
33+
/// Shutdown with the default timeout of 5 seconds.
34+
fn shutdown(&self) -> OTelSdkResult {
35+
self.shutdown_with_timeout(Duration::from_secs(5))
36+
}
3037

3138
/// Access the [Temporality] of the MetricExporter.
3239
fn temporality(&self) -> Temporality;

opentelemetry-sdk/src/metrics/in_memory_exporter.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::InMemoryExporterError;
88
use std::collections::VecDeque;
99
use std::fmt;
1010
use std::sync::{Arc, Mutex};
11+
use std::time::Duration;
1112

1213
use super::data::{AggregatedMetrics, Metric, ScopeMetrics};
1314

@@ -253,6 +254,10 @@ impl PushMetricExporter for InMemoryMetricExporter {
253254
Ok(())
254255
}
255256

257+
fn shutdown_with_timeout(&self, _timeout: Duration) -> OTelSdkResult {
258+
Ok(())
259+
}
260+
256261
fn temporality(&self) -> Temporality {
257262
self.temporality
258263
}

opentelemetry-sdk/src/metrics/periodic_reader.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,10 @@ mod tests {
566566
Ok(())
567567
}
568568

569+
fn shutdown_with_timeout(&self, _timeout: Duration) -> OTelSdkResult {
570+
Ok(())
571+
}
572+
569573
fn temporality(&self) -> Temporality {
570574
Temporality::Cumulative
571575
}
@@ -586,6 +590,10 @@ mod tests {
586590
}
587591

588592
fn shutdown(&self) -> OTelSdkResult {
593+
self.shutdown_with_timeout(Duration::from_secs(5))
594+
}
595+
596+
fn shutdown_with_timeout(&self, _timeout: Duration) -> OTelSdkResult {
589597
self.is_shutdown.store(true, Ordering::Relaxed);
590598
Ok(())
591599
}

opentelemetry-stdout/src/metrics/exporter.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use opentelemetry_sdk::{
1414
};
1515
use std::fmt::Debug;
1616
use std::sync::atomic;
17+
use std::time::Duration;
1718

1819
/// An OpenTelemetry exporter that writes to stdout on export.
1920
pub struct MetricExporter {
@@ -65,6 +66,10 @@ impl PushMetricExporter for MetricExporter {
6566
}
6667

6768
fn shutdown(&self) -> OTelSdkResult {
69+
self.shutdown_with_timeout(Duration::from_secs(5))
70+
}
71+
72+
fn shutdown_with_timeout(&self, _timeout: Duration) -> OTelSdkResult {
6873
self.is_shutdown.store(true, atomic::Ordering::SeqCst);
6974
Ok(())
7075
}

0 commit comments

Comments
 (0)