@@ -39,6 +39,7 @@ pub struct OpenTelemetryLayer<S, T> {
39
39
tracked_inactivity : bool ,
40
40
with_threads : bool ,
41
41
with_level : bool ,
42
+ with_target : bool ,
42
43
sem_conv_config : SemConvConfig ,
43
44
get_context : WithContext ,
44
45
_registry : marker:: PhantomData < S > ,
@@ -563,6 +564,7 @@ where
563
564
tracked_inactivity : true ,
564
565
with_threads : true ,
565
566
with_level : false ,
567
+ with_target : true ,
566
568
sem_conv_config : SemConvConfig {
567
569
error_fields_to_exceptions : true ,
568
570
error_records_to_exceptions : true ,
@@ -618,6 +620,7 @@ where
618
620
tracked_inactivity : self . tracked_inactivity ,
619
621
with_threads : self . with_threads ,
620
622
with_level : self . with_level ,
623
+ with_target : self . with_target ,
621
624
sem_conv_config : self . sem_conv_config ,
622
625
get_context : WithContext ( OpenTelemetryLayer :: < S , Tracer > :: get_context) ,
623
626
_registry : self . _registry ,
@@ -762,6 +765,16 @@ where
762
765
}
763
766
}
764
767
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
+
765
778
/// Retrieve the parent OpenTelemetry [`Context`] from the current tracing
766
779
/// [`span`] through the [`Registry`]. This [`Context`] links spans to their
767
780
/// parent for proper hierarchical visualization.
@@ -840,6 +853,9 @@ where
840
853
if self . with_level {
841
854
extra_attrs += 1 ;
842
855
}
856
+ if self . with_target {
857
+ extra_attrs += 1 ;
858
+ }
843
859
extra_attrs
844
860
}
845
861
}
@@ -921,6 +937,9 @@ where
921
937
if self . with_level {
922
938
builder_attrs. push ( KeyValue :: new ( "level" , attrs. metadata ( ) . level ( ) . as_str ( ) ) ) ;
923
939
}
940
+ if self . with_target {
941
+ builder_attrs. push ( KeyValue :: new ( "target" , attrs. metadata ( ) . target ( ) ) ) ;
942
+ }
924
943
925
944
let mut updates = SpanBuilderUpdates :: default ( ) ;
926
945
attrs. record ( & mut SpanAttributeVisitor {
@@ -1723,6 +1742,42 @@ mod tests {
1723
1742
assert ! ( !keys. contains( & "level" ) ) ;
1724
1743
}
1725
1744
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
+
1726
1781
#[ test]
1727
1782
fn propagates_error_fields_from_event_to_span ( ) {
1728
1783
let tracer = TestTracer ( Arc :: new ( Mutex :: new ( None ) ) ) ;
0 commit comments