diff --git a/tracing-subscriber/src/fmt/format/json.rs b/tracing-subscriber/src/fmt/format/json.rs index 3ef0fcd65..ceb7a6a3a 100644 --- a/tracing-subscriber/src/fmt/format/json.rs +++ b/tracing-subscriber/src/fmt/format/json.rs @@ -3,6 +3,7 @@ use crate::{ field::{RecordFields, VisitOutput}, fmt::{ fmt_layer::{FmtContext, FormattedFields}, + format::DefaultFields, writer::WriteAdaptor, }, registry::LookupSpan, @@ -123,6 +124,7 @@ impl 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(&self, serializer_o: Ser) -> Result where @@ -149,10 +151,41 @@ where Span: for<'lookup> crate::registry::LookupSpan<'lookup>, N: for<'writer> FormatFields<'writer> + 'static; -impl serde::ser::Serialize for SerializableSpan<'_, '_, Span, N> +impl serde::ser::Serialize for SerializableSpan<'_, '_, Span, DefaultFields> +where + Span: for<'lookup> crate::registry::LookupSpan<'lookup>, +{ + fn serialize(&self, serializer: Ser) -> Result + where + Ser: serde::ser::Serializer, + { + let mut serializer = serializer.serialize_map(None)?; + + let ext = self.0.extensions(); + let data = ext + .get::>() + .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 serde::ser::Serialize for SerializableSpan<'_, '_, Span, JsonFields> where Span: for<'lookup> crate::registry::LookupSpan<'lookup>, - N: for<'writer> FormatFields<'writer> + 'static, { fn serialize(&self, serializer: Ser) -> Result where @@ -162,7 +195,7 @@ where let ext = self.0.extensions(); let data = ext - .get::>() + .get::>() .expect("Unable to find FormattedFields in extensions; this is a bug"); // TODO: let's _not_ do this, but this resolves @@ -214,6 +247,7 @@ impl FormatEvent for Format 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(