Skip to content

Commit

Permalink
feat(json): parse str in case it's json value
Browse files Browse the repository at this point in the history
  • Loading branch information
robertohuertasm committed Dec 23, 2023
1 parent 20ef953 commit a6e622b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions dd-tracing-layer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dd-tracing-layer"
version = "0.2.0"
version = "0.3.0"
authors = ["Roberto Huertas <[email protected]>"]
description = "Send your logs to Datadog"
edition = "2021"
Expand All @@ -15,7 +15,7 @@ maintenance = { status = "actively-developed" }
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log-tracing-layer = { path = "../log-tracing-layer", version = "0.2.0" }
log-tracing-layer = { path = "../log-tracing-layer", version = "0.3.0" }
tracing-subscriber = "0.3"
tracing = "0.1"
reqwest = { version = "0.11", features = ["gzip"] }
Expand Down
8 changes: 6 additions & 2 deletions dd-tracing-layer/tests/datadog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ mod tests {
.with_tags("env:dev");
let dd = dd_tracing_layer::create(options);
let subscriber = tracing_subscriber::registry()
// .with(tracing_subscriber::fmt::Layer::new().json())
.with(tracing_subscriber::fmt::Layer::new().json())
.with(dd);
let _s = subscriber::set_default(subscriber);
log("a");
std::thread::sleep(std::time::Duration::from_secs(2));
log("2a");
tracing::info!(
ip = "127.0.0.1",
person = r#"{ "name": "rob", "age": 15 }"#,
message = "Testing Json"
);
std::thread::sleep(std::time::Duration::from_secs(2));
log("3a");
std::thread::sleep(std::time::Duration::from_secs(6));
Expand Down
2 changes: 1 addition & 1 deletion log-tracing-layer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "log-tracing-layer"
version = "0.2.0"
version = "0.3.0"
authors = ["Roberto Huertas <[email protected]>"]
description = "Build your own custom tracing layer."
edition = "2021"
Expand Down
6 changes: 5 additions & 1 deletion log-tracing-layer/src/visitor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::str::FromStr;

use serde_json::json;
use tracing::field::Visit;

Expand All @@ -14,7 +16,9 @@ impl JsonVisitor {
}
impl Visit for JsonVisitor {
fn record_str(&mut self, field: &tracing::field::Field, value: &str) {
self.filter_insert(field, json!(value));
// try to parse the string in case it's already a json value
let json_value = serde_json::Value::from_str(value).unwrap_or_else(|_| json!(value));
self.filter_insert(field, json_value);
}
fn record_bool(&mut self, field: &tracing::field::Field, value: bool) {
self.filter_insert(field, json!(value));
Expand Down

0 comments on commit a6e622b

Please sign in to comment.