Skip to content

Commit 86dfd4b

Browse files
cijothomaslalitbCopilot
authored
fix: BatchSpanProcessor implementation to match that of logs (#3116)
Co-authored-by: Lalit Kumar Bhasin <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 7acd380 commit 86dfd4b

File tree

3 files changed

+197
-108
lines changed

3 files changed

+197
-108
lines changed

opentelemetry-sdk/src/logs/in_memory_exporter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ impl InMemoryLogExporterBuilder {
132132

133133
/// If set, the records will not be [`InMemoryLogExporter::reset`] on shutdown.
134134
#[cfg(test)]
135+
#[allow(dead_code)]
135136
pub(crate) fn keep_records_on_shutdown(self) -> Self {
136137
Self {
137138
reset_on_shutdown: false,

opentelemetry-sdk/src/trace/in_memory_exporter.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::error::{OTelSdkError, OTelSdkResult};
22
use crate::resource::Resource;
33
use crate::trace::{SpanData, SpanExporter};
44
use crate::InMemoryExporterError;
5-
use std::sync::{Arc, Mutex};
5+
use std::sync::{atomic::AtomicBool, Arc, Mutex};
66
use std::time::Duration;
77

88
/// An in-memory span exporter that stores span data in memory.
@@ -51,6 +51,8 @@ use std::time::Duration;
5151
pub struct InMemorySpanExporter {
5252
spans: Arc<Mutex<Vec<SpanData>>>,
5353
resource: Arc<Mutex<Resource>>,
54+
should_reset_on_shutdown: bool,
55+
shutdown_called: Arc<AtomicBool>,
5456
}
5557

5658
impl Default for InMemorySpanExporter {
@@ -67,7 +69,9 @@ impl Default for InMemorySpanExporter {
6769
/// let exporter = InMemorySpanExporterBuilder::new().build();
6870
/// ```
6971
#[derive(Clone, Debug)]
70-
pub struct InMemorySpanExporterBuilder {}
72+
pub struct InMemorySpanExporterBuilder {
73+
reset_on_shutdown: bool,
74+
}
7175

7276
impl Default for InMemorySpanExporterBuilder {
7377
fn default() -> Self {
@@ -78,19 +82,38 @@ impl Default for InMemorySpanExporterBuilder {
7882
impl InMemorySpanExporterBuilder {
7983
/// Creates a new instance of the `InMemorySpanExporterBuilder`.
8084
pub fn new() -> Self {
81-
Self {}
85+
Self {
86+
reset_on_shutdown: true,
87+
}
8288
}
8389

8490
/// Creates a new instance of the `InMemorySpanExporter`.
8591
pub fn build(&self) -> InMemorySpanExporter {
8692
InMemorySpanExporter {
8793
spans: Arc::new(Mutex::new(Vec::new())),
8894
resource: Arc::new(Mutex::new(Resource::builder().build())),
95+
should_reset_on_shutdown: self.reset_on_shutdown,
96+
shutdown_called: Arc::new(AtomicBool::new(false)),
97+
}
98+
}
99+
100+
/// If set, the records will not be [`InMemorySpanExporter::reset`] on shutdown.
101+
#[cfg(test)]
102+
#[allow(dead_code)]
103+
pub(crate) fn keep_records_on_shutdown(self) -> Self {
104+
Self {
105+
reset_on_shutdown: false,
89106
}
90107
}
91108
}
92109

93110
impl InMemorySpanExporter {
111+
/// Returns true if shutdown was called.
112+
pub fn is_shutdown_called(&self) -> bool {
113+
self.shutdown_called
114+
.load(std::sync::atomic::Ordering::Relaxed)
115+
}
116+
94117
/// Returns the finished span as a vector of `SpanData`.
95118
///
96119
/// # Errors
@@ -140,7 +163,11 @@ impl SpanExporter for InMemorySpanExporter {
140163
}
141164

142165
fn shutdown_with_timeout(&mut self, _timeout: Duration) -> OTelSdkResult {
143-
self.reset();
166+
self.shutdown_called
167+
.store(true, std::sync::atomic::Ordering::Relaxed);
168+
if self.should_reset_on_shutdown {
169+
self.reset();
170+
}
144171
Ok(())
145172
}
146173

0 commit comments

Comments
 (0)