@@ -39,6 +39,7 @@ pub struct OpenTelemetryLayer<S, T> {
39
39
location : bool ,
40
40
tracked_inactivity : bool ,
41
41
with_threads : bool ,
42
+ with_level : bool ,
42
43
sem_conv_config : SemConvConfig ,
43
44
get_context : WithContext ,
44
45
_registry : marker:: PhantomData < S > ,
@@ -562,6 +563,7 @@ where
562
563
location : true ,
563
564
tracked_inactivity : true ,
564
565
with_threads : true ,
566
+ with_level : false ,
565
567
sem_conv_config : SemConvConfig {
566
568
error_fields_to_exceptions : true ,
567
569
error_records_to_exceptions : true ,
@@ -614,9 +616,11 @@ where
614
616
location : self . location ,
615
617
tracked_inactivity : self . tracked_inactivity ,
616
618
with_threads : self . with_threads ,
619
+ with_level : self . with_level ,
617
620
sem_conv_config : self . sem_conv_config ,
618
621
get_context : WithContext ( OpenTelemetryLayer :: < S , Tracer > :: get_context) ,
619
622
_registry : self . _registry ,
623
+ // cannot use ``..self` here due to different generics
620
624
}
621
625
}
622
626
@@ -742,6 +746,19 @@ where
742
746
}
743
747
}
744
748
749
+ /// Sets whether or not span metadata should include the `tracing` verbosity level information as a `level` field.
750
+ ///
751
+ /// The level is always added to events, and based on [`OpenTelemetryLayer::with_error_events_to_status`]
752
+ /// error-level events will mark the span status as an error.
753
+ ///
754
+ /// By default, level information is disabled.
755
+ pub fn with_level ( self , level : bool ) -> Self {
756
+ Self {
757
+ with_level : level,
758
+ ..self
759
+ }
760
+ }
761
+
745
762
/// Retrieve the parent OpenTelemetry [`Context`] from the current tracing
746
763
/// [`span`] through the [`Registry`]. This [`Context`] links spans to their
747
764
/// parent for proper hierarchical visualization.
@@ -817,6 +834,9 @@ where
817
834
if self . with_threads {
818
835
extra_attrs += 2 ;
819
836
}
837
+ if self . with_level {
838
+ extra_attrs += 1 ;
839
+ }
820
840
extra_attrs
821
841
}
822
842
}
@@ -895,6 +915,10 @@ where
895
915
}
896
916
}
897
917
918
+ if self . with_level {
919
+ builder_attrs. push ( KeyValue :: new ( "level" , attrs. metadata ( ) . level ( ) . as_str ( ) ) ) ;
920
+ }
921
+
898
922
let mut updates = SpanBuilderUpdates :: default ( ) ;
899
923
attrs. record ( & mut SpanAttributeVisitor {
900
924
span_builder_updates : & mut updates,
@@ -1607,6 +1631,42 @@ mod tests {
1607
1631
assert ! ( !keys. contains( & "thread.id" ) ) ;
1608
1632
}
1609
1633
1634
+ #[ test]
1635
+ fn includes_level ( ) {
1636
+ let tracer = TestTracer ( Arc :: new ( Mutex :: new ( None ) ) ) ;
1637
+ let subscriber = tracing_subscriber:: registry ( )
1638
+ . with ( layer ( ) . with_tracer ( tracer. clone ( ) ) . with_level ( true ) ) ;
1639
+
1640
+ tracing:: subscriber:: with_default ( subscriber, || {
1641
+ tracing:: debug_span!( "request" ) ;
1642
+ } ) ;
1643
+
1644
+ let attributes = tracer. with_data ( |data| data. builder . attributes . as_ref ( ) . unwrap ( ) . clone ( ) ) ;
1645
+ let keys = attributes
1646
+ . iter ( )
1647
+ . map ( |kv| kv. key . as_str ( ) )
1648
+ . collect :: < Vec < & str > > ( ) ;
1649
+ assert ! ( keys. contains( & "level" ) ) ;
1650
+ }
1651
+
1652
+ #[ test]
1653
+ fn excludes_level ( ) {
1654
+ let tracer = TestTracer ( Arc :: new ( Mutex :: new ( None ) ) ) ;
1655
+ let subscriber = tracing_subscriber:: registry ( )
1656
+ . with ( layer ( ) . with_tracer ( tracer. clone ( ) ) . with_level ( false ) ) ;
1657
+
1658
+ tracing:: subscriber:: with_default ( subscriber, || {
1659
+ tracing:: debug_span!( "request" ) ;
1660
+ } ) ;
1661
+
1662
+ let attributes = tracer. with_data ( |data| data. builder . attributes . as_ref ( ) . unwrap ( ) . clone ( ) ) ;
1663
+ let keys = attributes
1664
+ . iter ( )
1665
+ . map ( |kv| kv. key . as_str ( ) )
1666
+ . collect :: < Vec < & str > > ( ) ;
1667
+ assert ! ( !keys. contains( & "level" ) ) ;
1668
+ }
1669
+
1610
1670
#[ test]
1611
1671
fn propagates_error_fields_from_event_to_span ( ) {
1612
1672
let tracer = TestTracer ( Arc :: new ( Mutex :: new ( None ) ) ) ;
0 commit comments