tracing icon indicating copy to clipboard operation
tracing copied to clipboard

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

Open svix-aaron1011 opened this issue 4 months ago • 1 comments

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-aaron1011 avatar Apr 02 '24 16:04 svix-aaron1011

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?).

svix-jplatte avatar Apr 03 '24 13:04 svix-jplatte