Skip to content

chore: normalize duration so we don't fail parsing spans into protobuf #998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 46 additions & 30 deletions trace-protobuf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ fn main() -> Result<()> {

Ok(())
}

#[cfg(feature = "generate-protobuf")]
fn generate_protobuf() {
let mut config = prost_build::Config::new();
Expand Down Expand Up @@ -61,20 +60,60 @@ fn generate_protobuf() {

config.type_attribute("Span", "#[derive(Deserialize, Serialize)]");
config.field_attribute(
".pb.Span",
"#[serde(default)] #[serde(deserialize_with = \"deserialize_null_into_default\")]",
".pb.Span.service",
"#[serde(default)] #[serde(deserialize_with = \"crate::serializers::deserialize_null_into_default\")]",
);
config.field_attribute(
".pb.Span.name",
"#[serde(default)] #[serde(deserialize_with = \"crate::serializers::deserialize_null_into_default\")]",
);
config.field_attribute(
".pb.Span.resource",
"#[serde(default)] #[serde(deserialize_with = \"crate::serializers::deserialize_null_into_default\")]",
);
config.field_attribute(
".pb.Span.traceID",
"#[serde(default)] #[serde(deserialize_with = \"crate::serializers::deserialize_null_into_default\")]",
);
config.field_attribute(
".pb.Span.spanID",
"#[serde(default)] #[serde(deserialize_with = \"crate::serializers::deserialize_null_into_default\")]",
);
config.field_attribute(
".pb.Span.parentID",
"#[serde(default)] #[serde(deserialize_with = \"crate::serializers::deserialize_null_into_default\")]",
);
config.field_attribute(
".pb.Span.start",
"#[serde(default)] #[serde(deserialize_with = \"crate::serializers::deserialize_null_into_default\")]",
);
config.field_attribute(
".pb.Span.duration",
"#[serde(default)] #[serde(deserialize_with = \"crate::serializers::deserialize_duration\")]",
);
config.field_attribute(
".pb.Span.meta",
"#[serde(default)] #[serde(deserialize_with = \"crate::serializers::deserialize_null_into_default\")]",
);
config.field_attribute(
".pb.Span.metrics",
"#[serde(default)] #[serde(deserialize_with = \"crate::serializers::deserialize_null_into_default\")]",
);
config.field_attribute(
".pb.Span.type",
"#[serde(default)] #[serde(deserialize_with = \"crate::serializers::deserialize_null_into_default\")]",
Comment on lines +63 to +104
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we are only targeting duration, does it make sense to then apply it to everything if we weren't doing it before?

Or is this mainly because we might reject when any of these fields are empty?

);
config.field_attribute(
".pb.Span.meta_struct",
"#[serde(skip_serializing_if = \"::std::collections::HashMap::is_empty\")]",
"#[serde(default)] #[serde(deserialize_with = \"crate::serializers::deserialize_null_into_default\")] #[serde(skip_serializing_if = \"::std::collections::HashMap::is_empty\")]",
);
config.field_attribute(
".pb.Span.spanLinks",
"#[serde(skip_serializing_if = \"::prost::alloc::vec::Vec::is_empty\")]",
"#[serde(default)] #[serde(deserialize_with = \"crate::serializers::deserialize_null_into_default\")] #[serde(skip_serializing_if = \"::prost::alloc::vec::Vec::is_empty\")]",
);
config.field_attribute(
".pb.Span.error",
"#[serde(skip_serializing_if = \"is_default\")]",
"#[serde(default)] #[serde(deserialize_with = \"crate::serializers::deserialize_null_into_default\")] #[serde(skip_serializing_if = \"crate::serializers::is_default\")]",
);

config.type_attribute("StatsPayload", "#[derive(Deserialize, Serialize)]");
Expand Down Expand Up @@ -187,38 +226,15 @@ fn generate_protobuf() {
"
.as_bytes();

let null_deser = &[
license,
"use serde::{Deserialize, Deserializer, Serialize};

fn deserialize_null_into_default<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
T: Default + Deserialize<'de>,
D: Deserializer<'de>,
{
let opt = Option::deserialize(deserializer)?;
Ok(opt.unwrap_or_default())
}

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

"
.as_bytes(),
]
.concat();

let serde_uses = &[
license,
"use serde::{Deserialize, Serialize};

"
.as_bytes(),
]
.concat();

prepend_to_file(null_deser, &output_path.join("pb.rs"));
prepend_to_file(serde_uses, &output_path.join("pb.rs"));
prepend_to_file(serde_uses, &output_path.join("remoteconfig.rs"));
}

Expand Down
1 change: 1 addition & 0 deletions trace-protobuf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
pub mod pb;
pub mod remoteconfig;
mod serde;
mod serializers;

#[cfg(test)]
mod pb_test;
46 changes: 16 additions & 30 deletions trace-protobuf/src/pb.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0

use serde::{Deserialize, Deserializer, Serialize};

fn deserialize_null_into_default<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
T: Default + Deserialize<'de>,
D: Deserializer<'de>,
{
let opt = Option::deserialize(deserializer)?;
Ok(opt.unwrap_or_default())
}

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

use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down Expand Up @@ -66,62 +52,62 @@ pub struct Span {
/// @gotags: json:"service" msg:"service"
#[prost(string, tag = "1")]
#[serde(default)]
#[serde(deserialize_with = "deserialize_null_into_default")]
#[serde(deserialize_with = "crate::serializers::deserialize_null_into_default")]
pub service: ::prost::alloc::string::String,
/// name is the operation name of this span.
/// @gotags: json:"name" msg:"name"
#[prost(string, tag = "2")]
#[serde(default)]
#[serde(deserialize_with = "deserialize_null_into_default")]
#[serde(deserialize_with = "crate::serializers::deserialize_null_into_default")]
pub name: ::prost::alloc::string::String,
/// resource is the resource name of this span, also sometimes called the endpoint (for web spans).
/// @gotags: json:"resource" msg:"resource"
#[prost(string, tag = "3")]
#[serde(default)]
#[serde(deserialize_with = "deserialize_null_into_default")]
#[serde(deserialize_with = "crate::serializers::deserialize_null_into_default")]
pub resource: ::prost::alloc::string::String,
/// traceID is the ID of the trace to which this span belongs.
/// @gotags: json:"trace_id" msg:"trace_id"
#[prost(uint64, tag = "4")]
#[serde(default)]
#[serde(deserialize_with = "deserialize_null_into_default")]
#[serde(deserialize_with = "crate::serializers::deserialize_null_into_default")]
pub trace_id: u64,
/// spanID is the ID of this span.
/// @gotags: json:"span_id" msg:"span_id"
#[prost(uint64, tag = "5")]
#[serde(default)]
#[serde(deserialize_with = "deserialize_null_into_default")]
#[serde(deserialize_with = "crate::serializers::deserialize_null_into_default")]
pub span_id: u64,
/// parentID is the ID of this span's parent, or zero if this span has no parent.
/// @gotags: json:"parent_id" msg:"parent_id"
#[prost(uint64, tag = "6")]
#[serde(default)]
#[serde(deserialize_with = "deserialize_null_into_default")]
#[serde(deserialize_with = "crate::serializers::deserialize_null_into_default")]
pub parent_id: u64,
/// start is the number of nanoseconds between the Unix epoch and the beginning of this span.
/// @gotags: json:"start" msg:"start"
#[prost(int64, tag = "7")]
#[serde(default)]
#[serde(deserialize_with = "deserialize_null_into_default")]
#[serde(deserialize_with = "crate::serializers::deserialize_null_into_default")]
pub start: i64,
/// duration is the time length of this span in nanoseconds.
/// @gotags: json:"duration" msg:"duration"
#[prost(int64, tag = "8")]
#[serde(default)]
#[serde(deserialize_with = "deserialize_null_into_default")]
#[serde(deserialize_with = "crate::serializers::deserialize_duration")]
pub duration: i64,
/// error is 1 if there is an error associated with this span, or 0 if there is not.
/// @gotags: json:"error" msg:"error"
#[prost(int32, tag = "9")]
#[serde(default)]
#[serde(deserialize_with = "deserialize_null_into_default")]
#[serde(skip_serializing_if = "is_default")]
#[serde(deserialize_with = "crate::serializers::deserialize_null_into_default")]
#[serde(skip_serializing_if = "crate::serializers::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"
#[prost(map = "string, string", tag = "10")]
#[serde(default)]
#[serde(deserialize_with = "deserialize_null_into_default")]
#[serde(deserialize_with = "crate::serializers::deserialize_null_into_default")]
pub meta: ::std::collections::HashMap<
::prost::alloc::string::String,
::prost::alloc::string::String,
Expand All @@ -130,19 +116,19 @@ pub struct Span {
/// @gotags: json:"metrics,omitempty" msg:"metrics,omitempty"
#[prost(map = "string, double", tag = "11")]
#[serde(default)]
#[serde(deserialize_with = "deserialize_null_into_default")]
#[serde(deserialize_with = "crate::serializers::deserialize_null_into_default")]
pub metrics: ::std::collections::HashMap<::prost::alloc::string::String, f64>,
/// type is the type of the service with which this span is associated. Example values: web, db, lambda.
/// @gotags: json:"type" msg:"type"
#[prost(string, tag = "12")]
#[serde(default)]
#[serde(deserialize_with = "deserialize_null_into_default")]
#[serde(deserialize_with = "crate::serializers::deserialize_null_into_default")]
pub r#type: ::prost::alloc::string::String,
/// meta_struct is a registry of structured "other" data used by, e.g., AppSec.
/// @gotags: json:"meta_struct,omitempty" msg:"meta_struct,omitempty"
#[prost(map = "string, bytes", tag = "13")]
#[serde(default)]
#[serde(deserialize_with = "deserialize_null_into_default")]
#[serde(deserialize_with = "crate::serializers::deserialize_null_into_default")]
#[serde(skip_serializing_if = "::std::collections::HashMap::is_empty")]
pub meta_struct: ::std::collections::HashMap<
::prost::alloc::string::String,
Expand All @@ -152,7 +138,7 @@ pub struct Span {
/// @gotags: json:"span_links,omitempty" msg:"span_links,omitempty"
#[prost(message, repeated, tag = "14")]
#[serde(default)]
#[serde(deserialize_with = "deserialize_null_into_default")]
#[serde(deserialize_with = "crate::serializers::deserialize_null_into_default")]
#[serde(skip_serializing_if = "::prost::alloc::vec::Vec::is_empty")]
pub span_links: ::prost::alloc::vec::Vec<SpanLink>,
}
Expand Down
3 changes: 2 additions & 1 deletion trace-protobuf/src/pb_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

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

#[test]
fn test_is_default() {
Expand Down
1 change: 0 additions & 1 deletion trace-protobuf/src/remoteconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down
33 changes: 33 additions & 0 deletions trace-protobuf/src/serializers.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

serializer.rs but it's mean for deserializing?
Should we change the name of it?

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0

use serde::{Deserialize, Deserializer};

pub fn deserialize_null_into_default<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
T: Default + Deserialize<'de>,
D: Deserializer<'de>,
{
let opt = Option::deserialize(deserializer)?;
Ok(opt.unwrap_or_default())
}

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

pub fn deserialize_duration<'de, D>(deserializer: D) -> Result<i64, D::Error>
where
D: Deserializer<'de>,
{
let value: Result<i64, D::Error> = Deserialize::deserialize(deserializer);
match value {
Ok(v) => {
if v < 0 {
return Ok(0);
}
Ok(v)
}
Err(_) => Ok(0),
}
}
Loading