From f2ce8977455489ad6bbbddc744d854875bebc7e3 Mon Sep 17 00:00:00 2001 From: Alejandro Pedraza Date: Mon, 10 Feb 2025 07:56:10 -0500 Subject: [PATCH 1/2] fix(tracing): propagate span context The tracing span context set in consumers of the `TokioExecutor` was sometimes lost (if the task was run in a different thread). This adds a call to `in_current_span()` to preserve it. --- Cargo.toml | 2 +- src/rt/tokio.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e4883e6d..73ba645f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,7 +69,7 @@ service = ["dep:tower-service"] http1 = ["hyper/http1"] http2 = ["hyper/http2"] -tokio = ["dep:tokio", "tokio/net", "tokio/rt", "tokio/time"] +tokio = ["dep:tokio", "tokio/net", "tokio/rt", "tokio/time", "dep:tracing"] # internal features used in CI __internal_happy_eyeballs_tests = [] diff --git a/src/rt/tokio.rs b/src/rt/tokio.rs index e5f2eab7..53b04347 100644 --- a/src/rt/tokio.rs +++ b/src/rt/tokio.rs @@ -9,6 +9,7 @@ use std::{ use hyper::rt::{Executor, Sleep, Timer}; use pin_project_lite::pin_project; +use tracing::instrument::Instrument; /// Future executor that utilises `tokio` threads. #[non_exhaustive] @@ -49,7 +50,7 @@ where Fut::Output: Send + 'static, { fn execute(&self, fut: Fut) { - tokio::spawn(fut); + tokio::spawn(fut.in_current_span()); } } From 8ebdab91c2fb2128f5adabc5462ea7361eda4bc1 Mon Sep 17 00:00:00 2001 From: Alejandro Pedraza Date: Tue, 18 Feb 2025 20:07:49 -0500 Subject: [PATCH 2/2] Place new functionality behind a new 'tracing' feature --- Cargo.toml | 5 ++++- src/rt/tokio.rs | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 73ba645f..5d831f2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,6 +55,7 @@ full = [ "http1", "http2", "tokio", + "tracing", ] client = ["hyper/client", "dep:tracing", "dep:futures-channel", "dep:tower-service"] @@ -69,7 +70,9 @@ service = ["dep:tower-service"] http1 = ["hyper/http1"] http2 = ["hyper/http2"] -tokio = ["dep:tokio", "tokio/net", "tokio/rt", "tokio/time", "dep:tracing"] +tokio = ["dep:tokio", "tokio/net", "tokio/rt", "tokio/time"] + +tracing = ["dep:tracing"] # internal features used in CI __internal_happy_eyeballs_tests = [] diff --git a/src/rt/tokio.rs b/src/rt/tokio.rs index 53b04347..57e3c372 100644 --- a/src/rt/tokio.rs +++ b/src/rt/tokio.rs @@ -9,6 +9,8 @@ use std::{ use hyper::rt::{Executor, Sleep, Timer}; use pin_project_lite::pin_project; + +#[cfg(feature = "tracing")] use tracing::instrument::Instrument; /// Future executor that utilises `tokio` threads. @@ -50,7 +52,11 @@ where Fut::Output: Send + 'static, { fn execute(&self, fut: Fut) { + #[cfg(feature = "tracing")] tokio::spawn(fut.in_current_span()); + + #[cfg(not(feature = "tracing"))] + tokio::spawn(fut); } }