-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(system): Instrument spawned tasks
- Loading branch information
Showing
4 changed files
with
84 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
disallowed-methods = [ | ||
{ path = "tokio::spawn", reason = "use `relay_system::spawn` instead" }, | ||
] | ||
|
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,37 @@ | ||
use std::panic::Location; | ||
use std::time::Instant; | ||
|
||
use futures::Future; | ||
use tokio::task::JoinHandle; | ||
|
||
use crate::statsd::{SystemCounters, SystemTimers}; | ||
|
||
/// Spawns a new asynchronous task, returning a [`JoinHandle`] for it. | ||
/// | ||
/// This is in instrumented spawn variant of Tokio's [`tokio::spawn`]. | ||
#[track_caller] | ||
pub fn spawn<F>(future: F) -> JoinHandle<F::Output> | ||
where | ||
F: Future + Send + 'static, | ||
F::Output: Send + 'static, | ||
{ | ||
let location = Location::caller(); | ||
let task_id = format!("{}:{}", location.file(), location.line()); | ||
|
||
relay_statsd::metric!( | ||
counter(SystemCounters::RuntimeTasksCreated) += 1, | ||
id = &task_id | ||
); | ||
|
||
let created = Instant::now(); | ||
tokio::spawn(async move { | ||
let result = future.await; | ||
|
||
relay_statsd::metric!( | ||
timer(SystemTimers::RuntimeTasksFinished) = created.elapsed(), | ||
id = &task_id | ||
); | ||
|
||
result | ||
}) | ||
} |
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