Skip to content

Commit e206533

Browse files
authored
fix: JSON serialization of metrics::Examplar and `trace::span::Link… (#2069)
1 parent c3b59ba commit e206533

File tree

8 files changed

+1581
-191
lines changed

8 files changed

+1581
-191
lines changed

opentelemetry-proto/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## vNext
44

5+
- Fix JSON serialization of `metrics::Exemplar` and `trace::span::Link` [#2069](https://github.com/open-telemetry/opentelemetry-rust/pull/2069)
6+
57
## v0.25.0
68
- Update `opentelemetry` dependency version to 0.25
79
- Update `opentelemetry_sdk` dependency version to 0.25

opentelemetry-proto/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ name = "grpc_build"
2424
path = "tests/grpc_build.rs"
2525

2626
[[test]]
27-
name = "json_deserialize"
28-
path = "tests/json_deserialize.rs"
27+
name = "json_serde"
28+
path = "tests/json_serde.rs"
2929

3030

3131
[features]

opentelemetry-proto/src/proto/tonic/opentelemetry.proto.metrics.v1.rs

+24
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub struct MetricsData {
2626
#[cfg_attr(feature = "with-schemars", derive(schemars::JsonSchema))]
2727
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
2828
#[cfg_attr(feature = "with-serde", serde(rename_all = "camelCase"))]
29+
#[cfg_attr(feature = "with-serde", serde(default))]
2930
#[derive(Clone, PartialEq, ::prost::Message)]
3031
pub struct ResourceMetrics {
3132
/// The resource for the metrics in this message.
@@ -47,6 +48,7 @@ pub struct ResourceMetrics {
4748
#[cfg_attr(feature = "with-schemars", derive(schemars::JsonSchema))]
4849
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
4950
#[cfg_attr(feature = "with-serde", serde(rename_all = "camelCase"))]
51+
#[cfg_attr(feature = "with-serde", serde(default))]
5052
#[derive(Clone, PartialEq, ::prost::Message)]
5153
pub struct ScopeMetrics {
5254
/// The instrumentation scope information for the metrics in this message.
@@ -152,6 +154,7 @@ pub struct ScopeMetrics {
152154
#[cfg_attr(feature = "with-schemars", derive(schemars::JsonSchema))]
153155
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
154156
#[cfg_attr(feature = "with-serde", serde(rename_all = "camelCase"))]
157+
#[cfg_attr(feature = "with-serde", serde(default))]
155158
#[derive(Clone, PartialEq, ::prost::Message)]
156159
pub struct Metric {
157160
/// name of the metric.
@@ -214,6 +217,7 @@ pub mod metric {
214217
#[cfg_attr(feature = "with-schemars", derive(schemars::JsonSchema))]
215218
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
216219
#[cfg_attr(feature = "with-serde", serde(rename_all = "camelCase"))]
220+
#[cfg_attr(feature = "with-serde", serde(default))]
217221
#[derive(Clone, PartialEq, ::prost::Message)]
218222
pub struct Gauge {
219223
#[prost(message, repeated, tag = "1")]
@@ -224,6 +228,7 @@ pub struct Gauge {
224228
#[cfg_attr(feature = "with-schemars", derive(schemars::JsonSchema))]
225229
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
226230
#[cfg_attr(feature = "with-serde", serde(rename_all = "camelCase"))]
231+
#[cfg_attr(feature = "with-serde", serde(default))]
227232
#[derive(Clone, PartialEq, ::prost::Message)]
228233
pub struct Sum {
229234
#[prost(message, repeated, tag = "1")]
@@ -241,6 +246,7 @@ pub struct Sum {
241246
#[cfg_attr(feature = "with-schemars", derive(schemars::JsonSchema))]
242247
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
243248
#[cfg_attr(feature = "with-serde", serde(rename_all = "camelCase"))]
249+
#[cfg_attr(feature = "with-serde", serde(default))]
244250
#[derive(Clone, PartialEq, ::prost::Message)]
245251
pub struct Histogram {
246252
#[prost(message, repeated, tag = "1")]
@@ -255,6 +261,7 @@ pub struct Histogram {
255261
#[cfg_attr(feature = "with-schemars", derive(schemars::JsonSchema))]
256262
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
257263
#[cfg_attr(feature = "with-serde", serde(rename_all = "camelCase"))]
264+
#[cfg_attr(feature = "with-serde", serde(default))]
258265
#[derive(Clone, PartialEq, ::prost::Message)]
259266
pub struct ExponentialHistogram {
260267
#[prost(message, repeated, tag = "1")]
@@ -273,6 +280,7 @@ pub struct ExponentialHistogram {
273280
#[cfg_attr(feature = "with-schemars", derive(schemars::JsonSchema))]
274281
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
275282
#[cfg_attr(feature = "with-serde", serde(rename_all = "camelCase"))]
283+
#[cfg_attr(feature = "with-serde", serde(default))]
276284
#[derive(Clone, PartialEq, ::prost::Message)]
277285
pub struct Summary {
278286
#[prost(message, repeated, tag = "1")]
@@ -283,6 +291,7 @@ pub struct Summary {
283291
#[cfg_attr(feature = "with-schemars", derive(schemars::JsonSchema))]
284292
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
285293
#[cfg_attr(feature = "with-serde", serde(rename_all = "camelCase"))]
294+
#[cfg_attr(feature = "with-serde", serde(default))]
286295
#[derive(Clone, PartialEq, ::prost::Message)]
287296
pub struct NumberDataPoint {
288297
/// The set of key/value pairs that uniquely identify the timeseries from
@@ -360,6 +369,7 @@ pub mod number_data_point {
360369
#[cfg_attr(feature = "with-schemars", derive(schemars::JsonSchema))]
361370
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
362371
#[cfg_attr(feature = "with-serde", serde(rename_all = "camelCase"))]
372+
#[cfg_attr(feature = "with-serde", serde(default))]
363373
#[derive(Clone, PartialEq, ::prost::Message)]
364374
pub struct HistogramDataPoint {
365375
/// The set of key/value pairs that uniquely identify the timeseries from
@@ -676,11 +686,25 @@ pub struct Exemplar {
676686
/// span_id may be missing if the measurement is not recorded inside a trace
677687
/// or if the trace is not sampled.
678688
#[prost(bytes = "vec", tag = "4")]
689+
#[cfg_attr(
690+
feature = "with-serde",
691+
serde(
692+
serialize_with = "crate::proto::serializers::serialize_to_hex_string",
693+
deserialize_with = "crate::proto::serializers::deserialize_from_hex_string"
694+
)
695+
)]
679696
pub span_id: ::prost::alloc::vec::Vec<u8>,
680697
/// (Optional) Trace ID of the exemplar trace.
681698
/// trace_id may be missing if the measurement is not recorded inside a trace
682699
/// or if the trace is not sampled.
683700
#[prost(bytes = "vec", tag = "5")]
701+
#[cfg_attr(
702+
feature = "with-serde",
703+
serde(
704+
serialize_with = "crate::proto::serializers::serialize_to_hex_string",
705+
deserialize_with = "crate::proto::serializers::deserialize_from_hex_string"
706+
)
707+
)]
684708
pub trace_id: ::prost::alloc::vec::Vec<u8>,
685709
/// The value of the measurement that was recorded. An exemplar is
686710
/// considered invalid when one of the recognized value fields is not present

opentelemetry-proto/src/proto/tonic/opentelemetry.proto.trace.v1.rs

+14
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,23 @@ pub mod span {
280280
/// A unique identifier of a trace that this linked span is part of. The ID is a
281281
/// 16-byte array.
282282
#[prost(bytes = "vec", tag = "1")]
283+
#[cfg_attr(
284+
feature = "with-serde",
285+
serde(
286+
serialize_with = "crate::proto::serializers::serialize_to_hex_string",
287+
deserialize_with = "crate::proto::serializers::deserialize_from_hex_string"
288+
)
289+
)]
283290
pub trace_id: ::prost::alloc::vec::Vec<u8>,
284291
/// A unique identifier for the linked span. The ID is an 8-byte array.
285292
#[prost(bytes = "vec", tag = "2")]
293+
#[cfg_attr(
294+
feature = "with-serde",
295+
serde(
296+
serialize_with = "crate::proto::serializers::serialize_to_hex_string",
297+
deserialize_with = "crate::proto::serializers::deserialize_from_hex_string"
298+
)
299+
)]
286300
pub span_id: ::prost::alloc::vec::Vec<u8>,
287301
/// The trace_state associated with the link.
288302
#[prost(string, tag = "3")]

opentelemetry-proto/tests/grpc_build.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ fn build_tonic() {
4141
"#[cfg_attr(feature = \"with-serde\", serde(rename_all = \"camelCase\"))]",
4242
);
4343

44-
// optional numeric and String field need to default it to 0 otherwise JSON files without those field
45-
// cannot deserialize
44+
// Optional numeric, string and array fields need to default to their default value otherwise
45+
// JSON files without those field cannot deserialize
4646
// we cannot add serde(default) to all generated types because enums cannot be annotated with serde(default)
4747
for path in [
4848
"trace.v1.Span",
@@ -56,6 +56,16 @@ fn build_tonic() {
5656
"logs.v1.LogRecord",
5757
"logs.v1.ScopeLogs",
5858
"logs.v1.ResourceLogs",
59+
"metrics.v1.Metric",
60+
"metrics.v1.ResourceMetrics",
61+
"metrics.v1.ScopeMetrics",
62+
"metrics.v1.Gauge",
63+
"metrics.v1.Sum",
64+
"metrics.v1.Histogram",
65+
"metrics.v1.ExponentialHistogram",
66+
"metrics.v1.Summary",
67+
"metrics.v1.NumberDataPoint",
68+
"metrics.v1.HistogramDataPoint",
5969
] {
6070
builder = builder.type_attribute(
6171
path,
@@ -71,8 +81,12 @@ fn build_tonic() {
7181
"trace.v1.Span.trace_id",
7282
"trace.v1.Span.span_id",
7383
"trace.v1.Span.parent_span_id",
84+
"trace.v1.Span.Link.trace_id",
85+
"trace.v1.Span.Link.span_id",
7486
"logs.v1.LogRecord.span_id",
7587
"logs.v1.LogRecord.trace_id",
88+
"metrics.v1.Exemplar.span_id",
89+
"metrics.v1.Exemplar.trace_id",
7690
] {
7791
builder = builder
7892
.field_attribute(path, "#[cfg_attr(feature = \"with-serde\", serde(serialize_with = \"crate::proto::serializers::serialize_to_hex_string\", deserialize_with = \"crate::proto::serializers::deserialize_from_hex_string\"))]")

opentelemetry-proto/tests/json_deserialize.rs

-187
This file was deleted.

0 commit comments

Comments
 (0)