@@ -31,8 +31,9 @@ use opentelemetry_sdk::logs::LogRecord;
3131use opentelemetry_sdk:: logs:: LoggerProvider ;
3232
3333use crate :: append:: Append ;
34+ use crate :: diagnostic:: Visitor ;
35+ use crate :: Diagnostic ;
3436use crate :: Layout ;
35- use crate :: Marker ;
3637
3738/// Specifies the wire protocol to use when sending logs to OpenTelemetry.
3839///
@@ -56,7 +57,6 @@ pub struct OpentelemetryLogBuilder {
5657 protocol : Protocol ,
5758 labels : Vec < ( Cow < ' static , str > , Cow < ' static , str > ) > ,
5859 layout : Option < Layout > ,
59- marker : Option < Marker > ,
6060}
6161
6262impl OpentelemetryLogBuilder {
@@ -76,7 +76,6 @@ impl OpentelemetryLogBuilder {
7676 protocol : Protocol :: Grpc ,
7777 labels : vec ! [ ] ,
7878 layout : None ,
79- marker : None ,
8079 }
8180 }
8281
@@ -155,22 +154,6 @@ impl OpentelemetryLogBuilder {
155154 self
156155 }
157156
158- /// Sets the marker for the logs.
159- ///
160- /// # Examples
161- ///
162- /// ```
163- /// use logforth::append::opentelemetry::OpentelemetryLogBuilder;
164- /// use logforth::marker::TraceIdMarker;
165- ///
166- /// let builder = OpentelemetryLogBuilder::new("my_service", "http://localhost:4317");
167- /// builder.marker(TraceIdMarker::default());
168- /// ```
169- pub fn marker ( mut self , marker : impl Into < Marker > ) -> Self {
170- self . marker = Some ( marker. into ( ) ) ;
171- self
172- }
173-
174157 /// Builds the [`OpentelemetryLog`] appender.
175158 ///
176159 /// # Examples
@@ -190,7 +173,6 @@ impl OpentelemetryLogBuilder {
190173 protocol,
191174 labels,
192175 layout,
193- marker,
194176 } = self ;
195177
196178 let collector_timeout =
@@ -224,7 +206,6 @@ impl OpentelemetryLogBuilder {
224206 Ok ( OpentelemetryLog {
225207 name,
226208 layout,
227- marker,
228209 logger,
229210 provider,
230211 } )
@@ -250,61 +231,41 @@ impl OpentelemetryLogBuilder {
250231pub struct OpentelemetryLog {
251232 name : String ,
252233 layout : Option < Layout > ,
253- marker : Option < Marker > ,
254234 logger : opentelemetry_sdk:: logs:: Logger ,
255235 provider : LoggerProvider ,
256236}
257237
258238impl Append for OpentelemetryLog {
259- fn append ( & self , record : & Record ) -> anyhow:: Result < ( ) > {
260- let mut log_record_ = LogRecord :: default ( ) ;
261- log_record_ . observed_timestamp = Some ( SystemTime :: now ( ) ) ;
262- log_record_ . severity_number = Some ( log_level_to_otel_severity ( record. level ( ) ) ) ;
263- log_record_ . severity_text = Some ( record. level ( ) . as_str ( ) ) ;
264- log_record_ . target = Some ( record. target ( ) . to_string ( ) . into ( ) ) ;
265- log_record_ . body = Some ( AnyValue :: Bytes ( Box :: new ( match self . layout . as_ref ( ) {
239+ fn append ( & self , record : & Record , diagnostics : & [ Diagnostic ] ) -> anyhow:: Result < ( ) > {
240+ let mut log_record = LogRecord :: default ( ) ;
241+ log_record . observed_timestamp = Some ( SystemTime :: now ( ) ) ;
242+ log_record . severity_number = Some ( log_level_to_otel_severity ( record. level ( ) ) ) ;
243+ log_record . severity_text = Some ( record. level ( ) . as_str ( ) ) ;
244+ log_record . target = Some ( record. target ( ) . to_string ( ) . into ( ) ) ;
245+ log_record . body = Some ( AnyValue :: Bytes ( Box :: new ( match self . layout . as_ref ( ) {
266246 None => record. args ( ) . to_string ( ) . into_bytes ( ) ,
267- Some ( layout) => layout. format ( record, self . marker . as_ref ( ) ) ?,
247+ Some ( layout) => layout. format ( record, diagnostics ) ?,
268248 } ) ) ) ;
269249
270250 if let Some ( module_path) = record. module_path ( ) {
271- log_record_ . add_attribute ( "module_path" , module_path. to_string ( ) ) ;
251+ log_record . add_attribute ( "module_path" , module_path. to_string ( ) ) ;
272252 }
273253 if let Some ( file) = record. file ( ) {
274- log_record_ . add_attribute ( "file" , file. to_string ( ) ) ;
254+ log_record . add_attribute ( "file" , file. to_string ( ) ) ;
275255 }
276256 if let Some ( line) = record. line ( ) {
277- log_record_. add_attribute ( "line" , line) ;
278- }
279-
280- struct KvExtractor < ' a > {
281- record : & ' a mut LogRecord ,
282- }
283-
284- impl < ' kvs > log:: kv:: VisitSource < ' kvs > for KvExtractor < ' _ > {
285- fn visit_pair (
286- & mut self ,
287- key : log:: kv:: Key < ' kvs > ,
288- value : log:: kv:: Value < ' kvs > ,
289- ) -> Result < ( ) , log:: kv:: Error > {
290- self . record
291- . add_attribute ( key. to_string ( ) , value. to_string ( ) ) ;
292- Ok ( ( ) )
293- }
257+ log_record. add_attribute ( "line" , line) ;
294258 }
295259
296260 let mut extractor = KvExtractor {
297- record : & mut log_record_ ,
261+ record : & mut log_record ,
298262 } ;
299- record. key_values ( ) . visit ( & mut extractor) . ok ( ) ;
300-
301- if let Some ( marker) = & self . marker {
302- marker. mark ( |key, value| {
303- log_record_. add_attribute ( key. to_string ( ) , value. to_string ( ) ) ;
304- } ) ;
263+ record. key_values ( ) . visit ( & mut extractor) ?;
264+ for d in diagnostics {
265+ d. visit ( & mut extractor) ;
305266 }
306267
307- self . logger . emit ( log_record_ ) ;
268+ self . logger . emit ( log_record ) ;
308269 Ok ( ( ) )
309270 }
310271
@@ -329,3 +290,32 @@ fn log_level_to_otel_severity(level: log::Level) -> opentelemetry::logs::Severit
329290 log:: Level :: Trace => opentelemetry:: logs:: Severity :: Trace ,
330291 }
331292}
293+
294+ struct KvExtractor < ' a > {
295+ record : & ' a mut LogRecord ,
296+ }
297+
298+ impl < ' kvs > log:: kv:: VisitSource < ' kvs > for KvExtractor < ' _ > {
299+ fn visit_pair (
300+ & mut self ,
301+ key : log:: kv:: Key < ' kvs > ,
302+ value : log:: kv:: Value < ' kvs > ,
303+ ) -> Result < ( ) , log:: kv:: Error > {
304+ let key = key. to_string ( ) ;
305+ let value = value. to_string ( ) ;
306+ self . record . add_attribute ( key, value) ;
307+ Ok ( ( ) )
308+ }
309+ }
310+
311+ impl Visitor for KvExtractor < ' _ > {
312+ fn visit < ' k , ' v , K , V > ( & mut self , key : K , value : V )
313+ where
314+ K : Into < Cow < ' k , str > > ,
315+ V : Into < Cow < ' v , str > > ,
316+ {
317+ let key = key. into ( ) . into_owned ( ) ;
318+ let value = value. into ( ) . into_owned ( ) ;
319+ self . record . add_attribute ( key, value) ;
320+ }
321+ }
0 commit comments