-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
time,sync: make Sleep and BatchSemaphore instrumentation explicit roo…
…ts (#6727) When instrumenting resources in Tokio, a span is created for each resource. Previously, all resources inherited the currently active span as their parent (tracing default). However, this would keep that parent span alive until the resource (and its span) were dropped. This is often not correct, as a resource may be created in a task and then sent elsewhere, while the originating task ends. This artificial extension of the parent span's lifetime would make it look like that task was still alive (but idle) in any system reading the tracing instrumentation in Tokio, for example Tokio Console as reported in tokio-rs/console#345. In #6107, most of the existing resource spans were updated to make them explicit roots, so they have no contextual parent. However, 2. were missed: - `Sleep` - `BatchSemaphore` This change alters the resource spans for those 2 resources to also make them explicit roots.
- Loading branch information
Showing
4 changed files
with
64 additions
and
1 deletion.
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,61 @@ | ||
//! Tests for time resource instrumentation. | ||
//! | ||
//! These tests ensure that the instrumentation for tokio | ||
//! synchronization primitives is correct. | ||
use std::time::Duration; | ||
|
||
use tracing_mock::{expect, subscriber}; | ||
|
||
#[tokio::test] | ||
async fn test_sleep_creates_span() { | ||
let sleep_span = expect::span() | ||
.named("runtime.resource") | ||
.with_target("tokio::time::sleep"); | ||
|
||
let state_update = expect::event() | ||
.with_target("runtime::resource::state_update") | ||
.with_fields( | ||
expect::field("duration") | ||
.with_value(&(7_u64 + 1)) | ||
.and(expect::field("duration.op").with_value(&"override")), | ||
); | ||
|
||
let async_op_span = expect::span() | ||
.named("runtime.resource.async_op") | ||
.with_target("tokio::time::sleep"); | ||
|
||
let async_op_poll_span = expect::span() | ||
.named("runtime.resource.async_op.poll") | ||
.with_target("tokio::time::sleep"); | ||
|
||
let (subscriber, handle) = subscriber::mock() | ||
.new_span(sleep_span.clone().with_explicit_parent(None)) | ||
.enter(sleep_span.clone()) | ||
.event(state_update) | ||
.new_span( | ||
async_op_span | ||
.clone() | ||
.with_contextual_parent(Some("runtime.resource")) | ||
.with_field(expect::field("source").with_value(&"Sleep::new_timeout")), | ||
) | ||
.exit(sleep_span.clone()) | ||
.enter(async_op_span.clone()) | ||
.new_span( | ||
async_op_poll_span | ||
.clone() | ||
.with_contextual_parent(Some("runtime.resource.async_op")), | ||
) | ||
.exit(async_op_span.clone()) | ||
.drop_span(async_op_span) | ||
.drop_span(async_op_poll_span) | ||
.drop_span(sleep_span) | ||
.run_with_handle(); | ||
|
||
{ | ||
let _guard = tracing::subscriber::set_default(subscriber); | ||
|
||
_ = tokio::time::sleep(Duration::from_millis(7)); | ||
} | ||
|
||
handle.assert_finished(); | ||
} |