-
Notifications
You must be signed in to change notification settings - Fork 729
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 = …)]
(
#2091) This PR extends the `#[instrument]` attribute to accept an optionally `parent = …` argument that provides an explicit parent to the generated `Span`. ## Motivation This PR resolves #2021 and partially resolves #879. (It only partly resolves #879 in that it only provides a mechanism for specifying an explicit parent, but *not* for invoking `follows_from`.) ## Solution This PR follows the implementation strategy articulated by @hawkw: #879 (comment). The user-provided value to the `parent` argument may be any expression, and this expression is provided directly to the invocation of [`span!`](https://tracing.rs/tracing/macro.span.html).
- Loading branch information
Showing
4 changed files
with
153 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,102 @@ | ||
use tracing::{subscriber::with_default, Id, Level}; | ||
use tracing_attributes::instrument; | ||
use tracing_mock::*; | ||
|
||
#[instrument] | ||
fn with_default_parent() {} | ||
|
||
#[instrument(parent = parent_span, skip(parent_span))] | ||
fn with_explicit_parent<P>(parent_span: P) | ||
where | ||
P: Into<Option<Id>>, | ||
{ | ||
} | ||
|
||
#[test] | ||
fn default_parent_test() { | ||
let contextual_parent = span::mock().named("contextual_parent"); | ||
let child = span::mock().named("with_default_parent"); | ||
|
||
let (subscriber, handle) = subscriber::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) | ||
.exit(contextual_parent) | ||
.done() | ||
.run_with_handle(); | ||
|
||
with_default(subscriber, || { | ||
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 (subscriber, handle) = subscriber::mock() | ||
.new_span( | ||
contextual_parent | ||
.clone() | ||
.with_contextual_parent(None) | ||
.with_explicit_parent(None), | ||
) | ||
.new_span( | ||
explicit_parent | ||
.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) | ||
.exit(contextual_parent) | ||
.done() | ||
.run_with_handle(); | ||
|
||
with_default(subscriber, || { | ||
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(); | ||
} |