Skip to content

Commit f7d35e5

Browse files
authored
core: Make asyncify preserve the current span (#2790)
1 parent 8a16a12 commit f7d35e5

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

crates/core/src/util/mod.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use futures::{Future, FutureExt};
22
use std::borrow::Cow;
33
use std::pin::pin;
44
use tokio::sync::oneshot;
5+
use tracing::Span;
56

67
pub mod prometheus_handle;
78

@@ -40,13 +41,19 @@ where
4041
F: FnOnce() -> R + Send + 'static,
4142
R: Send + 'static,
4243
{
43-
tokio::task::spawn_blocking(f)
44-
.await
45-
.unwrap_or_else(|e| match e.try_into_panic() {
46-
Ok(panic_payload) => std::panic::resume_unwind(panic_payload),
47-
// the only other variant is cancelled, which shouldn't happen because we don't cancel it.
48-
Err(e) => panic!("Unexpected JoinError: {e}"),
49-
})
44+
// Ensure that `f` executes in the current span context.
45+
// If there is no current span, or it is disabled, `span` is disabled.
46+
let span = Span::current();
47+
tokio::task::spawn_blocking(move || {
48+
let _enter = span.enter();
49+
f()
50+
})
51+
.await
52+
.unwrap_or_else(|e| match e.try_into_panic() {
53+
Ok(panic_payload) => std::panic::resume_unwind(panic_payload),
54+
// the only other variant is cancelled, which shouldn't happen because we don't cancel it.
55+
Err(e) => panic!("Unexpected JoinError: {e}"),
56+
})
5057
}
5158

5259
/// Await `fut`, while also polling `also`.

0 commit comments

Comments
 (0)