Skip to content

Commit cd478d0

Browse files
add shutdown with timeout for log exporter
1 parent bc82d4f commit cd478d0

File tree

13 files changed

+44
-17
lines changed

13 files changed

+44
-17
lines changed

opentelemetry-otlp/src/exporter/http/logs.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use http::{header::CONTENT_TYPE, Method};
33
use opentelemetry::otel_debug;
44
use opentelemetry_sdk::error::{OTelSdkError, OTelSdkResult};
55
use opentelemetry_sdk::logs::{LogBatch, LogExporter};
6+
use std::time;
67

78
impl LogExporter for OtlpHttpClient {
89
async fn export(&self, batch: LogBatch<'_>) -> OTelSdkResult {
@@ -46,7 +47,7 @@ impl LogExporter for OtlpHttpClient {
4647
Ok(())
4748
}
4849

49-
fn shutdown(&self) -> OTelSdkResult {
50+
fn shutdown_with_timeout(&self, _timeout: time::Duration) -> OTelSdkResult {
5051
let mut client_guard = self.client.lock().map_err(|e| {
5152
OTelSdkError::InternalFailure(format!("Failed to acquire client lock: {}", e))
5253
})?;
@@ -58,6 +59,10 @@ impl LogExporter for OtlpHttpClient {
5859
Ok(())
5960
}
6061

62+
fn shutdown(&self) -> OTelSdkResult {
63+
self.shutdown_with_timeout(time::Duration::from_secs(5))
64+
}
65+
6166
fn set_resource(&mut self, resource: &opentelemetry_sdk::Resource) {
6267
self.resource = resource.into();
6368
}

opentelemetry-otlp/src/exporter/tonic/logs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use opentelemetry_proto::tonic::collector::logs::v1::{
55
};
66
use opentelemetry_sdk::error::{OTelSdkError, OTelSdkResult};
77
use opentelemetry_sdk::logs::{LogBatch, LogExporter};
8+
use std::time;
89
use tokio::sync::Mutex;
910
use tonic::{codegen::CompressionEncoding, service::Interceptor, transport::Channel, Request};
1011

@@ -84,7 +85,7 @@ impl LogExporter for TonicLogsClient {
8485
Ok(())
8586
}
8687

87-
fn shutdown(&self) -> OTelSdkResult {
88+
fn shutdown_with_timeout(&self, _timeout: time::Duration) -> OTelSdkResult {
8889
// TODO: Implement actual shutdown
8990
// Due to the use of tokio::sync::Mutex to guard
9091
// the inner client, we need to await the call to lock the mutex

opentelemetry-otlp/src/logs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
55
#[cfg(feature = "grpc-tonic")]
66
use opentelemetry::otel_debug;
7-
use std::fmt::Debug;
8-
97
use opentelemetry_sdk::{error::OTelSdkResult, logs::LogBatch};
8+
use std::fmt::Debug;
9+
use std::time;
1010

1111
use crate::{ExporterBuildError, HasExportConfig, NoExporterBuilderSet};
1212

@@ -157,7 +157,7 @@ impl opentelemetry_sdk::logs::LogExporter for LogExporter {
157157
}
158158
}
159159

160-
fn shutdown(&self) -> OTelSdkResult {
160+
fn shutdown_with_timeout(&self, _timeout: time::Duration) -> OTelSdkResult {
161161
match &self.client {
162162
#[cfg(feature = "grpc-tonic")]
163163
SupportedTransportClient::Tonic(client) => client.shutdown(),

opentelemetry-sdk/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ also modified to suppress telemetry before invoking exporters.
2424
- *Breaking* change for custom `MetricReader` authors.
2525
The `shutdown_with_timeout` method is added to `MetricReader` trait.
2626
`collect` method on `MetricReader` modified to return `OTelSdkResult`.
27-
27+
- *Breaking* The `shutdown_with_timeout` method is added to LogExporter trait. This is breaking change for custom `LogExporter` authors.
28+
-
2829
## 0.29.0
2930

3031
Released 2025-Mar-21

opentelemetry-sdk/benches/log_enabled.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use opentelemetry_sdk::logs::{
2121
use opentelemetry_sdk::Resource;
2222
#[cfg(not(target_os = "windows"))]
2323
use pprof::criterion::{Output, PProfProfiler};
24+
use std::time;
2425

2526
#[derive(Debug)]
2627
struct NoopExporter;
@@ -29,7 +30,7 @@ impl LogExporter for NoopExporter {
2930
Ok(())
3031
}
3132

32-
fn shutdown(&self) -> OTelSdkResult {
33+
fn shutdown_with_timeout(&self, _timeout: time::Duration) -> OTelSdkResult {
3334
Ok(())
3435
}
3536

opentelemetry-sdk/src/logs/export.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::Resource;
66
use opentelemetry::logs::Severity;
77
use opentelemetry::InstrumentationScope;
88
use std::fmt::Debug;
9+
use std::time;
910

1011
/// A batch of log records to be exported by a `LogExporter`.
1112
///
@@ -134,10 +135,11 @@ pub trait LogExporter: Send + Sync + Debug {
134135
&self,
135136
batch: LogBatch<'_>,
136137
) -> impl std::future::Future<Output = OTelSdkResult> + Send;
137-
138138
/// Shuts down the exporter.
139+
fn shutdown_with_timeout(&self, _timeout: time::Duration) -> OTelSdkResult;
140+
/// Shuts down the exporter with a default timeout.
139141
fn shutdown(&self) -> OTelSdkResult {
140-
Ok(())
142+
self.shutdown_with_timeout(time::Duration::from_secs(5))
141143
}
142144
#[cfg(feature = "spec_unstable_logs_enabled")]
143145
/// Check if logs are enabled.

opentelemetry-sdk/src/logs/in_memory_exporter.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use opentelemetry::InstrumentationScope;
77
use std::borrow::Cow;
88
use std::sync::atomic::AtomicBool;
99
use std::sync::{Arc, Mutex};
10+
use std::time;
1011

1112
/// An in-memory logs exporter that stores logs data in memory..
1213
///
@@ -205,7 +206,7 @@ impl LogExporter for InMemoryLogExporter {
205206
Ok(())
206207
}
207208

208-
fn shutdown(&self) -> OTelSdkResult {
209+
fn shutdown_with_timeout(&self, _timeout: time::Duration) -> OTelSdkResult {
209210
self.shutdown_called
210211
.store(true, std::sync::atomic::Ordering::Relaxed);
211212
if self.should_reset_on_shutdown {
@@ -214,6 +215,10 @@ impl LogExporter for InMemoryLogExporter {
214215
Ok(())
215216
}
216217

218+
fn shutdown(&self) -> OTelSdkResult {
219+
self.shutdown_with_timeout(time::Duration::from_secs(5))
220+
}
221+
217222
fn set_resource(&mut self, resource: &Resource) {
218223
let mut res_guard = self.resource.lock().expect("Resource lock poisoned");
219224
*res_guard = resource.clone();

opentelemetry-sdk/src/logs/log_processor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub(crate) mod tests {
8181
use opentelemetry::logs::{Logger, LoggerProvider};
8282
use opentelemetry::{InstrumentationScope, Key};
8383
use std::sync::{Arc, Mutex};
84+
use std::time;
8485

8586
#[derive(Debug, Clone)]
8687
pub(crate) struct MockLogExporter {
@@ -92,7 +93,7 @@ pub(crate) mod tests {
9293
Ok(())
9394
}
9495

95-
fn shutdown(&self) -> OTelSdkResult {
96+
fn shutdown_with_timeout(&self, _timeout: time::Duration) -> OTelSdkResult {
9697
Ok(())
9798
}
9899

opentelemetry-sdk/src/logs/log_processor_with_async_runtime.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ mod tests {
309309
use opentelemetry::KeyValue;
310310
use opentelemetry::{InstrumentationScope, Key};
311311
use std::sync::{Arc, Mutex};
312+
use std::time;
312313
use std::time::Duration;
313314

314315
#[derive(Debug, Clone)]
@@ -321,10 +322,14 @@ mod tests {
321322
Ok(())
322323
}
323324

324-
fn shutdown(&self) -> OTelSdkResult {
325+
fn shutdown_with_timeout(&self, _timeout: time::Duration) -> OTelSdkResult {
325326
Ok(())
326327
}
327328

329+
fn shutdown(&self) -> OTelSdkResult {
330+
self.shutdown_with_timeout(time::Duration::from_secs(5))
331+
}
332+
328333
fn set_resource(&mut self, resource: &Resource) {
329334
self.resource
330335
.lock()

opentelemetry-sdk/src/logs/logger_provider.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ mod tests {
291291
use std::fmt::{Debug, Formatter};
292292
use std::sync::atomic::AtomicU64;
293293
use std::sync::Mutex;
294-
use std::thread;
294+
use std::{thread, time};
295295

296296
struct ShutdownTestLogProcessor {
297297
is_shutdown: Arc<Mutex<bool>>,
@@ -364,7 +364,7 @@ mod tests {
364364
*res = resource.clone();
365365
}
366366

367-
fn shutdown(&self) -> OTelSdkResult {
367+
fn shutdown_with_timeout(&self, _timeout: time::Duration) -> OTelSdkResult {
368368
Ok(())
369369
}
370370
}

0 commit comments

Comments
 (0)