Crates.io | tracing-tools |
lib.rs | tracing-tools |
version | 0.6.0 |
source | src |
created_at | 2021-05-19 10:24:57.215456 |
updated_at | 2021-12-04 16:04:49.254088 |
description | Quick and simple band aid for instrumenting async code with tracing |
homepage | |
repository | https://github.com/let4be/tracing-tools |
max_upload_size | |
id | 399479 |
size | 40,407 |
Right now tracing is a bit unusable with async code, because recommended way to instrument async
code is by using #instrument
macro which
So this tiny crate tries to resolve those issues by switching to direct code instrumentation, example:
use anyhow::{anyhow, Context};
use tracing::{Level};
use tracing_tools::{span, TracingTask, PinnedFut};
use tokio::time::{sleep, Duration};
type Result<T> = anyhow::Result<T>;
struct Test {
}
impl Test {
fn drink_coffee(&self, ) -> Result<()> {
Err(anyhow!("out of coffee"))
}
fn fn1(&self, n: usize) -> PinnedFut<'_> {
TracingTask::new(span!(Level::INFO, n=n, another_field="bow wow!"), async move {
sleep(Duration::from_secs(1)).await;
Ok(())
}).instrument()
}
fn fn2(&self, n: usize) -> PinnedFut<'_> {
TracingTask::new(span!(Level::INFO, n=n, another_field="bow wow!"), async move {
Ok(self.drink_coffee().context("cannot drink coffee")?)
}).instrument()
}
fn fn3(& self, n: usize) -> PinnedFut<'_> {
TracingTask::new_short_lived(span!(Level::INFO, n=n, another_field="bow wow!"), async move {
Ok(self.drink_coffee().context("cannot drink coffee")?)
}).instrument()
}
}
fn configure_tracing() -> Result<()>{
let collector = tracing_subscriber::fmt()
.with_target(false)
.with_max_level(Level::INFO)
.finish();
tracing::subscriber::set_global_default(collector)?;
Ok(())
}
#[tokio::main]
async fn main() -> Result<()> {
configure_tracing()?;
let s = Test {};
let _ = s.fn1(1).await;
let _ = s.fn2(2).await;
let _ = s.fn3(3).await;
Ok(())
}
Feel free to fork ;)