Skip to content

string literal field names don't compile for level! event macros #2748

Closed
@hawkw

Description

@hawkw

Bug Report

Version

tracing 0.1.37

Platform

Any

Crates

tracing

Description

Tracing's macros support string literals as field names. In the documentation, we give an example with a span:

Fields with names that are not Rust identifiers, or with names that are Rust reserved words,
may be created using quoted string literals. However, this may not be used with the local
variable shorthand.

// records an event with fields whose names are not Rust identifiers
//  - "guid:x-request-id", containing a `:`, with the value "abcdef"
//  - "type", which is a reserved word, with the value "request"
span!(Level::TRACE, "api", "guid:x-request-id" = "abcdef", "type" = "request");

This form works when used with the span! and event! macros. However, when used with the level event macros (e.g. info!, debug!, and friends), the string literal form doesn't compile --- the macro appears to interpret this as format arguments, rather than as fields. For example:

use tracing::{self, Level}; // 0.1.37

fn main() {
    let foo = "lol";
    // this compiles
    tracing::span!(Level::TRACE, "api", "guid:x-request-id" = ?foo);
    
    // this compiles
    tracing::trace_span!("api", "guid:x-request-id" = ?foo);
    
    // this compiles
    tracing::event!(Level::TRACE, "guid:x-request-id" = ?foo);
    
    // this doesn't compile
    tracing::trace!("guid:x-request-id" = ?foo)
}

(playground)

We should fix this.

Also, it appears that the macros.rs integration test doesn't contain any tests for this form: https://github.com/tokio-rs/tracing/blob/99e0377a6c48dd88b06ed2ae0259c62d8312c58d/tracing/tests/macros.rs

This is probably why it only works in some cases. 🙃

Metadata

Metadata

Assignees

No one assigned

    Labels

    crate/tracingRelated to the `tracing` cratekind/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions