Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customizing logged span events #40

Merged
merged 3 commits into from
Aug 23, 2023

Conversation

9999years
Copy link
Member

Currently, new/close span events are logged (that is, when a span is created for the first time, usually when an #[instrument]ed function is called, and when spans are closed, usually when those functions return). It can be useful to disable those for terser logs, or to enable more events for greater detail.

This change adds a --trace-spans argument to customize which span events are logged.

Before shipping 1.0, we should clean up the option names. Maybe --tracing-filter could be --log or something.

@linear
Copy link

linear bot commented Aug 22, 2023

DUX-1319 Customizable span events

Allow customizing the span events that get logged

@9999years
Copy link
Member Author

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

This was referenced Aug 22, 2023
@github-actions github-actions bot added the patch Bug fixes or non-functional changes label Aug 22, 2023
@9999years 9999years requested a review from Gabriella439 August 22, 2023 22:13
evanrelf
evanrelf previously approved these changes Aug 22, 2023
src/cli.rs Outdated Show resolved Hide resolved
Comment on lines +20 to +30
fn value_variants<'a>() -> &'a [Self] {
&[
Self(FmtSpan::NEW),
Self(FmtSpan::ENTER),
Self(FmtSpan::EXIT),
Self(FmtSpan::CLOSE),
Self(FmtSpan::NONE),
Self(FmtSpan::ACTIVE),
Self(FmtSpan::FULL),
]
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I'm surprised this lifetime can't be elided?

  2. Could this list be generated with the Rust equivalent of [minBound .. maxBound] in Haskell? The compiler won't tell you if a new variant is added, making this list incomplete.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Me too! I guess the trait definition has an explicit lifetime so you're not allowed to elide it here?
Compilation error when I elide the lifetime
   error[E0106]: missing lifetime specifier
  --> src/clap/fmt_span.rs:20:28
   |
20 |     fn value_variants() -> &[Self] {
   |                            ^ expected named lifetime parameter
   |
   = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime
   |
20 |     fn value_variants() -> &'static [Self] {
   |                             +++++++

error[E0195]: lifetime parameters or bounds on method `value_variants` do not match the trait declaration
  --> src/clap/fmt_span.rs:20:22
   |
20 |     fn value_variants() -> &[Self] {
   |                      ^ lifetimes do not match method in trait

Some errors have detailed explanations: E0106, E0195.
For more information about an error, try `rustc --explain E0106`.
  1. I wish! This has been an open issue since before Rust 1.0 (peep the Ruby-like syntax in the original issue here). There's a few crates that implement this functionality for enum types with #[derive] macros, but they're pretty heavy so we don't use them here.

Comment on lines +32 to +46
fn to_possible_value(&self) -> Option<PossibleValue> {
Some(match self.0 {
FmtSpan::NEW => PossibleValue::new("new").help("Log when spans are created"),
FmtSpan::ENTER => PossibleValue::new("enter").help("Log when spans are entered"),
FmtSpan::EXIT => PossibleValue::new("exit").help("Log when spans are exited"),
FmtSpan::CLOSE => PossibleValue::new("close").help("Log when spans are dropped"),
FmtSpan::NONE => PossibleValue::new("none").help("Do not log span events"),
FmtSpan::ACTIVE => {
PossibleValue::new("active").help("Log when spans are entered/exited")
}
FmtSpan::FULL => PossibleValue::new("full").help("Log all span events"),
_ => {
return None;
}
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this one of those permanently non-exhaustive enum things, or are you ignoring some of the options? If the latter, why?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The former. The FmtSpan type is actually defined as a wrapper around a u8, not an enum:

pub struct FmtSpan(u8);

impl FmtSpan {
    pub const NEW: FmtSpan = FmtSpan(1 << 0);
    // ...
}

https://docs.rs/tracing-subscriber/latest/tracing_subscriber/fmt/format/struct.FmtSpan.html

So these look like enum variants, but they're actually constants in the type's namespace. (You can tell something funky is up because they're written in ALL_CAPS rather than TitleCase.)

In short: this is necessary to make the compiler not complain.

Base automatically changed from rebeccat/dux-1318-move-ghci-out-of-arcmutex_ to main August 22, 2023 23:48
@9999years 9999years dismissed evanrelf’s stale review August 22, 2023 23:48

The base branch was changed.

@9999years 9999years force-pushed the rebeccat/dux-1319-customizable-span-events branch from 488220b to d7c407c Compare August 22, 2023 23:52
@9999years 9999years merged commit 145864e into main Aug 23, 2023
@9999years 9999years deleted the rebeccat/dux-1319-customizable-span-events branch August 23, 2023 00:02
@github-actions
Copy link
Contributor

9999years added a commit that referenced this pull request Aug 23, 2023
Currently, new/close span events are logged (that is, when a span is
created for the first time, usually when an `#[instrument]`ed function
is called, and when spans are closed, usually when those functions
return). It can be useful to disable those for terser logs, or to enable
more events for greater detail.

This change adds a `--trace-spans` argument to customize which span
events are logged.

Before shipping 1.0, we should clean up the option names. Maybe
`--tracing-filter` could be `--log` or something.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patch Bug fixes or non-functional changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants