@@ -77,21 +77,21 @@ pub trait LogProcessor: Send + Sync + Debug {
77
77
/// debugging and testing. For scenarios requiring higher
78
78
/// performance/throughput, consider using [BatchLogProcessor].
79
79
#[ derive( Debug ) ]
80
- pub struct SimpleLogProcessor {
81
- exporter : Mutex < Box < dyn LogExporter > > ,
80
+ pub struct SimpleLogProcessor < T : LogExporter > {
81
+ exporter : Mutex < T > ,
82
82
is_shutdown : AtomicBool ,
83
83
}
84
84
85
- impl SimpleLogProcessor {
86
- pub ( crate ) fn new ( exporter : Box < dyn LogExporter > ) -> Self {
85
+ impl < T : LogExporter > SimpleLogProcessor < T > {
86
+ pub ( crate ) fn new ( exporter : T ) -> Self {
87
87
SimpleLogProcessor {
88
88
exporter : Mutex :: new ( exporter) ,
89
89
is_shutdown : AtomicBool :: new ( false ) ,
90
90
}
91
91
}
92
92
}
93
93
94
- impl LogProcessor for SimpleLogProcessor {
94
+ impl < T : LogExporter > LogProcessor for SimpleLogProcessor < T > {
95
95
fn emit ( & self , record : & mut LogRecord , instrumentation : & InstrumentationScope ) {
96
96
// noop after shutdown
97
97
if self . is_shutdown . load ( std:: sync:: atomic:: Ordering :: Relaxed ) {
@@ -152,19 +152,20 @@ impl LogProcessor for SimpleLogProcessor {
152
152
153
153
/// A [`LogProcessor`] that asynchronously buffers log records and reports
154
154
/// them at a pre-configured interval.
155
- pub struct BatchLogProcessor < R : RuntimeChannel > {
155
+ pub struct BatchLogProcessor < T : LogExporter , R : RuntimeChannel > {
156
+ exporter : Mutex < T > ,
156
157
message_sender : R :: Sender < BatchMessage > ,
157
158
}
158
159
159
- impl < R : RuntimeChannel > Debug for BatchLogProcessor < R > {
160
+ impl < T : LogExporter , R : RuntimeChannel > Debug for BatchLogProcessor < T , R > {
160
161
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
161
162
f. debug_struct ( "BatchLogProcessor" )
162
163
. field ( "message_sender" , & self . message_sender )
163
164
. finish ( )
164
165
}
165
166
}
166
167
167
- impl < R : RuntimeChannel > LogProcessor for BatchLogProcessor < R > {
168
+ impl < T : LogExporter , R : RuntimeChannel > LogProcessor for BatchLogProcessor < T , R > {
168
169
fn emit ( & self , record : & mut LogRecord , instrumentation : & InstrumentationScope ) {
169
170
let result = self . message_sender . try_send ( BatchMessage :: ExportLog ( (
170
171
record. clone ( ) ,
@@ -210,10 +211,11 @@ impl<R: RuntimeChannel> LogProcessor for BatchLogProcessor<R> {
210
211
}
211
212
}
212
213
213
- impl < R : RuntimeChannel > BatchLogProcessor < R > {
214
- pub ( crate ) fn new ( mut exporter : Box < dyn LogExporter > , config : BatchConfig , runtime : R ) -> Self {
214
+ impl < T : LogExporter , R : RuntimeChannel > BatchLogProcessor < T , R > {
215
+ pub ( crate ) fn new ( exporter : T , config : BatchConfig , runtime : R ) -> Self {
215
216
let ( message_sender, message_receiver) =
216
217
runtime. batch_message_channel ( config. max_queue_size ) ;
218
+ let exporter = Arc :: new ( Mutex :: new ( exporter) ) ;
217
219
let inner_runtime = runtime. clone ( ) ;
218
220
219
221
// Spawn worker process via user-defined spawn function.
@@ -234,9 +236,10 @@ impl<R: RuntimeChannel> BatchLogProcessor<R> {
234
236
BatchMessage :: ExportLog ( log) => {
235
237
logs. push ( log) ;
236
238
if logs. len ( ) == config. max_export_batch_size {
239
+ let mut locked_exporter = exporter. lock ( ) . unwrap ( ) ; // Get the MutexGuard
237
240
let result = export_with_timeout (
238
241
config. max_export_timeout ,
239
- exporter . as_mut ( ) ,
242
+ & mut * locked_exporter ,
240
243
& timeout_runtime,
241
244
logs. split_off ( 0 ) ,
242
245
)
@@ -252,9 +255,10 @@ impl<R: RuntimeChannel> BatchLogProcessor<R> {
252
255
}
253
256
// Log batch interval time reached or a force flush has been invoked, export current spans.
254
257
BatchMessage :: Flush ( res_channel) => {
258
+ let mut locked_exporter = exporter. lock ( ) . unwrap ( ) ; // Get the MutexGuard
255
259
let result = export_with_timeout (
256
260
config. max_export_timeout ,
257
- exporter . as_mut ( ) ,
261
+ & mut * locked_exporter ,
258
262
& timeout_runtime,
259
263
logs. split_off ( 0 ) ,
260
264
)
@@ -271,15 +275,16 @@ impl<R: RuntimeChannel> BatchLogProcessor<R> {
271
275
}
272
276
// Stream has terminated or processor is shutdown, return to finish execution.
273
277
BatchMessage :: Shutdown ( ch) => {
278
+ let mut locked_exporter = exporter. lock ( ) . unwrap ( ) ;
274
279
let result = export_with_timeout (
275
280
config. max_export_timeout ,
276
- exporter . as_mut ( ) ,
281
+ & mut * locked_exporter ,
277
282
& timeout_runtime,
278
283
logs. split_off ( 0 ) ,
279
284
)
280
285
. await ;
281
286
282
- exporter . shutdown ( ) ;
287
+ locked_exporter . shutdown ( ) ;
283
288
284
289
if let Err ( send_error) = ch. send ( result) {
285
290
otel_debug ! (
@@ -291,7 +296,8 @@ impl<R: RuntimeChannel> BatchLogProcessor<R> {
291
296
}
292
297
// propagate the resource
293
298
BatchMessage :: SetResource ( resource) => {
294
- exporter. set_resource ( & resource) ;
299
+ let mut locked_exporter = exporter. lock ( ) . unwrap ( ) ;
300
+ locked_exporter. set_resource ( & resource) ;
295
301
}
296
302
}
297
303
}
0 commit comments