await-tree-attributes
Procedural attributes for the await-tree crate.
Overview
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.
Usage
Add this to your Cargo.toml:
[]
= { = "0.3", = ["attributes"] }
Then use the #[instrument] attribute on your async functions:
use ;
async
async
async
Attribute Expansion
The #[instrument] macro transforms your async function by:
- Creating an await-tree span with the provided format arguments
- Wrapping the original function body in an async block
- Instrumenting the async block with the span
For example:
async
Expands to:
async
Features
- Format arguments: Pass format strings and arguments just like
format!()orprintln!() - No argument parsing: Format arguments are passed directly to
await_tree::span!()without modification - Function name fallback: If no arguments are provided, uses the function name as the span name
- Preserves function attributes: All function attributes and visibility modifiers are preserved
- Method chaining: Support for chaining any method calls on the span
Method Chaining
You can chain method calls on the span by including identifiers before the format arguments:
// Chain span methods
async
// Chain multiple methods
async
// Method calls without format args
async
// Any method name works (will fail at compile time if method doesn't exist)
async
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.
Requirements
- The macro can only be applied to
asyncfunctions - You must import
InstrumentAwaittrait to use the generated code - The
attributesfeature must be enabled in theawait-treedependency
License
Licensed under the Apache License, Version 2.0.