Skip to content

Commit 2430aa8

Browse files
feat: add shutdown with timeout for metric exporter
1 parent 79c27c7 commit 2430aa8

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
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/src/metrics/exporter.rs

Lines changed: 5 additions & 0 deletions
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,6 +28,9 @@ 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.
31+
fn shutdown_with_timeout(&self, timeout: Duration) -> OTelSdkResult;
32+
33+
/// Shutdown with the default timeout of 5 seconds.
2934
fn shutdown(&self) -> OTelSdkResult;
3035

3136
/// Access the [Temporality] of the MetricExporter.

opentelemetry-sdk/src/metrics/in_memory_exporter.rs

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

1112
/// An in-memory metrics exporter that stores metrics data in memory.
1213
///
@@ -281,6 +282,10 @@ impl PushMetricExporter for InMemoryMetricExporter {
281282
Ok(())
282283
}
283284

285+
fn shutdown_with_timeout(&self, _timeout: Duration) -> OTelSdkResult {
286+
Ok(())
287+
}
288+
284289
fn temporality(&self) -> Temporality {
285290
self.temporality
286291
}

opentelemetry-sdk/src/metrics/periodic_reader.rs

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

568+
fn shutdown_with_timeout(&self, _timeout: Duration) -> OTelSdkResult {
569+
Ok(())
570+
}
571+
568572
fn temporality(&self) -> Temporality {
569573
Temporality::Cumulative
570574
}
@@ -585,6 +589,10 @@ mod tests {
585589
}
586590

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

opentelemetry-stdout/src/metrics/exporter.rs

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

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

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

0 commit comments

Comments
 (0)