Skip to content

Commit

Permalink
fix: try to capture span fields
Browse files Browse the repository at this point in the history
  • Loading branch information
cmackenzie1 committed Oct 9, 2023
1 parent 3308693 commit 2806036
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
7 changes: 1 addition & 6 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,8 @@ where
.map_err(Error::Serde)?;
};

let span = event
.parent()
.and_then(|id| ctx.span(id))
.or_else(|| ctx.lookup_current());

// Write all fields from spans
if let Some(leaf_span) = span {
if let Some(leaf_span) = ctx.lookup_current() {
for span in leaf_span.scope().from_root() {
let ext = span.extensions();
let data = ext
Expand Down
54 changes: 32 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ impl Builder {
S: Subscriber + for<'a> LookupSpan<'a>,
{
tracing_subscriber::fmt::layer()
.event_format(self.events)
.fmt_fields(self.fields)
.event_format(self.events)
}

pub fn subscriber_builder(
Expand All @@ -234,42 +234,48 @@ where
mod tests {

use super::*;

use tracing::{debug, error, info, info_span, trace, warn};
use tracing_core::Level;
use tracing_subscriber::prelude::*;

#[test]
fn test_json_event_formatter() {
let formatter = formatter::JsonEventFormatter::new();
let subscriber = tracing_subscriber::fmt()
.fmt_fields(formatter::FieldsFormatter::new())
.event_format(formatter)
.with_max_level(Level::TRACE)
.finish();
let subscriber = tracing_subscriber::registry().with(builder().layer());

tracing::subscriber::with_default(subscriber, || {
trace!(a = "b", "hello world from trace");
debug!("hello world from debug");
info!("hello world from info");
warn!("hello world from warn");
error!("hello world from error");

let span = info_span!("test_span", b = "b", d = "d", later = tracing::field::Empty,);
let span = info_span!(
"test_span",
person.firstname = "cole",
person.lastname = "mackenzie",
later = tracing::field::Empty,
);
span.in_scope(|| {
info!("some message from inside a info_span");
let inner = info_span!("inner_span", a = "b", c = "d", inner_span = true);
inner.in_scope(|| {
info!(
inner_span_field = true,
later = "populated from inside a span",
"some message from inside a info_span",
);
});
});
});

let formatter = formatter::JsonEventFormatter::new()
.with_level_name("severity")
.with_message_name("msg")
.with_timestamp_name("ts")
.with_timestamp_format(TimestampFormat::Unix);
let subscriber = tracing_subscriber::registry().with(
builder()
.with_level_name("severity")
.with_message_name("message")
.with_timestamp_name("ts")
.with_timestamp_format(TimestampFormat::Unix)
.layer(),
);

let subscriber = tracing_subscriber::fmt()
.fmt_fields(formatter::FieldsFormatter::new())
.event_format(formatter)
.with_max_level(Level::TRACE)
.finish();
tracing::subscriber::with_default(subscriber, || {
trace!(a = "b", "hello world from trace");
debug!("hello world from debug");
Expand All @@ -284,9 +290,13 @@ mod tests {
);
span.in_scope(|| {
info!("some message from inside a info_span");
let inner = info_span!("inner_span", a = "b", c = "d");
let inner = info_span!("inner_span", a = "b", c = "d", inner_span = true);
inner.in_scope(|| {
info!("some message from inside a info_span");
info!(
inner_span_field = true,
later = "populated from inside a span",
"some message from inside a info_span",
);
});
});
});
Expand Down

0 comments on commit 2806036

Please sign in to comment.