diff --git a/tracing-serde/src/fields.rs b/tracing-serde/src/fields.rs index f6bab1ab53..eedc72d6b1 100644 --- a/tracing-serde/src/fields.rs +++ b/tracing-serde/src/fields.rs @@ -21,7 +21,7 @@ impl<'a> Serialize for SerializeFieldMap<'a, Event<'_>> { where S: Serializer, { - let len = self.0.metadata().fields().len(); + let len = self.0.fields().count(); let serializer = serializer.serialize_map(Some(len))?; let mut visitor = SerdeMapVisitor::new(serializer); self.0.record(&mut visitor); diff --git a/tracing-subscriber/src/fmt/format/json.rs b/tracing-subscriber/src/fmt/format/json.rs index d26c822df0..1718d64104 100644 --- a/tracing-subscriber/src/fmt/format/json.rs +++ b/tracing-subscriber/src/fmt/format/json.rs @@ -501,7 +501,7 @@ impl<'a> fmt::Debug for WriteAdaptor<'a> { #[cfg(test)] mod test { - use crate::fmt::{test::MockWriter, time::FormatTime}; + use crate::fmt::{format::FmtSpan, test::MockWriter, time::FormatTime}; use lazy_static::lazy_static; use tracing::{self, subscriber::with_default}; @@ -672,6 +672,39 @@ mod test { }); } + #[test] + fn json_span_event() { + // Check span events serialize correctly. + // Discussion: /~https://github.com/tokio-rs/tracing/issues/829#issuecomment-661984255 + // + lazy_static! { + static ref BUF: Mutex> = Mutex::new(vec![]); + } + let expected = r#"{"timestamp":"fake time","level":"INFO","fields":{"message":"enter"},"target":"tracing_subscriber::fmt::format::json::test"}"#; + + let make_writer = || MockWriter::new(&BUF); + let subscriber = crate::fmt::Subscriber::builder() + .json() + .flatten_event(false) + .with_current_span(false) + .with_span_list(false) + .with_span_events(FmtSpan::ENTER) + .with_writer(make_writer) + .with_timer(MockTime) + .finish(); + + with_default(subscriber, || { + tracing::info_span!("valid_json").in_scope(|| {}); + }); + + let actual = String::from_utf8(BUF.try_lock().unwrap().to_vec()).unwrap(); + assert_eq!( + serde_json::from_str::>(expected) + .unwrap(), + serde_json::from_str(actual.as_str()).unwrap() + ); + } + #[cfg(feature = "json")] fn test_json( make_writer: T,