Skip to content

Commit

Permalink
Hide the tracing implications of spawning tasks or threads
Browse files Browse the repository at this point in the history
Summary:
As we are migrating from slog to tracing, we will need to do the same thing everywhere we spawn a tokio task or a thread: propagate the parent span.
This will allow us to behave conceptually closer to the slog way of thinking.

To make it easy to transition, introduce:
* `mononoke::spawn_task` that will replace `tokio::spawn`
* `mononoke::spawn_thread` that will replace `thread::spawn`
Both will silently do whatever they need to do for `tracing` to be seemlessly functional.

Reviewed By: markbt

Differential Revision: D68956886

fbshipit-source-id: a28bd0d90ab5dcde04ea03f78a9aab1940566e7f
  • Loading branch information
Pierre Chevalier authored and facebook-github-bot committed Jan 31, 2025
1 parent 23e1d92 commit dcd4372
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions eden/mononoke/mononoke_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ license = "GPLv2+"
[dependencies]
justknobs = { version = "0.1.0", git = "https://github.com/facebookexperimental/rust-shed.git", branch = "main" }
mononoke_proc_macros = { version = "0.1.0", path = "proc_macros" }
tokio = { version = "1.41.0", features = ["full", "test-util", "tracing"] }
tracing = { version = "0.1.41", features = ["attributes", "valuable"] }
26 changes: 26 additions & 0 deletions eden/mononoke/mononoke_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,41 @@
*/

pub mod mononoke {
use std::future::Future;
use std::thread;

use justknobs::test_helpers;
use justknobs::test_helpers::JustKnobsInMemory;
pub use mononoke_proc_macros::fbinit_test;
pub use mononoke_proc_macros::test;
use tokio::task;
use tracing::Instrument;
use tracing::Span;

pub fn override_just_knobs() {
let just_knobs_json = include_str!("../just_knobs_defaults/just_knobs.json");
test_helpers::override_just_knobs(
JustKnobsInMemory::from_json(just_knobs_json).expect("failed to parse just knobs json"),
);
}

pub fn spawn_task<F>(future: F) -> task::JoinHandle<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
task::spawn(future.in_current_span())
}

pub fn spawn_thread<F, Output>(func: F) -> thread::JoinHandle<Output>
where
F: FnOnce() -> Output + Send + 'static,
Output: Send + 'static,
{
let current_span = Span::current();
thread::spawn(move || {
let _ = current_span.enter();
func()
})
}
}

0 comments on commit dcd4372

Please sign in to comment.