forked from tokio-rs/tracing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
attributes: permit setting parent span via
#[instrument(parent = …)]
Fixes: tokio-rs#2021 Refs: tokio-rs#879
- Loading branch information
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use tracing::{collect::with_default, Level, Span}; | ||
use tracing_attributes::instrument; | ||
use tracing_mock::*; | ||
|
||
#[instrument] | ||
fn with_default_parent() {} | ||
|
||
#[instrument(parent = parent_span)] | ||
fn with_explicit_parent(parent_span: &Span) {} | ||
|
||
#[test] | ||
fn default_parent_test() { | ||
let contextual_parent = span::mock().named("contextual_parent"); | ||
let child = span::mock().named("with_default_parent"); | ||
|
||
let (collector, handle) = collector::mock() | ||
.new_span( | ||
contextual_parent | ||
.clone() | ||
.with_contextual_parent(None) | ||
.with_explicit_parent(None), | ||
) | ||
.new_span( | ||
child | ||
.clone() | ||
.with_contextual_parent(Some("contextual_parent")) | ||
.with_explicit_parent(None), | ||
) | ||
.enter(child.clone()) | ||
.exit(child.clone()) | ||
.enter(contextual_parent.clone()) | ||
.new_span( | ||
child | ||
.clone() | ||
.with_contextual_parent(Some("contextual_parent")) | ||
.with_explicit_parent(None), | ||
) | ||
.enter(child.clone()) | ||
.exit(child.clone()) | ||
.exit(contextual_parent.clone()) | ||
.done() | ||
.run_with_handle(); | ||
|
||
with_default(collector, || { | ||
let contextual_parent = tracing::span!(Level::TRACE, "contextual_parent"); | ||
|
||
with_default_parent(); | ||
|
||
contextual_parent.in_scope(|| { | ||
with_default_parent(); | ||
}); | ||
}); | ||
|
||
handle.assert_finished(); | ||
} | ||
|
||
#[test] | ||
fn explicit_parent_test() { | ||
let contextual_parent = span::mock().named("contextual_parent"); | ||
let explicit_parent = span::mock().named("explicit_parent"); | ||
let child = span::mock().named("with_explicit_parent"); | ||
|
||
let (collector, handle) = collector::mock() | ||
.new_span( | ||
contextual_parent | ||
.clone() | ||
.with_contextual_parent(None) | ||
.with_explicit_parent(None), | ||
) | ||
.new_span( | ||
explicit_parent | ||
.clone() | ||
.with_contextual_parent(None) | ||
.with_explicit_parent(None), | ||
) | ||
.enter(contextual_parent.clone()) | ||
.new_span( | ||
child | ||
.clone() | ||
.with_contextual_parent(Some("contextual_parent")) | ||
.with_explicit_parent(Some("explicit_parent")), | ||
) | ||
.enter(child.clone()) | ||
.exit(child.clone()) | ||
.exit(contextual_parent.clone()) | ||
.done() | ||
.run_with_handle(); | ||
|
||
with_default(collector, || { | ||
let contextual_parent = tracing::span!(Level::INFO, "contextual_parent"); | ||
let explicit_parent = tracing::span!(Level::INFO, "explicit_parent"); | ||
|
||
contextual_parent.in_scope(|| { | ||
with_explicit_parent(&explicit_parent); | ||
}); | ||
}); | ||
|
||
handle.assert_finished(); | ||
} |