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

attributes: add err(Debug) meta to use Debug impl #1631

Merged
merged 5 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
89 changes: 75 additions & 14 deletions tracing-attributes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ use syn::{
/// ```
///
/// If the function returns a `Result<T, E>` and `E` implements `std::fmt::Display`, you can add
/// `err` to emit error events when the function returns `Err`:
/// `err` or `err(Display)` to emit error events when the function returns `Err`:
///
/// ```
/// # use tracing_attributes::instrument;
Expand All @@ -191,6 +191,18 @@ use syn::{
/// Ok(())
/// }
/// ```
///
/// The above example will be emitting error events using the `std::fmt::Display` implementation.
/// If `E` implements `std::fmt::Debug`, you can also make it use that implementation with
/// `err(Debug)`:
Copy link
Member

Choose a reason for hiding this comment

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

How about changing this to something like:

Suggested change
/// The above example will be emitting error events using the `std::fmt::Display` implementation.
/// If `E` implements `std::fmt::Debug`, you can also make it use that implementation with
/// `err(Debug)`:
/// By default, error values will be recorded using their `std::fmt::Display` implementations.
/// If an error implements `std::fmt::Debug`, it can be recorded using its `Debug` implementation
/// instead, by writing `err(Debug)`:

///
/// ```
/// # use tracing_attributes::instrument;
/// #[instrument(err(Debug))]
/// fn my_function(arg: usize) -> Result<(), std::io::Error> {
/// Ok(())
/// }
/// ```
///
/// `async fn`s may also be instrumented:
///
Expand Down Expand Up @@ -393,7 +405,6 @@ fn gen_block(
instrumented_function_name: &str,
self_type: Option<&syn::TypePath>,
) -> proc_macro2::TokenStream {
let err = args.err;

// generate the span's name
let span_name = args
Expand Down Expand Up @@ -507,29 +518,34 @@ fn gen_block(
))
})();

let err_event = match args.err_mode {
Some(ErrorMode::Display) => Some(quote!(tracing::error!(error = %e))),
Some(ErrorMode::Debug) => Some(quote!(tracing::error!(error = ?e))),
_ => None,
};

