tokio-rs / tracing

Application level tracing for Rust.
https://tracing.rs
MIT License
5.15k stars 673 forks source link

attributes: make `#[instrument]` support string literal fields #2924

Open svix-aaron1011 opened 3 months ago

svix-aaron1011 commented 3 months ago

Motivation

The 'tracing::span!' macro supports passing in field names as string literals, which can be used to use an invalid Rust identifier as a field name (e.g. "type"). However, the 'instrument!' macro required that arguments to 'fields' be period-separated Rust identifiers.

Solution

This commit extends the #[instrument] macro to support both period-separated identifiers and single string-literal fields. We preserve the original argument and pass it to the tracing::span! macro to preserve span/hygiene information.

Fixes #2438

svix-jplatte commented 3 months ago

I found it weird that this would be required and checked why foo.type didn't work without quoting in the first place. Turns out the proc-macro code is actually fine (it uses parse_any) to allow keywords, but one internal macro_rules! that was used by the proc-macro required the field path expression to match the expr fragment-specifier. #2925 fixes that and should thus supersede this PR (though maybe it doesn't hurt to support both if the span! macro also supports literals?).