@@ -2,7 +2,7 @@ use crate::error::{OTelSdkError, OTelSdkResult};
2
2
use crate :: resource:: Resource ;
3
3
use crate :: trace:: { SpanData , SpanExporter } ;
4
4
use crate :: InMemoryExporterError ;
5
- use std:: sync:: { Arc , Mutex } ;
5
+ use std:: sync:: { atomic :: AtomicBool , Arc , Mutex } ;
6
6
use std:: time:: Duration ;
7
7
8
8
/// An in-memory span exporter that stores span data in memory.
@@ -51,6 +51,8 @@ use std::time::Duration;
51
51
pub struct InMemorySpanExporter {
52
52
spans : Arc < Mutex < Vec < SpanData > > > ,
53
53
resource : Arc < Mutex < Resource > > ,
54
+ should_reset_on_shutdown : bool ,
55
+ shutdown_called : Arc < AtomicBool > ,
54
56
}
55
57
56
58
impl Default for InMemorySpanExporter {
@@ -67,7 +69,9 @@ impl Default for InMemorySpanExporter {
67
69
/// let exporter = InMemorySpanExporterBuilder::new().build();
68
70
/// ```
69
71
#[ derive( Clone , Debug ) ]
70
- pub struct InMemorySpanExporterBuilder { }
72
+ pub struct InMemorySpanExporterBuilder {
73
+ reset_on_shutdown : bool ,
74
+ }
71
75
72
76
impl Default for InMemorySpanExporterBuilder {
73
77
fn default ( ) -> Self {
@@ -78,19 +82,38 @@ impl Default for InMemorySpanExporterBuilder {
78
82
impl InMemorySpanExporterBuilder {
79
83
/// Creates a new instance of the `InMemorySpanExporterBuilder`.
80
84
pub fn new ( ) -> Self {
81
- Self { }
85
+ Self {
86
+ reset_on_shutdown : true ,
87
+ }
82
88
}
83
89
84
90
/// Creates a new instance of the `InMemorySpanExporter`.
85
91
pub fn build ( & self ) -> InMemorySpanExporter {
86
92
InMemorySpanExporter {
87
93
spans : Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ,
88
94
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 ,
89
106
}
90
107
}
91
108
}
92
109
93
110
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
+
94
117
/// Returns the finished span as a vector of `SpanData`.
95
118
///
96
119
/// # Errors
@@ -140,7 +163,11 @@ impl SpanExporter for InMemorySpanExporter {
140
163
}
141
164
142
165
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
+ }
144
171
Ok ( ( ) )
145
172
}
146
173
0 commit comments