@@ -55,7 +55,7 @@ pub trait LogProcessor: Send + Sync + Debug {
5555 ///
5656 /// # Parameters
5757 /// - `data`: A mutable reference to `LogData` representing the log record.
58- fn emit ( & self , data : & mut LogData ) ;
58+ fn emit ( & self , data : & mut LogData < ' _ > ) ;
5959 /// Force the logs lying in the cache to be exported.
6060 fn force_flush ( & self ) -> LogResult < ( ) > ;
6161 /// Shuts down the processor.
@@ -90,7 +90,7 @@ impl SimpleLogProcessor {
9090}
9191
9292impl LogProcessor for SimpleLogProcessor {
93- fn emit ( & self , data : & mut LogData ) {
93+ fn emit ( & self , data : & mut LogData < ' _ > ) {
9494 // noop after shutdown
9595 if self . is_shutdown . load ( std:: sync:: atomic:: Ordering :: Relaxed ) {
9696 return ;
@@ -152,10 +152,14 @@ impl<R: RuntimeChannel> Debug for BatchLogProcessor<R> {
152152}
153153
154154impl < R : RuntimeChannel > LogProcessor for BatchLogProcessor < R > {
155- fn emit ( & self , data : & mut LogData ) {
155+ fn emit ( & self , data : & mut LogData < ' _ > ) {
156+ let owned_data = LogData {
157+ record : Cow :: Owned ( data. record . clone ( ) . into_owned ( ) ) ,
158+ instrumentation : Cow :: Owned ( data. instrumentation . clone ( ) . into_owned ( ) ) ,
159+ } ;
156160 let result = self
157161 . message_sender
158- . try_send ( BatchMessage :: ExportLog ( data . clone ( ) ) ) ;
162+ . try_send ( BatchMessage :: ExportLog ( owned_data ) ) ;
159163
160164 if let Err ( err) = result {
161165 global:: handle_error ( LogError :: Other ( err. into ( ) ) ) ;
@@ -307,7 +311,7 @@ async fn export_with_timeout<'a, R, E>(
307311 time_out : Duration ,
308312 exporter : & mut E ,
309313 runtime : & R ,
310- batch : Vec < Cow < ' a , LogData > > ,
314+ batch : Vec < Cow < ' a , LogData < ' a > > > ,
311315) -> ExportResult
312316where
313317 R : RuntimeChannel ,
@@ -497,7 +501,7 @@ where
497501#[ derive( Debug ) ]
498502enum BatchMessage {
499503 /// Export logs, usually called when the log is emitted.
500- ExportLog ( LogData ) ,
504+ ExportLog ( LogData < ' static > ) ,
501505 /// Flush the current buffer to the backend, it can be triggered by
502506 /// pre configured interval or a call to `force_push` function.
503507 Flush ( Option < oneshot:: Sender < ExportResult > > ) ,
@@ -545,7 +549,7 @@ mod tests {
545549
546550 #[ async_trait]
547551 impl LogExporter for MockLogExporter {
548- async fn export < ' a > ( & mut self , _batch : Vec < Cow < ' a , LogData > > ) -> LogResult < ( ) > {
552+ async fn export < ' a > ( & mut self , _batch : Vec < Cow < ' a , LogData < ' a > > > ) -> LogResult < ( ) > {
549553 Ok ( ( ) )
550554 }
551555
@@ -814,21 +818,29 @@ mod tests {
814818
815819 #[ derive( Debug ) ]
816820 struct FirstProcessor {
817- pub ( crate ) logs : Arc < Mutex < Vec < LogData > > > ,
821+ pub ( crate ) logs : Arc < Mutex < Vec < LogData < ' static > > > > ,
818822 }
819823
820824 impl LogProcessor for FirstProcessor {
821- fn emit ( & self , data : & mut LogData ) {
825+ fn emit ( & self , data : & mut LogData < ' _ > ) {
826+ // Ensure the record is owned before modifying
827+ let record = data. record . to_mut ( ) ;
822828 // Add attribute
823- data . record . add_attribute (
829+ record. add_attribute (
824830 Key :: from_static_str ( "processed_by" ) ,
825831 AnyValue :: String ( "FirstProcessor" . into ( ) ) ,
826832 ) ;
827833
828834 // Update body
829- data. record . body = Some ( AnyValue :: String ( "Updated by FirstProcessor" . into ( ) ) ) ;
835+ record. body = Some ( AnyValue :: String ( "Updated by FirstProcessor" . into ( ) ) ) ;
836+
837+ // Convert the modified LogData to an owned version
838+ let owned_data = LogData {
839+ record : Cow :: Owned ( record. clone ( ) ) , // Since record is already owned, no need to clone deeply
840+ instrumentation : Cow :: Owned ( data. instrumentation . clone ( ) . into_owned ( ) ) ,
841+ } ;
830842
831- self . logs . lock ( ) . unwrap ( ) . push ( data . clone ( ) ) ; // Clone as the LogProcessor is storing the data.
843+ self . logs . lock ( ) . unwrap ( ) . push ( owned_data ) ; // Clone as the LogProcessor is storing the data.
832844 }
833845
834846 #[ cfg( feature = "logs_level_enabled" ) ]
@@ -847,11 +859,11 @@ mod tests {
847859
848860 #[ derive( Debug ) ]
849861 struct SecondProcessor {
850- pub ( crate ) logs : Arc < Mutex < Vec < LogData > > > ,
862+ pub ( crate ) logs : Arc < Mutex < Vec < LogData < ' static > > > > ,
851863 }
852864
853865 impl LogProcessor for SecondProcessor {
854- fn emit ( & self , data : & mut LogData ) {
866+ fn emit ( & self , data : & mut LogData < ' _ > ) {
855867 assert ! ( data. record. attributes_contains(
856868 & Key :: from_static_str( "processed_by" ) ,
857869 & AnyValue :: String ( "FirstProcessor" . into( ) )
@@ -860,7 +872,13 @@ mod tests {
860872 data. record. body. clone( ) . unwrap( )
861873 == AnyValue :: String ( "Updated by FirstProcessor" . into( ) )
862874 ) ;
863- self . logs . lock ( ) . unwrap ( ) . push ( data. clone ( ) ) ;
875+ // Convert the modified LogData to an owned version before storing it
876+ let record = data. record . to_mut ( ) ;
877+ let owned_data = LogData {
878+ record : Cow :: Owned ( record. clone ( ) ) , // Convert the record to owned
879+ instrumentation : Cow :: Owned ( data. instrumentation . clone ( ) . into_owned ( ) ) ,
880+ } ;
881+ self . logs . lock ( ) . unwrap ( ) . push ( owned_data) ;
864882 }
865883
866884 #[ cfg( feature = "logs_level_enabled" ) ]
0 commit comments