// Generate the instrumented function body.
// If the function is an `async fn`, this will wrap it in an async block,
// which is `instrument`ed using `tracing-futures`. Otherwise, this will
// enter the span and then perform the rest of the body.
// If `err` is in args, instrument any resulting `Err`s.
if async_context {
let mk_fut = if err {
quote_spanned!(block.span()=>
let mk_fut = match err_event {
Some(err_event) => quote_spanned!(block.span()=>
async move {
match async move { #block }.await {
#[allow(clippy::unit_arg)]
Ok(x) => Ok(x),
Err(e) => {
tracing::error!(error = %e);
#err_event;
Err(e)
}
}
}
)
} else {
quote_spanned!(block.span()=>
),
None => quote_spanned!(block.span()=>
async move { #block }
)
),
};

return quote!(
Expand Down Expand Up @@ -566,15 +582,15 @@ fn gen_block(
}
);

if err {
if let Some(err_event) = err_event {
return quote_spanned!(block.span()=>
#span
#[allow(clippy::redundant_closure_call)]
match (move || #block)() {
#[allow(clippy::unit_arg)]
Ok(x) => Ok(x),
Err(e) => {
tracing::error!(error = %e);
#err_event;
Err(e)
}
}
Expand Down Expand Up @@ -603,7 +619,7 @@ struct InstrumentArgs {
target: Option<LitStr>,
skips: HashSet<Ident>,
fields: Option<Fields>,
err: bool,
err_mode: Option<ErrorMode>,
/// Errors describing any unrecognized parse inputs that we skipped.
parse_warnings: Vec<syn::Error>,
}
Expand Down Expand Up @@ -728,8 +744,8 @@ impl Parse for InstrumentArgs {
}
args.fields = Some(input.parse()?);
} else if lookahead.peek(kw::err) {
let _ = input.parse::<kw::err>()?;
args.err = true;
let ErrorModes(mode) = input.parse()?;
args.err_mode = Some(mode);
} else if lookahead.peek(Token![,]) {
let _ = input.parse::<Token![,]>()?;
} else {
Expand Down Expand Up @@ -787,6 +803,51 @@ impl Parse for Skips {
}
}

#[derive(Debug, Hash, PartialEq, Eq)]
enum ErrorMode {
Display,
Debug,
}

impl Default for ErrorMode {
fn default() -> Self {
ErrorMode::Display
}
}

impl Parse for ErrorMode {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
if !input.peek(syn::token::Paren) {
return Ok(ErrorMode::default());
}
let content;
let _ = syn::parenthesized!(content in input);
let maybe_mode: Option<Ident> = content.parse()?;
maybe_mode.map_or(
Ok(ErrorMode::default()),
|ident| match ident.to_string().as_str() {
"Debug" => Ok(ErrorMode::Debug),
"Display" => Ok(ErrorMode::Display),
_ => Err(syn::Error::new(
ident.span(),
"unknown error mode, must be Debug or Display",
)),
}
)
}
}

struct ErrorModes(ErrorMode);

impl Parse for ErrorModes {

fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let _ = input.parse::<kw::err>();
let mode = ErrorMode::parse(input)?;
Ok(Self(mode))
}
}
Copy link
Member

Choose a reason for hiding this comment

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

I feel like the name ErrorModes is not really accurate for this anymore, since it no longer parses a list of error modes, and now simply parses the err keyword along with the mode. Maybe we should just rename it to

Suggested change
struct ErrorModes(ErrorMode);
impl Parse for ErrorModes {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let _ = input.parse::<kw::err>();
let mode = ErrorMode::parse(input)?;
Ok(Self(mode))
}
}
struct Error(ErrorMode);
impl Parse for Error {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let _ = input.parse::<kw::err>();
let mode = ErrorMode::parse(input)?;
Ok(Self(mode))
}
}

or, we could probably just get rid of this entirely, and move the err keyword into InstrumentArgs, like it was before --- we would just try to parse an ErrorMode if we parsed the err keyword, inside of the Parse impl for InstrumentArgs.

IMO, having a separate type for this is not really necessary any longer, but it's up to you...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's right, I completely missed this. I think removing Error and doing err parsing into the InstrumentArgs Parse impl. 👍


#[derive(Debug)]
struct Fields(Punctuated<Field, Token![,]>);

Expand Down
53 changes: 53 additions & 0 deletions tracing-attributes/tests/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,56 @@ fn impl_trait_return_type() {

handle.assert_finished();
}

#[instrument(err(Debug))]
fn err_dbg() -> Result<u8, TryFromIntError> {
u8::try_from(1234)
}

#[test]
fn test_err_dbg() {
let span = span::mock().named("err_dbg");
let (collector, handle) = collector::mock()
.new_span(span.clone())
.enter(span.clone())
.event(
event::mock()
.at_level(Level::ERROR)
.with_fields(
field::mock("error")
// use the actual error value that will be emitted, so
// that this test doesn't break if the standard library
// changes the `fmt::Debug` output from the error type
// in the future.
.with_value(&tracing::field::debug(u8::try_from(1234).unwrap_err()))
)
)
.exit(span.clone())
.drop_span(span)
.done()
.run_with_handle();
with_default(collector, || err_dbg().ok());
handle.assert_finished();
}

#[test]
fn test_err_display_default() {
let span = span::mock().named("err");
let (collector, handle) = collector::mock()
.new_span(span.clone())
.enter(span.clone())
.event(event::mock()
.at_level(Level::ERROR)
.with_fields(
field::mock("error")
// by default, errors will be emitted with their display values
.with_value(&tracing::field::display(u8::try_from(1234).unwrap_err()))
)
)
.exit(span.clone())
.drop_span(span)
.done()
.run_with_handle();
with_default(collector, || err().ok());
handle.assert_finished();
}