Skip to content

Commit

Permalink
Merge branch 'master' into dm/instrument-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbarsky committed May 25, 2024
2 parents 76a4d46 + 382ee01 commit c37fc98
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 170 deletions.
32 changes: 29 additions & 3 deletions tracing-attributes/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub(crate) struct EventArgs {
#[derive(Clone, Default, Debug)]
pub(crate) struct InstrumentArgs {
level: Option<Level>,
pub(crate) name: Option<LitStr>,
target: Option<LitStr>,
pub(crate) name: Option<LitStrOrIdent>,
target: Option<LitStrOrIdent>,
pub(crate) parent: Option<Expr>,
pub(crate) follows_from: Option<Expr>,
pub(crate) skips: HashSet<Ident>,
Expand Down Expand Up @@ -86,6 +86,8 @@ impl Parse for InstrumentArgs {
// XXX: apparently we support names as either named args with an
// sign, _or_ as unnamed string literals. That's weird, but
// changing it is apparently breaking.
// This also means that when using idents for name, it must be via
// a named arg, i.e. `#[instrument(name = SOME_IDENT)]`.
if args.name.is_some() {
return Err(input.error("expected only a single `name` argument"));
}
Expand Down Expand Up @@ -198,8 +200,32 @@ impl Parse for EventArgs {
}
}

#[derive(Debug, Clone)]
pub(super) enum LitStrOrIdent {
LitStr(LitStr),
Ident(Ident),
}

impl ToTokens for LitStrOrIdent {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
LitStrOrIdent::LitStr(target) => target.to_tokens(tokens),
LitStrOrIdent::Ident(ident) => ident.to_tokens(tokens),
}
}
}

impl Parse for LitStrOrIdent {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
input
.parse::<LitStr>()
.map(LitStrOrIdent::LitStr)
.or_else(|_| input.parse::<Ident>().map(LitStrOrIdent::Ident))
}
}

struct StrArg<T> {
value: LitStr,
value: LitStrOrIdent,
_p: std::marker::PhantomData<T>,
}

Expand Down
72 changes: 72 additions & 0 deletions tracing-attributes/tests/instrument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,75 @@ fn impl_trait_return_type() {

handle.assert_finished();
}

#[test]
fn name_ident() {
const MY_NAME: &str = "my_name";
#[instrument(name = MY_NAME)]
fn name() {}

let span_name = expect::span().named(MY_NAME);

let (collector, handle) = collector::mock()
.new_span(span_name.clone())
.enter(span_name.clone())
.exit(span_name.clone())
.drop_span(span_name)
.only()
.run_with_handle();

with_default(collector, || {
name();
});

handle.assert_finished();
}

#[test]
fn target_ident() {
const MY_TARGET: &str = "my_target";

#[instrument(target = MY_TARGET)]
fn target() {}

let span_target = expect::span().named("target").with_target(MY_TARGET);

let (collector, handle) = collector::mock()
.new_span(span_target.clone())
.enter(span_target.clone())
.exit(span_target.clone())
.drop_span(span_target)
.only()
.run_with_handle();

with_default(collector, || {
target();
});

handle.assert_finished();
}

#[test]
fn target_name_ident() {
const MY_NAME: &str = "my_name";
const MY_TARGET: &str = "my_target";

#[instrument(target = MY_TARGET, name = MY_NAME)]
fn name_target() {}

let span_name_target = expect::span().named(MY_NAME).with_target(MY_TARGET);

let (collector, handle) = collector::mock()
.new_span(span_name_target.clone())
.enter(span_name_target.clone())
.exit(span_name_target.clone())
.drop_span(span_name_target)
.only()
.run_with_handle();

with_default(collector, || {
name_target();
});

handle.assert_finished();
}
121 changes: 0 additions & 121 deletions tracing-futures/src/executor/futures_preview.rs

This file was deleted.

5 changes: 0 additions & 5 deletions tracing-futures/src/executor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#[cfg(feature = "futures-01")]
mod futures_01;

#[cfg(feature = "futures_preview")]
mod futures_preview;
#[cfg(feature = "futures_preview")]
pub use self::futures_preview::*;

#[cfg(feature = "futures-03")]
mod futures_03;
#[cfg(feature = "futures-03")]
Expand Down
14 changes: 7 additions & 7 deletions tracing-subscriber/src/filter/env/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,15 @@ impl Builder {
}

