Tracing Span Instrument Annotations
Enrich tracing spans with structured field annotations, enter guards for synchronous sections, and follow-from causality links.
Code
use tracing::{field::Empty, info_span, Instrument as _, Span};
#[tracing::instrument(name = "charge", fields(order_id, amount_cents, risk = Empty))]
async fn charge(card_token: &str, order_id: u64, amount: u64) -> Result<(), String> {
let span = Span::current();
// 运行中途才能确定的字段:record 到已建 span 上
span.record("amount_cents", amount);
if amount > 1_000_000 {
span.record("risk", "high");
return Err("amount exceeds single-charge limit".into());
}
span.record("risk", "low");
// 模拟网关调用:子 span 自动嵌套
tracing::info!(card_last4 = &card_token[(card_token.len() - 4)..], "contacting gateway");
Ok(())
}
fn sync_section() {
let span = info_span!("sync_transform", rows = 0);
let _guard = span.enter();
for i in 0..3 {
// 同步代码里 span 字段随循环更新
Span::current().record("rows", i + 1);
}
tracing::info!("transform complete");
}
#[tokio::main]
async fn main() {
let outcome = charge("tok_4242424242424242", 991, 25_000).await;
println!("charge result: {:?}", outcome);
let parent = info_span!("request", id = 7);
async {
sync_section();
let child = info_span!("db_query");
child.follows_from(parent.clone());
let _g = child.enter();
tracing::info!("query issued under causality link");
}
.instrument(parent)
.await;
}What the code does
The moving parts are the functions charge, sync_section and main.
The one repetition in this snippet is the loop for i in 0..3. Fallible operations return Result, forcing the caller to handle both arms explicitly.
Watch out for
- Dependency check:
tracing::— not built-in, must be present in the project. - Errors travel through
Result; if you drop the?operators while adapting, each call site needs its ownmatch.
Applying it
Entry signature: fn charge(card_token: &str, order_id: u64, amount: u64) -> Result<(), String>
Running the file prints labeled results such as charge result: — each operation’s effect is visible in the output.
Start the program and main runs charge and sync_section one after another.
Statements that matter
return Err("amount exceeds single-charge limit".into());
fn sync_section() {
let span = info_span!("sync_transform", rows = 0);
let _guard = span.enter();
for i in 0..3 {
// 同步代码里 span 字段随循环更新
Span::current().record("rows", i + 1);
}
tracing::info!("transform complete");
}
Interactive Tools for This Topic
FAQ
- How are errors reported?
- Through the type system: failures are `Result` variants the caller must consume.
- What does this need from the package registry?
- These are real dependencies: `tracing::`. Without it the snippet will not load.
Report a problem with this exampleOpens a pre-filled issue on GitHub.