Skip to content

Add JSON support for DefaultFields #3333

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions tracing-subscriber/src/fmt/format/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
field::{RecordFields, VisitOutput},
fmt::{
fmt_layer::{FmtContext, FormattedFields},
format::DefaultFields,
writer::WriteAdaptor,
},
registry::LookupSpan,
Expand Down Expand Up @@ -123,6 +124,7 @@ impl<Span, N> serde::ser::Serialize for SerializableContext<'_, '_, Span, N>
where
Span: Subscriber + for<'lookup> crate::registry::LookupSpan<'lookup>,
N: for<'writer> FormatFields<'writer> + 'static,
for<'inner, 'outer> SerializableSpan<'inner, 'outer, Span, N>: serde::Serialize,
{
fn serialize<Ser>(&self, serializer_o: Ser) -> Result<Ser::Ok, Ser::Error>
where
Expand All @@ -149,10 +151,41 @@ where
Span: for<'lookup> crate::registry::LookupSpan<'lookup>,
N: for<'writer> FormatFields<'writer> + 'static;

impl<Span, N> serde::ser::Serialize for SerializableSpan<'_, '_, Span, N>
impl<Span> serde::ser::Serialize for SerializableSpan<'_, '_, Span, DefaultFields>
where
Span: for<'lookup> crate::registry::LookupSpan<'lookup>,
{
fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
where
Ser: serde::ser::Serializer,
{
let mut serializer = serializer.serialize_map(None)?;

let ext = self.0.extensions();
let data = ext
.get::<FormattedFields<DefaultFields>>()
.expect("Unable to find FormattedFields in extensions; this is a bug");

let fields = data.split(" ");

for key_value_pair in fields {
let mut kvp_iter = key_value_pair.split("=");
if let (Some(key), Some(value)) = (kvp_iter.next(), kvp_iter.next()) {
// The default formatter surrounds the values in literal quotation marks;
// remove these from the serialization so they don't end up in the JSON
// output.
serializer.serialize_entry(&key, &value[1..value.len() - 1]);
}
}

serializer.serialize_entry("name", self.0.metadata().name())?;
serializer.end()
}
}

impl<Span> serde::ser::Serialize for SerializableSpan<'_, '_, Span, JsonFields>
where
Span: for<'lookup> crate::registry::LookupSpan<'lookup>,
N: for<'writer> FormatFields<'writer> + 'static,
{
fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
where
Expand All @@ -162,7 +195,7 @@ where

let ext = self.0.extensions();
let data = ext
.get::<FormattedFields<N>>()
.get::<FormattedFields<JsonFields>>()
.expect("Unable to find FormattedFields in extensions; this is a bug");

// TODO: let's _not_ do this, but this resolves
Expand Down Expand Up @@ -214,6 +247,7 @@ impl<S, N, T> FormatEvent<S, N> for Format<Json, T>
where
S: Subscriber + for<'lookup> LookupSpan<'lookup>,
N: for<'writer> FormatFields<'writer> + 'static,
for<'inner, 'outer> SerializableSpan<'inner, 'outer, S, N>: serde::Serialize,
T: FormatTime,
{
fn format_event(
Expand Down