Skip to content

Commit

Permalink
feat: include fields into nested json object in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
open-schnick committed May 30, 2024
1 parent 521667a commit d84a8e8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
19 changes: 9 additions & 10 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@ impl DatadogLog {
log.insert("timestamp".to_string(), self.timestamp.to_rfc3339().into());
log.insert("level".to_string(), self.level.to_string().into());

let mut message = self.message;

self.fields.sort();

for field in self.fields {
let mut message = self.message;

for field in &self.fields {
// message is just a regular field
if field.name != "message" {
message.push_str(&format!(
" {}={}",
field.name,
field.value.trim_matches('\"')
));
let value = field.value.trim_matches('\"');

message.push_str(&format!(" {}={}", field.name, value));
log.insert(format!("fields.{}", &field.name), value.into());
}
}

Expand Down Expand Up @@ -144,7 +143,7 @@ mod format {
datadog_ids: None,
};

assert_that(sut.format()).is(json!({"timestamp": "2022-01-01T00:00:00+00:00", "level": "INFO", "message": "Hello World! foo=bar", "target": "target"}).to_string());
assert_that(sut.format()).is(json!({"timestamp": "2022-01-01T00:00:00+00:00", "level": "INFO", "fields.foo": "bar", "message": "Hello World! foo=bar", "target": "target"}).to_string());
}

#[test]
Expand Down Expand Up @@ -173,7 +172,7 @@ mod format {
datadog_ids: None,
};

assert_that(sut.format()).is(json!({"timestamp": "2022-01-01T00:00:00+00:00", "level": "INFO", "message": "Hello World! a=c b=b c=a", "target": "target"}).to_string());
assert_that(sut.format()).is(json!({"timestamp": "2022-01-01T00:00:00+00:00", "level": "INFO", "fields.a": "c", "fields.b": "b", "fields.c": "a", "message": "Hello World! a=c b=b c=a", "target": "target"}).to_string());
}

#[macro_export]
Expand Down
8 changes: 4 additions & 4 deletions src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ mod simple_layer {
let events = sink.events();
assert_that(&events).size().is(1);

assert_that(events).first().contains("\",\"level\":\"INFO\",\"message\":\"Hello World! user=John Doe\",\"target\":\"datadog_formatting_layer::layer::simple_layer\"}");
assert_that(events).first().contains("\",\"level\":\"INFO\",\"fields.user\":\"John Doe\",\"message\":\"Hello World! user=John Doe\",\"target\":\"datadog_formatting_layer::layer::simple_layer\"}");
}

#[allow(clippy::redundant_clone)]
Expand All @@ -123,9 +123,9 @@ mod simple_layer {
let events = sink.events();
assert_that(&events).size().is(3);

assert_that(events.clone()).first().contains("\",\"level\":\"DEBUG\",\"message\":\"First Span! first_value=Argument\",\"target\":\"datadog_formatting_layer::layer::setup\"}");
assert_that(events.clone()).second().contains("\",\"level\":\"DEBUG\",\"message\":\"Second Span! attr=value first_value=Argument\",\"target\":\"datadog_formatting_layer::layer::setup\"}");
assert_that(events.clone()).third().contains("\",\"level\":\"INFO\",\"message\":\" attr=value first_value=Argument return=Return Value\",\"target\":\"datadog_formatting_layer::layer::setup\"}");
assert_that(events.clone()).first().contains("\",\"level\":\"DEBUG\",\"fields.first_value\":\"Argument\",\"message\":\"First Span! first_value=Argument\",\"target\":\"datadog_formatting_layer::layer::setup\"}");
assert_that(events.clone()).second().contains("\",\"level\":\"DEBUG\",\"fields.attr\":\"value\",\"fields.first_value\":\"Argument\",\"message\":\"Second Span! attr=value first_value=Argument\",\"target\":\"datadog_formatting_layer::layer::setup\"}");
assert_that(events.clone()).third().contains("\",\"level\":\"INFO\",\"fields.attr\":\"value\",\"fields.first_value\":\"Argument\",\"fields.return\":\"Return Value\",\"message\":\" attr=value first_value=Argument return=Return Value\",\"target\":\"datadog_formatting_layer::layer::setup\"}");
}
}

Expand Down

0 comments on commit d84a8e8

Please sign in to comment.