@@ -240,6 +240,27 @@ impl<C, N, E, W> Subscriber<C, N, E, W> {
240
240
self . is_ansi = ansi;
241
241
}
242
242
243
+ /// Modifies how synthesized events are emitted at points in the [span
244
+ /// lifecycle][lifecycle].
245
+ ///
246
+ /// See [`Self::with_span_events`] for documentation on the [`FmtSpan`]
247
+ ///
248
+ /// This method is primarily expected to be used with the
249
+ /// [`reload::Handle::modify`](crate::reload::Handle::modify) method
250
+ ///
251
+ /// Note that using this method modifies the span configuration instantly and does not take into
252
+ /// account any current spans. If the previous configuration was set to capture
253
+ /// `FmtSpan::ALL`, for example, using this method to change to `FmtSpan::NONE` will cause an
254
+ /// exit event for currently entered events not to be formatted
255
+ ///
256
+ /// [lifecycle]: mod@tracing::span#the-span-lifecycle
257
+ pub fn set_span_events ( & mut self , kind : FmtSpan ) {
258
+ self . fmt_span = format:: FmtSpanConfig {
259
+ kind,
260
+ fmt_timing : self . fmt_span . fmt_timing ,
261
+ }
262
+ }
263
+
243
264
/// Configures the subscriber to support [`libtest`'s output capturing][capturing] when used in
244
265
/// unit tests.
245
266
///
@@ -1590,4 +1611,52 @@ mod test {
1590
1611
// dropping `_saved_no_color` will restore the previous value of
1591
1612
// `NO_COLOR`.
1592
1613
}
1614
+
1615
+ // Validates that span event configuration can be modified with a reload handle
1616
+ #[ test]
1617
+ fn modify_span_events ( ) {
1618
+ let make_writer = MockMakeWriter :: default ( ) ;
1619
+
1620
+ let inner_subscriber = fmt:: Subscriber :: default ( )
1621
+ . with_writer ( make_writer. clone ( ) )
1622
+ . with_level ( false )
1623
+ . with_ansi ( false )
1624
+ . with_timer ( MockTime )
1625
+ . with_span_events ( FmtSpan :: ACTIVE ) ;
1626
+
1627
+ let ( reloadable_subscriber, reload_handle) =
1628
+ crate :: reload:: Subscriber :: new ( inner_subscriber) ;
1629
+ let reload = reloadable_subscriber. with_collector ( Registry :: default ( ) ) ;
1630
+
1631
+ with_default ( reload, || {
1632
+ {
1633
+ let span1 = tracing:: info_span!( "span1" , x = 42 ) ;
1634
+ let _e = span1. enter ( ) ;
1635
+ }
1636
+
1637
+ let _ = reload_handle. modify ( |s| s. set_span_events ( FmtSpan :: NONE ) ) ;
1638
+
1639
+ // this span should not be logged at all!
1640
+ {
1641
+ let span2 = tracing:: info_span!( "span2" , x = 100 ) ;
1642
+ let _e = span2. enter ( ) ;
1643
+ }
1644
+
1645
+ {
1646
+ let span3 = tracing:: info_span!( "span3" , x = 42 ) ;
1647
+ let _e = span3. enter ( ) ;
1648
+
1649
+ // The span config was modified after span3 was already entered.
1650
+ // We should only see an exit
1651
+ let _ = reload_handle. modify ( |s| s. set_span_events ( FmtSpan :: ACTIVE ) ) ;
1652
+ }
1653
+ } ) ;
1654
+ let actual = sanitize_timings ( make_writer. get_string ( ) ) ;
1655
+ assert_eq ! (
1656
+ "fake time span1{x=42}: tracing_subscriber::fmt::fmt_subscriber::test: enter\n \
1657
+ fake time span1{x=42}: tracing_subscriber::fmt::fmt_subscriber::test: exit\n \
1658
+ fake time span3{x=42}: tracing_subscriber::fmt::fmt_subscriber::test: exit\n ",
1659
+ actual. as_str( )
1660
+ ) ;
1661
+ }
1593
1662
}
0 commit comments