Commit c58d993
authored
runtime: Instrument with tracing (#6981)
## Summary
This is so that tracing subscribers, can correctly attribute information
to eg. opentelemetry distributed tracing IDs.
## API Changes
None
## Testing
Wrote a test, but didn't check it in because it would require the `std`
feature of tracing and make tracing-subscriber a dependency. Happy to
add the test if those things are ok.
```
#[tokio::test]
async fn test_spawn_propagates_tracing_span() {
install_global_subscriber();
let handle = TokioRuntime::current();
let span = tracing::info_span!("parent_span");
let parent_id = span.id();
assert!(parent_id.is_some(), "subscriber must be active");
let _enter = span.enter();
// spawn: async future is instrumented with the parent span
let spawned_id = handle.spawn(async { tracing::Span::current().id() }).await;
assert_eq!(spawned_id, parent_id, "spawn should propagate span");
// spawn_cpu: closure enters the parent span
let cpu_id = handle.spawn_cpu(|| tracing::Span::current().id()).await;
assert_eq!(cpu_id, parent_id, "spawn_cpu should propagate span");
// spawn_blocking: closure enters the parent span on the blocking thread
let blocking_id = handle
.spawn_blocking(|| tracing::Span::current().id())
.await;
assert_eq!(
blocking_id, parent_id,
"spawn_blocking should propagate span"
);
}
```
Signed-off-by: Frederic Branczyk <fbranczyk@gmail.com>1 parent bf5f390 commit c58d993
1 file changed
Lines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
65 | 66 | | |
66 | 67 | | |
67 | 68 | | |
| 69 | + | |
68 | 70 | | |
69 | 71 | | |
70 | 72 | | |
71 | 73 | | |
72 | 74 | | |
| 75 | + | |
73 | 76 | | |
74 | 77 | | |
75 | 78 | | |
| |||
103 | 106 | | |
104 | 107 | | |
105 | 108 | | |
| 109 | + | |
106 | 110 | | |
| 111 | + | |
107 | 112 | | |
108 | 113 | | |
109 | 114 | | |
| |||
123 | 128 | | |
124 | 129 | | |
125 | 130 | | |
| 131 | + | |
126 | 132 | | |
| 133 | + | |
127 | 134 | | |
128 | 135 | | |
129 | 136 | | |
| |||
0 commit comments