@@ -39,6 +39,7 @@ pub struct OpenTelemetryLayer<S, T> {
3939 tracked_inactivity : bool ,
4040 with_threads : bool ,
4141 with_level : bool ,
42+ with_target : bool ,
4243 sem_conv_config : SemConvConfig ,
4344 get_context : WithContext ,
4445 _registry : marker:: PhantomData < S > ,
@@ -563,6 +564,7 @@ where
563564 tracked_inactivity : true ,
564565 with_threads : true ,
565566 with_level : false ,
567+ with_target : true ,
566568 sem_conv_config : SemConvConfig {
567569 error_fields_to_exceptions : true ,
568570 error_records_to_exceptions : true ,
@@ -618,6 +620,7 @@ where
618620 tracked_inactivity : self . tracked_inactivity ,
619621 with_threads : self . with_threads ,
620622 with_level : self . with_level ,
623+ with_target : self . with_target ,
621624 sem_conv_config : self . sem_conv_config ,
622625 get_context : WithContext ( OpenTelemetryLayer :: < S , Tracer > :: get_context) ,
623626 _registry : self . _registry ,
@@ -762,6 +765,16 @@ where
762765 }
763766 }
764767
768+ /// Sets whether or not span metadata should include an attribute with `target` from `tracing` spans.
769+ ///
770+ /// By default, the target attribute is enabled..
771+ pub fn with_target ( self , target : bool ) -> Self {
772+ Self {
773+ with_target : target,
774+ ..self
775+ }
776+ }
777+
765778 /// Retrieve the parent OpenTelemetry [`Context`] from the current tracing
766779 /// [`span`] through the [`Registry`]. This [`Context`] links spans to their
767780 /// parent for proper hierarchical visualization.
@@ -840,6 +853,9 @@ where
840853 if self . with_level {
841854 extra_attrs += 1 ;
842855 }
856+ if self . with_target {
857+ extra_attrs += 1 ;
858+ }
843859 extra_attrs
844860 }
845861}
@@ -921,6 +937,9 @@ where
921937 if self . with_level {
922938 builder_attrs. push ( KeyValue :: new ( "level" , attrs. metadata ( ) . level ( ) . as_str ( ) ) ) ;
923939 }
940+ if self . with_target {
941+ builder_attrs. push ( KeyValue :: new ( "target" , attrs. metadata ( ) . target ( ) ) ) ;
942+ }
924943
925944 let mut updates = SpanBuilderUpdates :: default ( ) ;
926945 attrs. record ( & mut SpanAttributeVisitor {
@@ -1723,6 +1742,42 @@ mod tests {
17231742 assert ! ( !keys. contains( & "level" ) ) ;
17241743 }
17251744
1745+ #[ test]
1746+ fn includes_target ( ) {
1747+ let tracer = TestTracer ( Arc :: new ( Mutex :: new ( None ) ) ) ;
1748+ let subscriber = tracing_subscriber:: registry ( )
1749+ . with ( layer ( ) . with_tracer ( tracer. clone ( ) ) . with_target ( true ) ) ;
1750+
1751+ tracing:: subscriber:: with_default ( subscriber, || {
1752+ tracing:: debug_span!( "request" ) ;
1753+ } ) ;
1754+
1755+ let attributes = tracer. with_data ( |data| data. builder . attributes . as_ref ( ) . unwrap ( ) . clone ( ) ) ;
1756+ let keys = attributes
1757+ . iter ( )
1758+ . map ( |kv| kv. key . as_str ( ) )
1759+ . collect :: < Vec < & str > > ( ) ;
1760+ assert ! ( keys. contains( & "target" ) ) ;
1761+ }
1762+
1763+ #[ test]
1764+ fn excludes_target ( ) {
1765+ let tracer = TestTracer ( Arc :: new ( Mutex :: new ( None ) ) ) ;
1766+ let subscriber = tracing_subscriber:: registry ( )
1767+ . with ( layer ( ) . with_tracer ( tracer. clone ( ) ) . with_target ( false ) ) ;
1768+
1769+ tracing:: subscriber:: with_default ( subscriber, || {
1770+ tracing:: debug_span!( "request" ) ;
1771+ } ) ;
1772+
1773+ let attributes = tracer. with_data ( |data| data. builder . attributes . as_ref ( ) . unwrap ( ) . clone ( ) ) ;
1774+ let keys = attributes
1775+ . iter ( )
1776+ . map ( |kv| kv. key . as_str ( ) )
1777+ . collect :: < Vec < & str > > ( ) ;
1778+ assert ! ( !keys. contains( & "target" ) ) ;
1779+ }
1780+
17261781 #[ test]
17271782 fn propagates_error_fields_from_event_to_span ( ) {
17281783 let tracer = TestTracer ( Arc :: new ( Mutex :: new ( None ) ) ) ;
0 commit comments