@@ -22,6 +22,7 @@ use std::{
22
22
sync:: Arc ,
23
23
time:: Duration ,
24
24
} ;
25
+ use std:: borrow:: Cow ;
25
26
26
27
/// Delay interval between two consecutive exports.
27
28
const OTEL_BLRP_SCHEDULE_DELAY : & str = "OTEL_BLRP_SCHEDULE_DELAY" ;
@@ -45,7 +46,7 @@ const OTEL_BLRP_MAX_EXPORT_BATCH_SIZE_DEFAULT: usize = 512;
45
46
/// [`Logger`]: crate::logs::Logger
46
47
pub trait LogProcessor : Send + Sync + Debug {
47
48
/// Called when a log record is ready to processed and exported.
48
- fn emit ( & self , data : LogData ) ;
49
+ fn emit ( & self , data : & mut LogData ) ;
49
50
/// Force the logs lying in the cache to be exported.
50
51
fn force_flush ( & self ) -> LogResult < ( ) > ;
51
52
/// Shuts down the processor.
@@ -80,7 +81,7 @@ impl SimpleLogProcessor {
80
81
}
81
82
82
83
impl LogProcessor for SimpleLogProcessor {
83
- fn emit ( & self , data : LogData ) {
84
+ fn emit ( & self , data : & mut LogData ) {
84
85
// noop after shutdown
85
86
if self . is_shutdown . load ( std:: sync:: atomic:: Ordering :: Relaxed ) {
86
87
return ;
@@ -90,7 +91,7 @@ impl LogProcessor for SimpleLogProcessor {
90
91
. exporter
91
92
. lock ( )
92
93
. map_err ( |_| LogError :: Other ( "simple logprocessor mutex poison" . into ( ) ) )
93
- . and_then ( |mut exporter| futures_executor:: block_on ( exporter. export ( vec ! [ data] ) ) ) ;
94
+ . and_then ( |mut exporter| futures_executor:: block_on ( exporter. export ( vec ! [ Cow :: Borrowed ( data) ] ) ) ) ;
94
95
if let Err ( err) = result {
95
96
global:: handle_error ( err) ;
96
97
}
@@ -140,8 +141,8 @@ impl<R: RuntimeChannel> Debug for BatchLogProcessor<R> {
140
141
}
141
142
142
143
impl < R : RuntimeChannel > LogProcessor for BatchLogProcessor < R > {
143
- fn emit ( & self , data : LogData ) {
144
- let result = self . message_sender . try_send ( BatchMessage :: ExportLog ( data) ) ;
144
+ fn emit ( & self , data : & mut LogData ) {
145
+ let result = self . message_sender . try_send ( BatchMessage :: ExportLog ( data. clone ( ) ) ) ;
145
146
146
147
if let Err ( err) = result {
147
148
global:: handle_error ( LogError :: Other ( err. into ( ) ) ) ;
@@ -201,7 +202,7 @@ impl<R: RuntimeChannel> BatchLogProcessor<R> {
201
202
match message {
202
203
// Log has finished, add to buffer of pending logs.
203
204
BatchMessage :: ExportLog ( log) => {
204
- logs. push ( log) ;
205
+ logs. push ( Cow :: Owned ( log) ) ;
205
206
206
207
if logs. len ( ) == config. max_export_batch_size {
207
208
let result = export_with_timeout (
@@ -285,11 +286,11 @@ impl<R: RuntimeChannel> BatchLogProcessor<R> {
285
286
}
286
287
}
287
288
288
- async fn export_with_timeout < R , E > (
289
+ async fn export_with_timeout < ' a , R , E > (
289
290
time_out : Duration ,
290
291
exporter : & mut E ,
291
292
runtime : & R ,
292
- batch : Vec < LogData > ,
293
+ batch : Vec < Cow < ' a , LogData > > ,
293
294
) -> ExportResult
294
295
where
295
296
R : RuntimeChannel ,
@@ -496,6 +497,7 @@ mod tests {
496
497
OTEL_BLRP_MAX_QUEUE_SIZE , OTEL_BLRP_SCHEDULE_DELAY ,
497
498
} ;
498
499
use crate :: testing:: logs:: InMemoryLogsExporterBuilder ;
500
+ use std:: borrow:: Cow ;
499
501
use crate :: {
500
502
export:: logs:: { LogData , LogExporter } ,
501
503
logs:: {
@@ -522,7 +524,7 @@ mod tests {
522
524
523
525
#[ async_trait]
524
526
impl LogExporter for MockLogExporter {
525
- async fn export ( & mut self , _batch : Vec < LogData > ) -> LogResult < ( ) > {
527
+ async fn export < ' a > ( & mut self , _batch : Vec < Cow < ' a , LogData > > ) -> LogResult < ( ) > {
526
528
Ok ( ( ) )
527
529
}
528
530
@@ -744,17 +746,15 @@ mod tests {
744
746
BatchConfig :: default ( ) ,
745
747
runtime:: Tokio ,
746
748
) ;
747
- processor . emit ( LogData {
749
+ let mut log_data = LogData {
748
750
record : Default :: default ( ) ,
749
751
instrumentation : Default :: default ( ) ,
750
- } ) ;
752
+ } ;
753
+ processor. emit ( & mut log_data) ;
751
754
processor. force_flush ( ) . unwrap ( ) ;
752
755
processor. shutdown ( ) . unwrap ( ) ;
753
756
// todo: expect to see errors here. How should we assert this?
754
- processor. emit ( LogData {
755
- record : Default :: default ( ) ,
756
- instrumentation : Default :: default ( ) ,
757
- } ) ;
757
+ processor. emit ( & mut log_data) ;
758
758
assert_eq ! ( 1 , exporter. get_emitted_logs( ) . unwrap( ) . len( ) )
759
759
}
760
760
@@ -765,10 +765,12 @@ mod tests {
765
765
. build ( ) ;
766
766
let processor = SimpleLogProcessor :: new ( Box :: new ( exporter. clone ( ) ) ) ;
767
767
768
- processor . emit ( LogData {
768
+ let mut log_data = LogData {
769
769
record : Default :: default ( ) ,
770
770
instrumentation : Default :: default ( ) ,
771
- } ) ;
771
+ } ;
772
+
773
+ processor. emit ( & mut log_data) ;
772
774
773
775
processor. shutdown ( ) . unwrap ( ) ;
774
776
@@ -777,10 +779,7 @@ mod tests {
777
779
. load ( std:: sync:: atomic:: Ordering :: Relaxed ) ;
778
780
assert ! ( is_shutdown) ;
779
781
780
- processor. emit ( LogData {
781
- record : Default :: default ( ) ,
782
- instrumentation : Default :: default ( ) ,
783
- } ) ;
782
+ processor. emit ( & mut log_data) ;
784
783
785
784
assert_eq ! ( 1 , exporter. get_emitted_logs( ) . unwrap( ) . len( ) )
786
785
}
0 commit comments