Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic when using JSON format and #[instrument] #1776

Closed
gahag-cw opened this issue Dec 13, 2021 · 2 comments
Closed

Panic when using JSON format and #[instrument] #1776

gahag-cw opened this issue Dec 13, 2021 · 2 comments

Comments

@gahag-cw
Copy link

Bug Report

Version

├── tracing v0.1.29
│   ├── tracing-attributes v0.1.18 (proc-macro)
│   └── tracing-core v0.1.21
└── tracing-subscriber v0.3.3
    ├── tracing-core v0.1.21 (*)
    ├── tracing-log v0.1.2
    │   └── tracing-core v0.1.21 (*)
    └── tracing-serde v0.1.2
        └── tracing-core v0.1.21 (*)

Platform

Linux 5.10.84-1-lts #1 SMP Wed, 08 Dec 2021 10:17:01 +0000 x86_64 GNU/Linux

Crates

  • tracing-subscriber

Description

When using the JSON format in conjunction with the #[instrument] macro, we get a panic.

Minimal code:

fn main() {
    tracing_subscriber::fmt()
        .event_format(tracing_subscriber::fmt::format().json())
        .init();

    test("hai")
}

#[tracing::instrument]
fn test(input: &str) {
    tracing::info!("got: {}", input);
}

Which results in:

thread 'main' panicked at 'span 'test' had malformed fields! this is a bug.
  error: expected value at line 1 column 1
  fields: FormattedFields { fields: "input=\"hai\"", formatter: tracing_subscriber::fmt::format::DefaultFields }', ~/.local/share/cargo/registry/src/github.com-1ecc6299db9ec823/tracing-subscriber-0.3.0/src/fmt/format/json.rs:166:49
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

If we comment the #[tracing::instrument] line, it works as expected.

@davidbarsky
Copy link
Member

Sorry for the delay and the fact that you encountered this issue. The issue is the same as #1365; you'll also need add a JSON-specific field formatter, which is needed to record span fields (in reproduction, that would be input).

tracing_subscriber::fmt()
    .event_format(tracing_subscriber::fmt::format().json())
    .fmt_fields(tracing_subscriber::fmt::format::JsonFields::new())
    .init();

An alternative would be to use the .json() builder method on fmt::SubscriberBuilder:

tracing_subscriber::fmt()
    .json()
    .init();

@gahag-cw
Copy link
Author

No worries, thanks for taking your time into this. Eventually, I stumbled on the json example in the repository and found out that I should use tracing_subscriber::fmt().json() directly. Not sure why I was using it the other way previously, perhaps there is some tutorial somewhere that's misleading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants