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

Attach follows_from() before constructing future #2686

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion tracing-attributes/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,11 @@ fn gen_block<B: ToTokens>(

return quote!(
let __tracing_attr_span = #span;
let __tracing_instrument_future = #mk_fut;
if !__tracing_attr_span.is_disabled() {
#follows_from
}
let __tracing_instrument_future = #mk_fut;
if !__tracing_attr_span.is_disabled() {
tracing::Instrument::instrument(
__tracing_instrument_future,
__tracing_attr_span
Expand Down
40 changes: 40 additions & 0 deletions tracing-attributes/tests/follows_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ async fn with_follows_from_async(causes: impl IntoIterator<Item = impl Into<Opti
#[instrument(follows_from = [&Span::current()])]
fn follows_from_current() {}

#[derive(Debug)]
struct NonCopyContext {
id: Option<Id>,
}

impl NonCopyContext {
fn id(&self) -> Option<tracing::Id> {
self.id.clone()
}
}

#[instrument(follows_from = [ctx.id()])]
fn follows_from_noncopy(ctx: NonCopyContext) {
println!("{:?}", ctx.id());
}

#[test]
fn follows_from_sync_test() {
let cause_a = expect::span().named("cause_a");
Expand Down Expand Up @@ -99,3 +115,27 @@ fn follows_from_current_test() {

handle.assert_finished();
}

#[test]
fn follows_from_noncopy_test() {
let cause_a = expect::span().named("cause_a");
let consequence = expect::span().named("follows_from_noncopy");

let (collector, handle) = collector::mock()
.new_span(cause_a.clone())
.new_span(consequence.clone())
.follows_from(consequence.clone(), cause_a)
.enter(consequence.clone())
.exit(consequence)
.only()
.run_with_handle();

with_default(collector, || {
let cause_a = tracing::info_span!("cause_a");
let context = NonCopyContext { id: cause_a.id() };

follows_from_noncopy(context)
});

handle.assert_finished();
}