Skip to content

Commit 290e4db

Browse files
committed
format log level
incoming logs as JSON might contain a level which we want to send as a status in datadog logs, applied to AWS JSON logs and also internal logs
1 parent dfcb768 commit 290e4db

4 files changed

Lines changed: 369 additions & 19 deletions

File tree

bottlecap/Cargo.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bottlecap/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ tokio = { version = "1.47", default-features = false, features = ["macros", "rt-
3434
tokio-util = { version = "0.7", default-features = false }
3535
tracing = { version = "0.1", default-features = false }
3636
tracing-core = { version = "0.1", default-features = false }
37-
tracing-subscriber = { version = "0.3", default-features = false, features = ["std", "registry", "fmt", "env-filter", "tracing-log"] }
37+
tracing-subscriber = { version = "0.3", default-features = false, features = ["std", "registry", "fmt", "env-filter", "tracing-log", "json"] }
3838
hmac = { version = "0.12", default-features = false }
3939
sha2 = { version = "0.10", default-features = false }
4040
hex = { version = "0.4", default-features = false, features = ["std"] }

bottlecap/src/logger.rs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@ use tracing_subscriber::registry::LookupSpan;
99
#[derive(Debug, Clone, Copy)]
1010
pub struct Formatter;
1111

12+
/// Visitor that captures the message from tracing event fields.
13+
struct MessageVisitor(String);
14+
15+
impl tracing::field::Visit for MessageVisitor {
16+
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn fmt::Debug) {
17+
if field.name() == "message" {
18+
self.0 = format!("{value:?}");
19+
}
20+
}
21+
22+
fn record_str(&mut self, field: &tracing::field::Field, value: &str) {
23+
if field.name() == "message" {
24+
self.0 = value.to_string();
25+
}
26+
}
27+
}
28+
1229
impl<S, N> FormatEvent<S, N> for Formatter
1330
where
1431
S: Subscriber + for<'a> LookupSpan<'a>,
@@ -20,36 +37,38 @@ where
2037
mut writer: format::Writer<'_>,
2138
event: &Event<'_>,
2239
) -> fmt::Result {
23-
// Format values from the event's's metadata:
2440
let metadata = event.metadata();
25-
write!(&mut writer, "DD_EXTENSION | {} | ", metadata.level())?;
41+
let level = metadata.level();
42+
43+
let mut visitor = MessageVisitor(String::new());
44+
event.record(&mut visitor);
2645

27-
// Format all the spans in the event's span context.
46+
// Build span context prefix
47+
let mut span_prefix = String::new();
2848
if let Some(scope) = ctx.event_scope() {
2949
for span in scope.from_root() {
30-
write!(writer, "{}", span.name())?;
31-
32-
// `FormattedFields` is a formatted representation of the span's
33-
// fields, which is stored in its extensions by the `fmt` layer's
34-
// `new_span` method. The fields will have been formatted
35-
// by the same field formatter that's provided to the event
36-
// formatter in the `FmtContext`.
50+
span_prefix.push_str(span.name());
3751
let ext = span.extensions();
3852
let fields = &ext
3953
.get::<FormattedFields<N>>()
4054
.expect("will never be `None`");
41-
42-
// Skip formatting the fields if the span had no fields.
4355
if !fields.is_empty() {
44-
write!(writer, "{{{fields}}}")?;
56+
span_prefix.push('{');
57+
span_prefix.push_str(fields);
58+
span_prefix.push('}');
4559
}
46-
write!(writer, ": ")?;
60+
span_prefix.push_str(": ");
4761
}
4862
}
4963

50-
// Write fields on the event
51-
ctx.field_format().format_fields(writer.by_ref(), event)?;
64+
let message = format!("DD_EXTENSION | {level} | {span_prefix}{}", visitor.0);
65+
66+
// Use serde_json for safe serialization (handles escaping automatically)
67+
let output = serde_json::json!({
68+
"level": level.to_string(),
69+
"message": message,
70+
});
5271

53-
writeln!(writer)
72+
writeln!(writer, "{output}")
5473
}
5574
}

0 commit comments

Comments
 (0)