From e227ae90f080712b66a175be234b019fa9da930e Mon Sep 17 00:00:00 2001 From: Howard Tam Date: Mon, 3 Jun 2024 10:03:14 -0700 Subject: [PATCH 1/3] Fix serde Make compatible with Collector serde shape --- .../src/proto/tonic/opentelemetry.proto.common.v1.rs | 8 +------- .../src/proto/tonic/opentelemetry.proto.metrics.v1.rs | 3 +++ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/opentelemetry-proto/src/proto/tonic/opentelemetry.proto.common.v1.rs b/opentelemetry-proto/src/proto/tonic/opentelemetry.proto.common.v1.rs index f8fe252297..828e3f8c86 100644 --- a/opentelemetry-proto/src/proto/tonic/opentelemetry.proto.common.v1.rs +++ b/opentelemetry-proto/src/proto/tonic/opentelemetry.proto.common.v1.rs @@ -11,6 +11,7 @@ pub struct AnyValue { /// The value is one of the listed fields. It is valid for all values to be unspecified /// in which case this AnyValue is considered to be "empty". #[prost(oneof = "any_value::Value", tags = "1, 2, 3, 4, 5, 6, 7")] + #[cfg_attr(feature = "with-serde", serde(flatten))] pub value: ::core::option::Option, } /// Nested message and enum types in `AnyValue`. @@ -80,13 +81,6 @@ pub struct KeyValue { #[prost(string, tag = "1")] pub key: ::prost::alloc::string::String, #[prost(message, optional, tag = "2")] - #[cfg_attr( - feature = "with-serde", - serde( - serialize_with = "crate::proto::serializers::serialize_to_value", - deserialize_with = "crate::proto::serializers::deserialize_from_value" - ) - )] pub value: ::core::option::Option, } /// InstrumentationScope is a message representing the instrumentation scope information diff --git a/opentelemetry-proto/src/proto/tonic/opentelemetry.proto.metrics.v1.rs b/opentelemetry-proto/src/proto/tonic/opentelemetry.proto.metrics.v1.rs index 462305014f..d5e27fcc1e 100644 --- a/opentelemetry-proto/src/proto/tonic/opentelemetry.proto.metrics.v1.rs +++ b/opentelemetry-proto/src/proto/tonic/opentelemetry.proto.metrics.v1.rs @@ -181,6 +181,7 @@ pub struct Metric { /// reported value type for the data points, as well as the relatationship to /// the time interval over which they are reported. #[prost(oneof = "metric::Data", tags = "5, 7, 9, 10, 11")] + #[cfg_attr(feature = "with-serde", serde(flatten))] pub data: ::core::option::Option, } /// Nested message and enum types in `Metric`. @@ -325,6 +326,7 @@ pub struct NumberDataPoint { /// The value itself. A point is considered invalid when one of the recognized /// value fields is not present inside this oneof. #[prost(oneof = "number_data_point::Value", tags = "4, 6")] + #[cfg_attr(feature = "with-serde", serde(flatten))] pub value: ::core::option::Option, } /// Nested message and enum types in `NumberDataPoint`. @@ -674,6 +676,7 @@ pub struct Exemplar { /// considered invalid when one of the recognized value fields is not present /// inside this oneof. #[prost(oneof = "exemplar::Value", tags = "3, 6")] + #[cfg_attr(feature = "with-serde", serde(flatten))] pub value: ::core::option::Option, } /// Nested message and enum types in `Exemplar`. From f63c3cc1c8e37edccf38d6ecef7728738c10eb97 Mon Sep 17 00:00:00 2001 From: Howard Tam Date: Mon, 3 Jun 2024 14:32:45 -0700 Subject: [PATCH 2/3] Update build script --- opentelemetry-proto/tests/grpc_build.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/opentelemetry-proto/tests/grpc_build.rs b/opentelemetry-proto/tests/grpc_build.rs index 8477a4e626..8334e53c0f 100644 --- a/opentelemetry-proto/tests/grpc_build.rs +++ b/opentelemetry-proto/tests/grpc_build.rs @@ -94,11 +94,22 @@ fn build_tonic() { } // add custom serializer and deserializer for AnyValue - for path in ["common.v1.KeyValue.value", "logs.v1.LogRecord.body"] { + for path in ["logs.v1.LogRecord.body"] { builder = builder .field_attribute(path, "#[cfg_attr(feature =\"with-serde\", serde(serialize_with = \"crate::proto::serializers::serialize_to_value\", deserialize_with = \"crate::proto::serializers::deserialize_from_value\"))]"); } + // flatten + for path in [ + "common.v1.AnyValue.value", + "metrics.v1.Metric.data", + "metrics.v1.NumberDataPoint.value", + "metrics.v1.Exemplar.value", + ] { + builder = + builder.field_attribute(path, "#[cfg_attr(feature =\"with-serde\", serde(flatten))]"); + } + builder .out_dir(out_dir.path()) .compile(TONIC_PROTO_FILES, TONIC_INCLUDES) From e9556ec72c4878fbfb46412805f36cef294ffe22 Mon Sep 17 00:00:00 2001 From: Howard Tam Date: Mon, 3 Jun 2024 14:40:23 -0700 Subject: [PATCH 3/3] Fix lint --- opentelemetry-proto/tests/grpc_build.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/opentelemetry-proto/tests/grpc_build.rs b/opentelemetry-proto/tests/grpc_build.rs index 8334e53c0f..0eb761d897 100644 --- a/opentelemetry-proto/tests/grpc_build.rs +++ b/opentelemetry-proto/tests/grpc_build.rs @@ -94,10 +94,8 @@ fn build_tonic() { } // add custom serializer and deserializer for AnyValue - for path in ["logs.v1.LogRecord.body"] { - builder = builder - .field_attribute(path, "#[cfg_attr(feature =\"with-serde\", serde(serialize_with = \"crate::proto::serializers::serialize_to_value\", deserialize_with = \"crate::proto::serializers::deserialize_from_value\"))]"); - } + builder = builder + .field_attribute("logs.v1.LogRecord.body", "#[cfg_attr(feature =\"with-serde\", serde(serialize_with = \"crate::proto::serializers::serialize_to_value\", deserialize_with = \"crate::proto::serializers::deserialize_from_value\"))]"); // flatten for path in [