Skip to content

Commit 27d364b

Browse files
authored
Fix: BatchLogProcessor to invoke shutdown on exporter (#2696)
1 parent cb81eb6 commit 27d364b

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

opentelemetry-sdk/CHANGELOG.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,25 @@
3333
}
3434
}
3535
```
36+
3637
- **Breaking** The SpanExporter::export() method no longer requires a mutable reference to self.
37-
Before:
38+
Before:
39+
3840
```rust
3941
async fn export(&mut self, batch: Vec<SpanData>) -> OTelSdkResult
4042
```
41-
After:
42-
```rust
43+
44+
After:
45+
46+
```rust
4347
async fn export(&self, batch: Vec<SpanData>) -> OTelSdkResult
4448
```
49+
4550
Custom exporters will need to internally synchronize any mutable state, if applicable.
46-
51+
52+
- Bug Fix: `BatchLogProcessor` now correctly calls `shutdown` on the exporter
53+
when its `shutdown` is invoked.
54+
4755
## 0.28.0
4856

4957
Released 2025-Feb-10

opentelemetry-sdk/src/logs/batch_log_processor.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ impl BatchLogProcessor {
436436
&current_batch_size,
437437
&config,
438438
);
439+
let _ = exporter.shutdown();
439440
let _ = sender.send(result);
440441

441442
otel_debug!(
@@ -925,7 +926,8 @@ mod tests {
925926
processor.shutdown().unwrap();
926927
// todo: expect to see errors here. How should we assert this?
927928
processor.emit(&mut record, &instrumentation);
928-
assert_eq!(1, exporter.get_emitted_logs().unwrap().len())
929+
assert_eq!(1, exporter.get_emitted_logs().unwrap().len());
930+
assert!(exporter.is_shutdown_called());
929931
}
930932

931933
#[tokio::test(flavor = "current_thread")]

opentelemetry-sdk/src/logs/in_memory_exporter.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::logs::{LogBatch, LogExporter};
44
use crate::Resource;
55
use opentelemetry::InstrumentationScope;
66
use std::borrow::Cow;
7+
use std::sync::atomic::AtomicBool;
78
use std::sync::{Arc, Mutex};
89

910
type LogResult<T> = Result<T, OTelSdkError>;
@@ -42,6 +43,7 @@ pub struct InMemoryLogExporter {
4243
logs: Arc<Mutex<Vec<OwnedLogData>>>,
4344
resource: Arc<Mutex<Resource>>,
4445
should_reset_on_shutdown: bool,
46+
shutdown_called: Arc<AtomicBool>,
4547
}
4648

4749
impl Default for InMemoryLogExporter {
@@ -124,6 +126,7 @@ impl InMemoryLogExporterBuilder {
124126
logs: Arc::new(Mutex::new(Vec::new())),
125127
resource: Arc::new(Mutex::new(Resource::builder().build())),
126128
should_reset_on_shutdown: self.reset_on_shutdown,
129+
shutdown_called: Arc::new(AtomicBool::new(false)),
127130
}
128131
}
129132

@@ -137,6 +140,12 @@ impl InMemoryLogExporterBuilder {
137140
}
138141

139142
impl InMemoryLogExporter {
143+
/// Returns true if shutdown was called.
144+
pub fn is_shutdown_called(&self) -> bool {
145+
self.shutdown_called
146+
.load(std::sync::atomic::Ordering::Relaxed)
147+
}
148+
140149
/// Returns the logs emitted via Logger as a vector of `LogDataWithResource`.
141150
///
142151
/// # Example
@@ -203,6 +212,8 @@ impl LogExporter for InMemoryLogExporter {
203212
}
204213

205214
fn shutdown(&mut self) -> OTelSdkResult {
215+
self.shutdown_called
216+
.store(true, std::sync::atomic::Ordering::Relaxed);
206217
if self.should_reset_on_shutdown {
207218
self.reset();
208219
}

opentelemetry-sdk/src/logs/simple_log_processor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ mod tests {
223223

224224
processor.emit(&mut record, &instrumentation);
225225

226-
assert_eq!(1, exporter.get_emitted_logs().unwrap().len())
226+
assert_eq!(1, exporter.get_emitted_logs().unwrap().len());
227+
assert!(exporter.is_shutdown_called());
227228
}
228229

229230
#[test]

0 commit comments

Comments
 (0)