-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tokio taskname registration for use in tokio-console (#89)
This introduces the `tracing` feature that has to be enabled and also the environment variable `RUSTFLAGS="--cfg tokio_unstable"` must be set to enable tokio taskname registration. See example program `tokio-console.rs` for usage. --------- Co-authored-by: Finomnis <[email protected]>
- Loading branch information
1 parent
8b2d7f1
commit e3cee38
Showing
10 changed files
with
183 additions
and
62 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
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
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,66 @@ | ||
//! This example demonstrates how to use the tokio-console application for tracing tokio tasks's | ||
//! runtime behaviour. Subsystems will appear under their registration names. | ||
//! | ||
//! Run this example with: | ||
//! | ||
//! ``` | ||
//! RUSTFLAGS="--cfg tokio_unstable" cargo run --features "tracing" --example tokio_console | ||
//! ``` | ||
//! | ||
//! Then, open the `tokio-console` application (see https://crates.io/crates/tokio-console) to | ||
//! follow the subsystem tasks live. | ||
use miette::Result; | ||
use tokio::time::{sleep, Duration}; | ||
use tokio_graceful_shutdown::{FutureExt, SubsystemBuilder, SubsystemHandle, Toplevel}; | ||
use tracing::Level; | ||
use tracing_subscriber::{fmt::writer::MakeWriterExt, prelude::*}; | ||
|
||
async fn child(subsys: SubsystemHandle) -> Result<()> { | ||
sleep(Duration::from_millis(3000)) | ||
.cancel_on_shutdown(&subsys) | ||
.await | ||
.ok(); | ||
Ok(()) | ||
} | ||
|
||
async fn parent(subsys: SubsystemHandle) -> Result<()> { | ||
tracing::info!("Parent started."); | ||
|
||
let mut iteration = 0; | ||
while !subsys.is_shutdown_requested() { | ||
subsys.start(SubsystemBuilder::new(format!("child{iteration}"), child)); | ||
iteration += 1; | ||
|
||
sleep(Duration::from_millis(1000)) | ||
.cancel_on_shutdown(&subsys) | ||
.await | ||
.ok(); | ||
} | ||
|
||
tracing::info!("Parent stopped."); | ||
Ok(()) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
// Init tokio-console server and tracing | ||
let console_layer = console_subscriber::spawn(); | ||
tracing_subscriber::registry() | ||
.with(console_layer) | ||
.with( | ||
tracing_subscriber::fmt::layer() | ||
.with_writer(std::io::stdout.with_max_level(Level::DEBUG)) | ||
.compact(), | ||
) | ||
.init(); | ||
|
||
// Setup and execute subsystem tree | ||
Toplevel::new(|s| async move { | ||
s.start(SubsystemBuilder::new("parent", parent)); | ||
}) | ||
.catch_signals() | ||
.handle_shutdown_requests(Duration::from_millis(1000)) | ||
.await | ||
.map_err(Into::into) | ||
} |
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
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,23 @@ | ||
use std::future::Future; | ||
use tokio::task::JoinHandle; | ||
|
||
#[cfg(not(all(tokio_unstable, feature = "tracing")))] | ||
#[track_caller] | ||
pub(crate) fn spawn<F: Future + Send + 'static>(f: F, _name: &str) -> JoinHandle<F::Output> | ||
where | ||
<F as Future>::Output: Send + 'static, | ||
{ | ||
tokio::spawn(f) | ||
} | ||
|
||
#[cfg(all(tokio_unstable, feature = "tracing"))] | ||
#[track_caller] | ||
pub(crate) fn spawn<F: Future + Send + 'static>(f: F, name: &str) -> JoinHandle<F::Output> | ||
where | ||
<F as Future>::Output: Send + 'static, | ||
{ | ||
tokio::task::Builder::new() | ||
.name(name) | ||
.spawn(f) | ||
.expect("a task should be spawned") | ||
} |
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