Skip to content

Commit

Permalink
fix json test
Browse files Browse the repository at this point in the history
  • Loading branch information
David Barsky committed Nov 9, 2019
1 parent 989cf0b commit 9dc5402
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
42 changes: 23 additions & 19 deletions tracing-subscriber/src/fmt/format/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::{Format, FormatEvent, FormatFields, FormatTime};
use crate::{
field::MakeVisitor,
fmt::FmtContext,
fmt::FormattedFields,
registry::{LookupMetadata, LookupSpan},
};
use serde::ser::{SerializeMap, Serializer as _};
Expand Down Expand Up @@ -41,6 +42,7 @@ where
where
S: Subscriber + for<'a> LookupSpan<'a> + LookupMetadata,
{
use serde_json::{json, Value};
use tracing_serde::fields::AsMap;
let mut timestamp = String::new();
self.timer.format_time(&mut timestamp)?;
Expand All @@ -59,17 +61,27 @@ where
serializer.serialize_entry("timestamp", &timestamp)?;
serializer.serialize_entry("level", &meta.level().as_serde())?;

// TODO(david): actually enable this; get this to work.
// let span = ctx.ctx.current_span();
// serializer
// .serialize_entry("span", &span)
// .unwrap_or(Ok(()))?;
let id = ctx.ctx.current_span();
let id = id.id();
if let Some(id) = id {
if let Some(span) = ctx.ctx.span(id) {
let ext = span.extensions();
let data = ext
.get::<FormattedFields<N>>()
.expect("Unable to find FormattedFields in extensions; this is a bug");
let data = &data.fmt_fields;
let mut fields: Value = serde_json::from_str(&data)?;
fields["name"] = json!(span.metadata().name());
serializer.serialize_entry("span", &fields).unwrap_or(());
}
}

if self.display_target {
serializer.serialize_entry("target", meta.target())?;
}

serializer.serialize_entry("fields", &event.field_map())
serializer.serialize_entry("fields", &event.field_map())?;
serializer.end()
};

visit().map_err(|_| fmt::Error)?;
Expand Down Expand Up @@ -250,8 +262,7 @@ impl<'a> fmt::Debug for WriteAdaptor<'a> {

#[cfg(test)]
mod test {
use crate::fmt::{format::Format, test::MockWriter, time::FormatTime, FmtLayer};
use crate::{Layer, Registry};
use crate::fmt::{test::MockWriter, time::FormatTime};
use lazy_static::lazy_static;
use tracing::{self, subscriber::with_default};

Expand All @@ -273,7 +284,7 @@ mod test {
let make_writer = || MockWriter::new(&BUF);

let expected =
"{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"span\":{\"name\":\"json_span\",\"fields\":\"{\\\"answer\\\":42,\\\"number\\\":3}\"},\"target\":\"tracing_subscriber::fmt::format::json::test\",\"fields\":{\"message\":\"some json test\"}\n";
"{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"span\":{\"answer\":42,\"name\":\"json_span\",\"number\":3},\"target\":\"tracing_subscriber::fmt::format::json::test\",\"fields\":{\"message\":\"some json test\"}\n";

test_json(make_writer, expected, &BUF);
}
Expand All @@ -283,18 +294,11 @@ mod test {
where
T: crate::fmt::MakeWriter + Send + Sync + 'static,
{
// let subscriber = crate::fmt::Subscriber::builder()
// .json()
// .with_writer(make_writer)
// .with_timer(MockTime)
// .finish();

let format = Format::default().json().with_timer(MockTime);
let fmt = FmtLayer::builder()
let subscriber = crate::fmt::Subscriber::builder()
.json()
.with_writer(make_writer)
.event_format(format)
.with_timer(MockTime)
.finish();
let subscriber = fmt.with_subscriber(Registry::default());

with_default(subscriber, || {
let span = tracing::span!(tracing::Level::INFO, "json_span", answer = 42, number = 3);
Expand Down
2 changes: 1 addition & 1 deletion tracing-subscriber/src/fmt/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{
};
use crate::{
field::{MakeOutput, MakeVisitor, RecordFields, VisitFmt, VisitOutput},
registry::{LookupSpan},
registry::LookupSpan,
};

use std::{
Expand Down

0 comments on commit 9dc5402

Please sign in to comment.