It is not uncommon that users who are new to tracing look at the examples in the `master` branch of the repository and find that they don't compile. This is because they are examples which compile with the code from the master branch, which is for the as yet unreleased tracing 0.2.0 ecosystem. Users should instead go to the `v0.1.x` branch to find examples compatible with the crates published on crates.io. This change adds a doc-comment to the beginning of every example file informing the user of this fact and suggesting that they check out the `v0.1.x` branch instead.
47 lines
1.4 KiB
Rust
47 lines
1.4 KiB
Rust
//! NOTE: This is pre-release documentation for the upcoming tracing 0.2.0 ecosystem. For the
|
|
//! release examples, please see the `v0.1.x` branch instead.
|
|
//!
|
|
//! This is a example showing how information is scoped with tokio's
|
|
//! `task::spawn`.
|
|
//!
|
|
//! You can run this example by running the following command in a terminal
|
|
//!
|
|
//! ```
|
|
//! cargo run --example tokio-spawny-thing
|
|
//! ```
|
|
#![deny(rust_2018_idioms)]
|
|
use futures::future::try_join_all;
|
|
use tracing::{debug, info, instrument, span, Instrument as _, Level};
|
|
|
|
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
|
|
|
|
#[instrument]
|
|
async fn parent_task(subtasks: usize) -> Result<(), Error> {
|
|
info!("spawning subtasks...");
|
|
let subtasks = (1..=subtasks)
|
|
.map(|number| {
|
|
let span = span!(Level::INFO, "subtask", %number);
|
|
debug!(message = "creating subtask;", number);
|
|
tokio::spawn(subtask(number).instrument(span))
|
|
})
|
|
.collect::<Vec<_>>();
|
|
|
|
// the returnable error would be if one of the subtasks panicked.
|
|
let sum: usize = try_join_all(subtasks).await?.iter().sum();
|
|
info!(%sum, "all subtasks completed; calculated sum");
|
|
Ok(())
|
|
}
|
|
|
|
async fn subtask(number: usize) -> usize {
|
|
info!(%number, "polling subtask");
|
|
number
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Error> {
|
|
tracing_subscriber::fmt()
|
|
.with_max_level(tracing::Level::DEBUG)
|
|
.try_init()?;
|
|
parent_task(10).await?;
|
|
Ok(())
|
|
}
|