Skip to content

Commit

Permalink
Use serde_bytes for meta_struct
Browse files Browse the repository at this point in the history
Otherwise meta_struct will be serialized as an array of integers instead of a byte string, breaking the expected representation.

Signed-off-by: Bob Weinand <bob.weinand@datadoghq.com>
  • Loading branch information
bwoebi committed Jul 19, 2024
1 parent d351d66 commit 0a36ced
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
31 changes: 31 additions & 0 deletions trace-protobuf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ fn generate_protobuf() {
".pb.Span.error",
"#[serde(skip_serializing_if = \"is_default\")]",
);
config.field_attribute(
".pb.Span.meta_struct",
"#[serde(serialize_with = \"serialize_meta_struct\")]",
);

config.type_attribute("StatsPayload", "#[derive(Deserialize, Serialize)]");
config.type_attribute("StatsPayload", "#[serde(rename_all = \"PascalCase\")]");
Expand Down Expand Up @@ -131,6 +135,33 @@ fn generate_protobuf() {
)
.unwrap();

let fn_serialize_meta_struct = "
fn serialize_meta_struct<S>(input: &::std::collections::HashMap<
::prost::alloc::string::String,
::prost::alloc::vec::Vec<u8>,
>, serializer: S) -> Result<S::Ok, S::Error>
where S: serde::Serializer
{
unsafe {
::std::mem::transmute::<&::std::collections::HashMap<
::prost::alloc::string::String,
::prost::alloc::vec::Vec<u8>,
>, &::std::collections::HashMap<
::prost::alloc::string::String,
BytesWrapper,
>>(input)
}.serialize(serializer)
}
#[derive(Serialize)]
#[repr(transparent)]
struct BytesWrapper(#[serde(with = \"serde_bytes\")] Vec<u8>);
"
.as_bytes();

prepend_to_file(fn_serialize_meta_struct, &output_path.join("pb.rs"));

// add license, serde imports, custom deserializer code to the top of the protobuf rust structs
// file
let add_to_top = "// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
Expand Down
22 changes: 22 additions & 0 deletions trace-protobuf/src/pb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ pub fn is_default<T: Default + PartialEq>(t: &T) -> bool {
t == &T::default()
}


fn serialize_meta_struct<S>(input: &::std::collections::HashMap<
::prost::alloc::string::String,
::prost::alloc::vec::Vec<u8>,
>, serializer: S) -> Result<S::Ok, S::Error>
where S: serde::Serializer
{
unsafe {
::std::mem::transmute::<&::std::collections::HashMap<
::prost::alloc::string::String,
::prost::alloc::vec::Vec<u8>,
>, &::std::collections::HashMap<
::prost::alloc::string::String,
BytesWrapper,
>>(input)
}.serialize(serializer)
}

#[derive(Serialize)]
struct BytesWrapper(#[serde(with = "serde_bytes")] Vec<u8>);

#[derive(Deserialize, Serialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down Expand Up @@ -140,6 +161,7 @@ pub struct Span {
#[serde(default)]
#[serde(deserialize_with = "deserialize_null_into_default")]
#[serde(skip_serializing_if = "::std::collections::HashMap::is_empty")]
#[serde(serialize_with = "serialize_meta_struct")]
pub meta_struct: ::std::collections::HashMap<
::prost::alloc::string::String,
::prost::alloc::vec::Vec<u8>,
Expand Down

0 comments on commit 0a36ced

Please sign in to comment.