@@ -28,7 +28,7 @@ const SPAN_STATUS_MESSAGE_FIELD: &str = "otel.status_message";
28
28
/// [tracing]: https://github.com/tokio-rs/tracing
29
29
pub struct OpenTelemetryLayer < S , T > {
30
30
tracer : T ,
31
- event_location : bool ,
31
+ location : bool ,
32
32
tracked_inactivity : bool ,
33
33
get_context : WithContext ,
34
34
_registry : marker:: PhantomData < S > ,
@@ -312,7 +312,7 @@ where
312
312
pub fn new ( tracer : T ) -> Self {
313
313
OpenTelemetryLayer {
314
314
tracer,
315
- event_location : true ,
315
+ location : true ,
316
316
tracked_inactivity : true ,
317
317
get_context : WithContext ( Self :: get_context) ,
318
318
_registry : marker:: PhantomData ,
@@ -351,20 +351,32 @@ where
351
351
{
352
352
OpenTelemetryLayer {
353
353
tracer,
354
- event_location : self . event_location ,
354
+ location : self . location ,
355
355
tracked_inactivity : self . tracked_inactivity ,
356
356
get_context : WithContext ( OpenTelemetryLayer :: < S , Tracer > :: get_context) ,
357
357
_registry : self . _registry ,
358
358
}
359
359
}
360
360
361
+ /// Sets whether or not span and event metadata should include detailed
362
+ /// location information, such as the file, module and line number.
363
+ ///
364
+ /// By default, locations are enabled.
365
+ pub fn with_location ( self , location : bool ) -> Self {
366
+ Self { location, ..self }
367
+ }
368
+
361
369
/// Sets whether or not event span's metadata should include detailed location
362
370
/// information, such as the file, module and line number.
363
371
///
364
372
/// By default, event locations are enabled.
373
+ #[ deprecated(
374
+ since = "0.17.3" ,
375
+ note = "renamed to `OpenTelemetrySubscriber::with_location`"
376
+ ) ]
365
377
pub fn with_event_location ( self , event_location : bool ) -> Self {
366
378
Self {
367
- event_location,
379
+ location : event_location,
368
380
..self
369
381
}
370
382
}
@@ -467,18 +479,20 @@ where
467
479
. attributes
468
480
. get_or_insert ( Vec :: with_capacity ( attrs. fields ( ) . len ( ) + 3 ) ) ;
469
481
470
- let meta = attrs. metadata ( ) ;
482
+ if self . location {
483
+ let meta = attrs. metadata ( ) ;
471
484
472
- if let Some ( filename) = meta. file ( ) {
473
- builder_attrs. push ( KeyValue :: new ( "code.filepath" , filename) ) ;
474
- }
485
+ if let Some ( filename) = meta. file ( ) {
486
+ builder_attrs. push ( KeyValue :: new ( "code.filepath" , filename) ) ;
487
+ }
475
488
476
- if let Some ( module) = meta. module_path ( ) {
477
- builder_attrs. push ( KeyValue :: new ( "code.namespace" , module) ) ;
478
- }
489
+ if let Some ( module) = meta. module_path ( ) {
490
+ builder_attrs. push ( KeyValue :: new ( "code.namespace" , module) ) ;
491
+ }
479
492
480
- if let Some ( line) = meta. line ( ) {
481
- builder_attrs. push ( KeyValue :: new ( "code.lineno" , line as i64 ) ) ;
493
+ if let Some ( line) = meta. line ( ) {
494
+ builder_attrs. push ( KeyValue :: new ( "code.lineno" , line as i64 ) ) ;
495
+ }
482
496
}
483
497
484
498
attrs. record ( & mut SpanAttributeVisitor ( & mut builder) ) ;
@@ -601,7 +615,7 @@ where
601
615
builder. status_code = Some ( otel:: StatusCode :: Error ) ;
602
616
}
603
617
604
- if self . event_location {
618
+ if self . location {
605
619
#[ cfg( not( feature = "tracing-log" ) ) ]
606
620
let normalized_meta: Option < tracing_core:: Metadata < ' _ > > = None ;
607
621
let ( file, module) = match & normalized_meta {
@@ -999,4 +1013,64 @@ mod tests {
999
1013
)
1000
1014
) ;
1001
1015
}
1016
+
1017
+ #[ test]
1018
+ fn includes_span_location ( ) {
1019
+ let tracer = TestTracer ( Arc :: new ( Mutex :: new ( None ) ) ) ;
1020
+ let subscriber = tracing_subscriber:: registry ( )
1021
+ . with ( layer ( ) . with_tracer ( tracer. clone ( ) ) . with_location ( true ) ) ;
1022
+
1023
+ tracing:: subscriber:: with_default ( subscriber, || {
1024
+ tracing:: debug_span!( "request" ) ;
1025
+ } ) ;
1026
+
1027
+ let attributes = tracer
1028
+ . 0
1029
+ . lock ( )
1030
+ . unwrap ( )
1031
+ . as_ref ( )
1032
+ . unwrap ( )
1033
+ . builder
1034
+ . attributes
1035
+ . as_ref ( )
1036
+ . unwrap ( )
1037
+ . clone ( ) ;
1038
+ let keys = attributes
1039
+ . iter ( )
1040
+ . map ( |attr| attr. key . as_str ( ) )
1041
+ . collect :: < Vec < & str > > ( ) ;
1042
+ assert ! ( keys. contains( & "code.filepath" ) ) ;
1043
+ assert ! ( keys. contains( & "code.namespace" ) ) ;
1044
+ assert ! ( keys. contains( & "code.lineno" ) ) ;
1045
+ }
1046
+
1047
+ #[ test]
1048
+ fn excludes_span_location ( ) {
1049
+ let tracer = TestTracer ( Arc :: new ( Mutex :: new ( None ) ) ) ;
1050
+ let subscriber = tracing_subscriber:: registry ( )
1051
+ . with ( layer ( ) . with_tracer ( tracer. clone ( ) ) . with_location ( false ) ) ;
1052
+
1053
+ tracing:: subscriber:: with_default ( subscriber, || {
1054
+ tracing:: debug_span!( "request" ) ;
1055
+ } ) ;
1056
+
1057
+ let attributes = tracer
1058
+ . 0
1059
+ . lock ( )
1060
+ . unwrap ( )
1061
+ . as_ref ( )
1062
+ . unwrap ( )
1063
+ . builder
1064
+ . attributes
1065
+ . as_ref ( )
1066
+ . unwrap ( )
1067
+ . clone ( ) ;
1068
+ let keys = attributes
1069
+ . iter ( )
1070
+ . map ( |attr| attr. key . as_str ( ) )
1071
+ . collect :: < Vec < & str > > ( ) ;
1072
+ assert ! ( !keys. contains( & "code.filepath" ) ) ;
1073
+ assert ! ( !keys. contains( & "code.namespace" ) ) ;
1074
+ assert ! ( !keys. contains( & "code.lineno" ) ) ;
1075
+ }
1002
1076
}
0 commit comments