Skip to content

Commit

Permalink
Skip serialization of field Span.error if equals to 0 (#500)
Browse files Browse the repository at this point in the history
* Skip serialization of field Span.error if equals to 0

* Add tests

* Move serde_json in dev-dependencies

* Fix clippy
  • Loading branch information
iamluc authored Jun 24, 2024
1 parent 88cb232 commit 7aeb446
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 3 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion LICENSE-3rdparty.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19251,7 +19251,7 @@ third_party_libraries:

END OF TERMS AND CONDITIONS
- package_name: serde_json
package_version: 1.0.115
package_version: 1.0.117
repository: https://github.com/serde-rs/json
license: MIT OR Apache-2.0
licenses:
Expand Down
3 changes: 3 additions & 0 deletions trace-protobuf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ prost = "0.11.6"
serde = { version = "1.0.145", features = ["derive"] }
serde_bytes = "0.11.9"

[dev-dependencies]
serde_json = "1.0.117"

[build-dependencies]
prost-build = { version = "0.11.9", optional = true }
protoc-bin-vendored = { version = "3.0.0", optional = true }
Expand Down
8 changes: 8 additions & 0 deletions trace-protobuf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ fn generate_protobuf() {
".pb.Span.spanLinks",
"#[serde(skip_serializing_if = \"::prost::alloc::vec::Vec::is_empty\")]",
);
config.field_attribute(
".pb.Span.error",
"#[serde(skip_serializing_if = \"is_default\")]",
);

config.type_attribute("StatsPayload", "#[derive(Deserialize, Serialize)]");
config.type_attribute("StatsPayload", "#[serde(rename_all = \"PascalCase\")]");
Expand Down Expand Up @@ -141,6 +145,10 @@ where
Ok(opt.unwrap_or_default())
}
pub fn is_default<T: Default + PartialEq>(t: &T) -> bool {
t == &T::default()
}
"
.as_bytes();

Expand Down
3 changes: 3 additions & 0 deletions trace-protobuf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@

#[rustfmt::skip]
pub mod pb;

#[cfg(test)]
mod pb_test;
5 changes: 5 additions & 0 deletions trace-protobuf/src/pb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ where
Ok(opt.unwrap_or_default())
}

pub fn is_default<T: Default + PartialEq>(t: &T) -> bool {
t == &T::default()
}

#[derive(Deserialize, Serialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down Expand Up @@ -107,6 +111,7 @@ pub struct Span {
#[prost(int32, tag = "9")]
#[serde(default)]
#[serde(deserialize_with = "deserialize_null_into_default")]
#[serde(skip_serializing_if = "is_default")]
pub error: i32,
/// meta is a mapping from tag name to tag value for string-valued tags.
/// @gotags: json:"meta,omitempty" msg:"meta,omitempty"
Expand Down
36 changes: 36 additions & 0 deletions trace-protobuf/src/pb_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0

#[cfg(test)]
mod tests {
use crate::pb::{is_default, Span};

#[test]
fn test_is_default() {
assert!(is_default(&false));
assert!(!is_default(&true));

assert!(is_default(&0));
assert!(!is_default(&1));

assert!(is_default(&""));
assert!(!is_default(&"foo"));
}

#[test]
fn test_serialize_span() {
let mut span = Span {
name: "test".to_string(),
..Default::default()
};

let json = serde_json::to_string(&span).unwrap();
let expected = "{\"service\":\"\",\"name\":\"test\",\"resource\":\"\",\"trace_id\":0,\"span_id\":0,\"parent_id\":0,\"start\":0,\"duration\":0,\"meta\":{},\"metrics\":{},\"type\":\"\"}";
assert_eq!(expected, json);

span.error = 42;
let json = serde_json::to_string(&span).unwrap();
let expected = "{\"service\":\"\",\"name\":\"test\",\"resource\":\"\",\"trace_id\":0,\"span_id\":0,\"parent_id\":0,\"start\":0,\"duration\":0,\"error\":42,\"meta\":{},\"metrics\":{},\"type\":\"\"}";
assert_eq!(expected, json);
}
}

0 comments on commit 7aeb446

Please sign in to comment.