Skip to content

Commit a193ccb

Browse files
committed
fix ts encoding
1 parent f203b03 commit a193ccb

11 files changed

+48
-9
lines changed

opentelemetry-proto/src/proto.rs

+14
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,21 @@ pub(crate) mod serializers {
6969
Ok(Some(AnyValue { value }))
7070
}
7171

72+
pub fn serialize_u64_to_string<S>(value: &u64, serializer: S) -> Result<S::Ok, S::Error>
73+
where
74+
S: Serializer,
75+
{
76+
let s = value.to_string();
77+
serializer.serialize_str(&s)
78+
}
7279

80+
pub fn deserialize_string_to_u64<'de, D>(deserializer: D) -> Result<u64, D::Error>
81+
where
82+
D: Deserializer<'de>,
83+
{
84+
let s: String = Deserialize::deserialize(deserializer)?;
85+
s.parse::<u64>().map_err(de::Error::custom)
86+
}
7387
}
7488

7589
#[cfg(feature = "gen-tonic-messages")]

opentelemetry-proto/src/proto/tonic/opentelemetry.proto.collector.logs.v1.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// This file is @generated by prost-build.
21
#[cfg_attr(feature = "with-schemars", derive(schemars::JsonSchema))]
32
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
43
#[cfg_attr(feature = "with-serde", serde(rename_all = "camelCase"))]

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

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// This file is @generated by prost-build.
21
#[cfg_attr(feature = "with-schemars", derive(schemars::JsonSchema))]
32
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
43
#[cfg_attr(feature = "with-serde", serde(rename_all = "camelCase"))]

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

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// This file is @generated by prost-build.
21
#[cfg_attr(feature = "with-schemars", derive(schemars::JsonSchema))]
32
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
43
#[cfg_attr(feature = "with-serde", serde(rename_all = "camelCase"))]

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

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// This file is @generated by prost-build.
21
/// AnyValue is used to represent any type of attribute value. AnyValue may contain a
32
/// primitive value such as a string or integer or it may contain an arbitrary nested
43
/// object containing arrays, key-value lists and primitives.

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

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// This file is @generated by prost-build.
21
/// LogsData represents the logs data that can be stored in a persistent storage,
32
/// OR can be embedded by other protocols that transfer OTLP logs data but do not
43
/// implement the OTLP protocol.

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

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// This file is @generated by prost-build.
21
/// MetricsData represents the metrics data that can be stored in a persistent
32
/// storage, OR can be embedded by other protocols that transfer OTLP metrics
43
/// data but do not implement the OTLP protocol.

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

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// This file is @generated by prost-build.
21
/// Resource information.
32
#[cfg_attr(feature = "with-schemars", derive(schemars::JsonSchema))]
43
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// This file is @generated by prost-build.
21
/// TracesData represents the traces data that can be stored in a persistent storage,
32
/// OR can be embedded by other protocols that transfer OTLP traces data but do
43
/// not implement the OTLP protocol.
@@ -166,6 +165,13 @@ pub struct Span {
166165
///
167166
/// This field is semantically required and it is expected that end_time >= start_time.
168167
#[prost(fixed64, tag = "7")]
168+
#[cfg_attr(
169+
feature = "with-serde",
170+
serde(
171+
serialize_with = "crate::proto::serializers::serialize_u64_to_string",
172+
deserialize_with = "crate::proto::serializers::deserialize_string_to_u64"
173+
)
174+
)]
169175
pub start_time_unix_nano: u64,
170176
/// end_time_unix_nano is the end time of the span. On the client side, this is the time
171177
/// kept by the local machine where the span execution ends. On the server side, this
@@ -174,6 +180,13 @@ pub struct Span {
174180
///
175181
/// This field is semantically required and it is expected that end_time >= start_time.
176182
#[prost(fixed64, tag = "8")]
183+
#[cfg_attr(
184+
feature = "with-serde",
185+
serde(
186+
serialize_with = "crate::proto::serializers::serialize_u64_to_string",
187+
deserialize_with = "crate::proto::serializers::deserialize_string_to_u64"
188+
)
189+
)]
177190
pub end_time_unix_nano: u64,
178191
/// attributes is a collection of key/value pairs. Note, global attributes
179192
/// like server name can be set using the resource API. Examples of attributes:
@@ -227,6 +240,13 @@ pub mod span {
227240
pub struct Event {
228241
/// time_unix_nano is the time the event occurred.
229242
#[prost(fixed64, tag = "1")]
243+
#[cfg_attr(
244+
feature = "with-serde",
245+
serde(
246+
serialize_with = "crate::proto::serializers::serialize_u64_to_string",
247+
deserialize_with = "crate::proto::serializers::deserialize_string_to_u64"
248+
)
249+
)]
230250
pub time_unix_nano: u64,
231251
/// name of the event.
232252
/// This field is semantically required to be set to non-empty string.

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

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// This file is @generated by prost-build.
21
#[cfg_attr(feature = "with-schemars", derive(schemars::JsonSchema))]
32
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
43
#[cfg_attr(feature = "with-serde", serde(rename_all = "camelCase"))]

opentelemetry-proto/tests/grpc_build.rs

+13
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ fn build_tonic() {
7373
.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\"))]")
7474
}
7575

76+
// special serializer and deserializer for timestamp
77+
// OTLP/JSON format may uses string for timestamp
78+
// the proto file uses u64 for timestamp
79+
// Thus, special serializer and deserializer are needed
80+
for path in [
81+
"trace.v1.Span.start_time_unix_nano",
82+
"trace.v1.Span.end_time_unix_nano",
83+
"trace.v1.Span.Event.time_unix_nano",
84+
] {
85+
builder = builder
86+
.field_attribute(path, "#[cfg_attr(feature = \"with-serde\", serde(serialize_with = \"crate::proto::serializers::serialize_u64_to_string\", deserialize_with = \"crate::proto::serializers::deserialize_string_to_u64\"))]")
87+
}
88+
7689
// add custom serializer and deserializer for AnyValue
7790
builder = builder
7891
.field_attribute("common.v1.KeyValue.value", "#[cfg_attr(feature =\"with-serde\", serde(serialize_with = \"crate::proto::serializers::serialize_to_value\", deserialize_with = \"crate::proto::serializers::deserialize_from_value\"))]");

0 commit comments

Comments
 (0)