Skip to content

Commit

Permalink
attributes: ensure res and err events inherit target (tokio-rs#…
Browse files Browse the repository at this point in the history
…2184)

## Motivation

Currently, when an `#[instrument]` attribute has an overridden target,
the events generated by `ret` and `err` arguments do not inherit that
target.

For example, if I write

```rust
#[tracing::instrument(target = "some_target", ret)
fn do_stuff() -> Something {
    // ...
}
```

the `do_stuff` span will have the target "some_target", but the return
value event generated by `ret` will have the current module path as its
target, and there is no way to change the return value event's target.

## Solution

This branch changes the macro expansion for `#[instrument]` with the
`ret` and/or `err` arguments so that an overridden target is propagated
to the events generated by `ret` and `err`.

Fixes tokio-rs#2183
  • Loading branch information
tbraun96 authored and kaffarell committed May 22, 2024
1 parent 3d2f486 commit 81ad440
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tracing-attributes/tests/ret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,33 @@ fn test_custom_target() {
handle.assert_finished();
}

#[test]
fn test_custom_target() {
let filter: EnvFilter = "my_target=info".parse().expect("filter should parse");
let span = span::mock()
.named("ret_with_target")
.with_target("my_target");

let (subscriber, handle) = subscriber::mock()
.new_span(span.clone())
.enter(span.clone())
.event(
event::mock()
.with_fields(field::mock("return").with_value(&tracing::field::debug(42)))
.at_level(Level::INFO)
.with_target("my_target"),
)
.exit(span.clone())
.drop_span(span)
.done()
.run_with_handle();

let subscriber = subscriber.with(filter);

with_default(subscriber, ret_with_target);
handle.assert_finished();
}

#[instrument(level = "warn", ret)]
fn ret_warn() -> i32 {
42
Expand Down

0 comments on commit 81ad440

Please sign in to comment.