Skip to content

Commit bd1723a

Browse files
lpraneishds
authored andcommitted
subscriber: add set_span_events to fmt::Subscriber (#2962)
1 parent cd06dc8 commit bd1723a

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

tracing-subscriber/src/fmt/fmt_layer.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,27 @@ impl<S, N, E, W> Layer<S, N, E, W> {
241241
self.is_ansi = ansi;
242242
}
243243

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

0 commit comments

Comments
 (0)