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

Better documentation & tests for using Level constants for #[instrument(level = X)] #2350

Merged
merged 7 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions tracing-attributes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,14 @@ mod expand;
/// Setting the level for the generated span:
/// ```
/// # use tracing_attributes::instrument;
/// #[instrument(level = "debug")]
/// #[instrument(level = Level::DEBUG)]
/// pub fn my_function() {
/// // ...
/// }
/// ```
/// Levels can be specifide either with `Level` constants, literal strings
davidbarsky marked this conversation as resolved.
Show resolved Hide resolved
/// (`"debug", etc) or numerically (1 - 5).
Copy link
Member

Choose a reason for hiding this comment

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

maybe worth indicating how numbers are mapped to Levels --- maybe with a table, or just mentioning that 5 corresponds to the TRACE level? we could link to the docs here: https://docs.rs/tracing-core/latest/tracing_core/struct.Level.html#comparing-levels

///
/// Overriding the generated span's name:
/// ```
/// # use tracing_attributes::instrument;
Expand Down Expand Up @@ -235,7 +238,7 @@ mod expand;
///
/// ```
/// # use tracing_attributes::instrument;
/// #[instrument(ret(level = "warn"))]
/// #[instrument(ret(level = Level::WARN))]
/// fn my_function() -> i32 {
/// 42
/// }
Expand Down Expand Up @@ -271,7 +274,7 @@ mod expand;
///
/// ```
/// # use tracing_attributes::instrument;
/// #[instrument(err(level = "info"))]
/// #[instrument(err(level = Level::INFO))]
/// fn my_function(arg: usize) -> Result<(), std::io::Error> {
/// Ok(())
/// }
Expand Down
2 changes: 1 addition & 1 deletion tracing-attributes/tests/instrument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn override_everything() {
#[instrument(target = "my_target", level = "debug")]
fn my_fn() {}

#[instrument(level = "debug", target = "my_target")]
#[instrument(level = Level::DEBUG, target = "my_target")]
fn my_other_fn() {}

let span = span::mock()
Expand Down
46 changes: 46 additions & 0 deletions tracing-attributes/tests/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,49 @@ fn numeric_levels() {

handle.assert_finished();
}

#[test]
fn enum_levels() {
#[instrument(level = Level::TRACE)]
fn trace() {}

#[instrument(level = Level::DEBUG)]
fn debug() {}

#[instrument(level = tracing::Level::INFO)]
fn info() {}

#[instrument(level = Level::WARN)]
fn warn() {}

#[instrument(level = Level::ERROR)]
fn error() {}
let (collector, handle) = collector::mock()
.new_span(span::mock().named("trace").at_level(Level::TRACE))
.enter(span::mock().named("trace").at_level(Level::TRACE))
.exit(span::mock().named("trace").at_level(Level::TRACE))
.new_span(span::mock().named("debug").at_level(Level::DEBUG))
.enter(span::mock().named("debug").at_level(Level::DEBUG))
.exit(span::mock().named("debug").at_level(Level::DEBUG))
.new_span(span::mock().named("info").at_level(Level::INFO))
.enter(span::mock().named("info").at_level(Level::INFO))
.exit(span::mock().named("info").at_level(Level::INFO))
.new_span(span::mock().named("warn").at_level(Level::WARN))
.enter(span::mock().named("warn").at_level(Level::WARN))
.exit(span::mock().named("warn").at_level(Level::WARN))
.new_span(span::mock().named("error").at_level(Level::ERROR))
.enter(span::mock().named("error").at_level(Level::ERROR))
.exit(span::mock().named("error").at_level(Level::ERROR))
.done()
.run_with_handle();

with_default(collector, || {
trace();
debug();
info();
warn();
error();
});

handle.assert_finished();
}