1
1
use crate :: { OtelData , PreSampledTracer } ;
2
2
use opentelemetry:: {
3
3
trace:: { self as otel, noop, TraceContextExt } ,
4
- Context as OtelContext , Key , KeyValue ,
4
+ Context as OtelContext , Key , KeyValue , Value ,
5
5
} ;
6
6
use std:: any:: TypeId ;
7
+ #[ cfg( not( feature = "tracing-log" ) ) ]
8
+ use std:: borrow:: Cow ;
7
9
use std:: fmt;
8
10
use std:: marker;
9
11
use std:: time:: { Instant , SystemTime } ;
@@ -27,6 +29,7 @@ const SPAN_STATUS_MESSAGE_FIELD: &str = "otel.status_message";
27
29
/// [tracing]: https://github.com/tokio-rs/tracing
28
30
pub struct OpenTelemetryLayer < S , T > {
29
31
tracer : T ,
32
+ event_location : bool ,
30
33
tracked_inactivity : bool ,
31
34
get_context : WithContext ,
32
35
_registry : marker:: PhantomData < S > ,
@@ -289,6 +292,7 @@ where
289
292
pub fn new ( tracer : T ) -> Self {
290
293
OpenTelemetryLayer {
291
294
tracer,
295
+ event_location : true ,
292
296
tracked_inactivity : true ,
293
297
get_context : WithContext ( Self :: get_context) ,
294
298
_registry : marker:: PhantomData ,
@@ -327,12 +331,24 @@ where
327
331
{
328
332
OpenTelemetryLayer {
329
333
tracer,
334
+ event_location : self . event_location ,
330
335
tracked_inactivity : self . tracked_inactivity ,
331
336
get_context : WithContext ( OpenTelemetryLayer :: < S , Tracer > :: get_context) ,
332
337
_registry : self . _registry ,
333
338
}
334
339
}
335
340
341
+ /// Sets whether or not event span's metadata should include detailed location
342
+ /// information, such as the file, module and line number.
343
+ ///
344
+ /// By default, event locations are enabled.
345
+ pub fn with_event_location ( self , event_location : bool ) -> Self {
346
+ Self {
347
+ event_location,
348
+ ..self
349
+ }
350
+ }
351
+
336
352
/// Sets whether or not spans metadata should include the _busy time_
337
353
/// (total time for which it was entered), and _idle time_ (total time
338
354
/// the span existed but was not entered).
@@ -555,6 +571,33 @@ where
555
571
builder. status_code = Some ( otel:: StatusCode :: Error ) ;
556
572
}
557
573
574
+ if self . event_location {
575
+ let builder_attrs = builder. attributes . get_or_insert ( Vec :: new ( ) ) ;
576
+
577
+ #[ cfg( not( feature = "tracing-log" ) ) ]
578
+ let normalized_meta = None ;
579
+ let ( file, module) = match & normalized_meta {
580
+ Some ( meta) => (
581
+ meta. file ( ) . map ( |s| Value :: from ( s. to_owned ( ) ) ) ,
582
+ meta. module_path ( ) . map ( |s| Value :: from ( s. to_owned ( ) ) ) ,
583
+ ) ,
584
+ None => (
585
+ event. metadata ( ) . file ( ) . map ( Value :: from) ,
586
+ event. metadata ( ) . module_path ( ) . map ( Value :: from) ,
587
+ ) ,
588
+ } ;
589
+
590
+ if let Some ( file) = file {
591
+ builder_attrs. push ( KeyValue :: new ( "code.filepath" , file) ) ;
592
+ }
593
+ if let Some ( module) = module {
594
+ builder_attrs. push ( KeyValue :: new ( "code.namespace" , module) ) ;
595
+ }
596
+ if let Some ( line) = meta. line ( ) {
597
+ builder_attrs. push ( KeyValue :: new ( "code.lineno" , line as i64 ) ) ;
598
+ }
599
+ }
600
+
558
601
if let Some ( ref mut events) = builder. events {
559
602
events. push ( otel_event) ;
560
603
} else {
0 commit comments