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

subscriber: improve test for #1333 #1350

Merged
merged 1 commit into from
Apr 9, 2021
Merged
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
63 changes: 36 additions & 27 deletions tracing-subscriber/src/fmt/format/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,31 +613,13 @@ mod test {
// This test reproduces issue #707, where using `Span::record` causes
// any events inside the span to be ignored.

let make_writer = MockMakeWriter::default();
let subscriber = crate::fmt()
.json()
.with_writer(make_writer.clone())
.finish();

let parse_buf = || -> serde_json::Value {
let buf = String::from_utf8(make_writer.buf().to_vec()).unwrap();
let json = buf
.lines()
.last()
.expect("expected at least one line to be written!");
match serde_json::from_str(&json) {
Ok(v) => v,
Err(e) => panic!(
"assertion failed: JSON shouldn't be malformed\n error: {}\n json: {}",
e, json
),
}
};
let buffer = MockMakeWriter::default();
let subscriber = crate::fmt().json().with_writer(buffer.clone()).finish();

with_default(subscriber, || {
tracing::info!("an event outside the root span");
assert_eq!(
parse_buf()["fields"]["message"],
parse_as_json(&buffer)["fields"]["message"],
"an event outside the root span"
);

Expand All @@ -647,7 +629,7 @@ mod test {

tracing::info!("an event inside the root span");
assert_eq!(
parse_buf()["fields"]["message"],
parse_as_json(&buffer)["fields"]["message"],
"an event inside the root span"
);
});
Expand All @@ -658,18 +640,45 @@ mod test {
// Check span events serialize correctly.
// Discussion: /~https://github.com/tokio-rs/tracing/issues/829#issuecomment-661984255
//
let expected = r#"{"timestamp":"fake time","level":"INFO","fields":{"message":"enter"},"target":"tracing_subscriber::fmt::format::json::test"}"#;
let collector = collector()
let buffer = MockMakeWriter::default();
let subscriber = collector()
.with_writer(buffer.clone())
.flatten_event(false)
.with_current_span(false)
.with_span_list(false)
.with_span_events(FmtSpan::ENTER);
.with_span_events(FmtSpan::FULL)
.finish();

test_json(expected, collector, || {
tracing::info_span!("valid_json").in_scope(|| {});
with_default(subscriber, || {
let span = tracing::info_span!("valid_json");
assert_eq!(parse_as_json(&buffer)["fields"]["message"], "new");

let _enter = span.enter();
assert_eq!(parse_as_json(&buffer)["fields"]["message"], "enter");

drop(_enter);
assert_eq!(parse_as_json(&buffer)["fields"]["message"], "exit");

drop(span);
assert_eq!(parse_as_json(&buffer)["fields"]["message"], "close");
});
}

fn parse_as_json(buffer: &MockMakeWriter) -> serde_json::Value {
let buf = String::from_utf8(buffer.buf().to_vec()).unwrap();
let json = buf
.lines()
.last()
.expect("expected at least one line to be written!");
match serde_json::from_str(&json) {
Ok(v) => v,
Err(e) => panic!(
"assertion failed: JSON shouldn't be malformed\n error: {}\n json: {}",
e, json
),
}
}

fn test_json<T>(
expected: &str,
builder: crate::fmt::CollectorBuilder<JsonFields, Format<Json>>,
Expand Down