if !disabled.is_empty() {
#[cfg(feature = "nu_ansi_term")]
#[cfg(feature = "nu-ansi-term")]
use nu_ansi_term::{Color, Style};
// NOTE: We can't use a configured `MakeWriter` because the EnvFilter
// has no knowledge of any underlying subscriber or collector, which
// may or may not use a `MakeWriter`.
let warn = |msg: &str| {
#[cfg(not(feature = "nu_ansi_term"))]
#[cfg(not(feature = "nu-ansi-term"))]
let msg = format!("warning: {}", msg);
#[cfg(feature = "nu_ansi_term")]
#[cfg(feature = "nu-ansi-term")]
let msg = {
let bold = Style::new().bold();
let mut warning = Color::Yellow.paint("warning");
Expand All @@ -228,9 +228,9 @@ impl Builder {
eprintln!("{}", msg);
};
let ctx_prefixed = |prefix: &str, msg: &str| {
#[cfg(not(feature = "nu_ansi_term"))]
#[cfg(not(feature = "nu-ansi-term"))]
let msg = format!("{} {}", prefix, msg);
#[cfg(feature = "nu_ansi_term")]
#[cfg(feature = "nu-ansi-term")]
let msg = {
let mut equal = Color::Fixed(21).paint("="); // dark blue
equal.style_ref_mut().is_bold = true;
Expand All @@ -241,9 +241,9 @@ impl Builder {
let ctx_help = |msg| ctx_prefixed("help:", msg);
let ctx_note = |msg| ctx_prefixed("note:", msg);
let ctx = |msg: &str| {
#[cfg(not(feature = "nu_ansi_term"))]
#[cfg(not(feature = "nu-ansi-term"))]
let msg = format!("note: {}", msg);
#[cfg(feature = "nu_ansi_term")]
#[cfg(feature = "nu-ansi-term")]
let msg = {
let mut pipe = Color::Fixed(21).paint("|");
pipe.style_ref_mut().is_bold = true;
Expand Down
69 changes: 69 additions & 0 deletions tracing-subscriber/src/fmt/fmt_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,27 @@ impl<C, N, E, W> Subscriber<C, N, E, W> {
self.is_ansi = ansi;
}

/// Modifies how synthesized events are emitted at points in the [span
/// lifecycle][lifecycle].
///
/// See [`Self::with_span_events`] for documentation on the [`FmtSpan`]
///
/// This method is primarily expected to be used with the
/// [`reload::Handle::modify`](crate::reload::Handle::modify) method
///
/// Note that using this method modifies the span configuration instantly and does not take into
/// account any current spans. If the previous configuration was set to capture
/// `FmtSpan::ALL`, for example, using this method to change to `FmtSpan::NONE` will cause an
/// exit event for currently entered events not to be formatted
///
/// [lifecycle]: mod@tracing::span#the-span-lifecycle
pub fn set_span_events(&mut self, kind: FmtSpan) {
self.fmt_span = format::FmtSpanConfig {
kind,
fmt_timing: self.fmt_span.fmt_timing,
}
}

/// Configures the subscriber to support [`libtest`'s output capturing][capturing] when used in
/// unit tests.
///
Expand Down Expand Up @@ -1590,4 +1611,52 @@ mod test {
// dropping `_saved_no_color` will restore the previous value of
// `NO_COLOR`.
}

// Validates that span event configuration can be modified with a reload handle
#[test]
fn modify_span_events() {
let make_writer = MockMakeWriter::default();

let inner_subscriber = fmt::Subscriber::default()
.with_writer(make_writer.clone())
.with_level(false)
.with_ansi(false)
.with_timer(MockTime)
.with_span_events(FmtSpan::ACTIVE);

let (reloadable_subscriber, reload_handle) =
crate::reload::Subscriber::new(inner_subscriber);
let reload = reloadable_subscriber.with_collector(Registry::default());

with_default(reload, || {
{
let span1 = tracing::info_span!("span1", x = 42);
let _e = span1.enter();
}

let _ = reload_handle.modify(|s| s.set_span_events(FmtSpan::NONE));

// this span should not be logged at all!
{
let span2 = tracing::info_span!("span2", x = 100);
let _e = span2.enter();
}

{
let span3 = tracing::info_span!("span3", x = 42);
let _e = span3.enter();

// The span config was modified after span3 was already entered.
// We should only see an exit
let _ = reload_handle.modify(|s| s.set_span_events(FmtSpan::ACTIVE));
}
});
let actual = sanitize_timings(make_writer.get_string());
assert_eq!(
"fake time span1{x=42}: tracing_subscriber::fmt::fmt_subscriber::test: enter\n\
fake time span1{x=42}: tracing_subscriber::fmt::fmt_subscriber::test: exit\n\
fake time span3{x=42}: tracing_subscriber::fmt::fmt_subscriber::test: exit\n",
actual.as_str()
);
}
}
Loading

0 comments on commit c37fc98

Please sign in to comment.