| Crates.io | await-tree-attributes |
| lib.rs | await-tree-attributes |
| version | 0.1.0-alpha.2 |
| created_at | 2025-06-10 07:02:57.238572+00 |
| updated_at | 2025-06-10 08:58:29.993377+00 |
| description | Procedural attributes for await-tree instrumentation |
| homepage | |
| repository | https://github.com/risingwavelabs/await-tree |
| max_upload_size | |
| id | 1706724 |
| size | 26,780 |
Procedural attributes for the await-tree crate.
This crate provides the #[instrument] attribute macro that automatically instruments async functions with await-tree spans, similar to how tracing::instrument works but specifically designed for await-tree.
Add this to your Cargo.toml:
[dependencies]
await-tree = { version = "0.3", features = ["attributes"] }
Then use the #[instrument] attribute on your async functions:
use await_tree::{instrument, InstrumentAwait};
#[instrument("fetch_data({})", id)]
async fn fetch_data(id: u32) -> String {
// Your async code here
format!("data_{}", id)
}
#[instrument(long_running, verbose, "complex_task({}, {})", name, value)]
async fn complex_task(name: &str, value: i32) -> String {
format!("{}: {}", name, value)
}
#[instrument]
async fn simple_function() -> String {
"hello".to_string()
}
The #[instrument] macro transforms your async function by:
For example:
#[instrument("span_name({})", arg1)]
async fn foo(arg1: i32, arg2: String) {
// original function body
}
Expands to:
async fn foo(arg1: i32, arg2: String) {
let span = await_tree::span!("span_name({})", arg1);
let fut = async move {
// original function body
};
fut.instrument_await(span).await
}
format!() or println!()await_tree::span!() without modificationYou can chain method calls on the span by including identifiers before the format arguments:
// Chain span methods
#[instrument(long_running, "slow_task")]
async fn slow_task() { /* ... */ }
// Chain multiple methods
#[instrument(long_running, verbose, "complex_task({})", id)]
async fn complex_task(id: u32) { /* ... */ }
// Method calls without format args
#[instrument(long_running, verbose)]
async fn keywords_only() { /* ... */ }
// Any method name works (will fail at compile time if method doesn't exist)
#[instrument(custom_attribute, "task")]
async fn custom_task() { /* ... */ }
The identifiers are processed in order and result in method calls on the span:
long_running → .long_running()verbose → .verbose()custom_attribute → .custom_attribute()If a method doesn't exist on the Span type, the code will fail to compile with a clear error message.
async functionsInstrumentAwait trait to use the generated codeattributes feature must be enabled in the await-tree dependencyLicensed under the Apache License, Version 2.0.