Skip to content

Commit 940b0ee

Browse files
committed
initial commit
1 parent 17855be commit 940b0ee

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

opentelemetry-proto/src/proto.rs

+18
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,24 @@ pub(crate) mod serializers {
182182
let s: String = Deserialize::deserialize(deserializer)?;
183183
s.parse::<i64>().map_err(de::Error::custom)
184184
}
185+
pub fn serialize_vec_u64_to_strings<S>(vec: &Vec<u64>, serializer: S) -> Result<S::Ok, S::Error>
186+
where
187+
S: Serializer,
188+
{
189+
let str_vec: Vec<String> = vec.iter().map(|&num| num.to_string()).collect();
190+
serializer.collect_seq(str_vec)
191+
}
192+
193+
pub fn deserialize_strings_to_vec_u64<'de, D>(deserializer: D) -> Result<Vec<u64>, D::Error>
194+
where
195+
D: Deserializer<'de>,
196+
{
197+
let str_vec: Vec<String> = Deserialize::deserialize(deserializer)?;
198+
str_vec
199+
.into_iter()
200+
.map(|s| s.parse::<u64>().map_err(de::Error::custom))
201+
.collect()
202+
}
185203
}
186204

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

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

+70
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,13 @@ pub struct HistogramDataPoint {
431431
/// value must be equal to the sum of the "count" fields in buckets if a
432432
/// histogram is provided.
433433
#[prost(fixed64, tag = "4")]
434+
#[cfg_attr(
435+
feature = "with-serde",
436+
serde(
437+
serialize_with = "crate::proto::serializers::serialize_u64_to_string",
438+
deserialize_with = "crate::proto::serializers::deserialize_string_to_u64"
439+
)
440+
)]
434441
pub count: u64,
435442
/// sum of the values in the population. If count is zero then this field
436443
/// must be zero.
@@ -450,6 +457,13 @@ pub struct HistogramDataPoint {
450457
/// The number of elements in bucket_counts array must be by one greater than
451458
/// the number of elements in explicit_bounds array.
452459
#[prost(fixed64, repeated, tag = "6")]
460+
#[cfg_attr(
461+
feature = "with-serde",
462+
serde(
463+
serialize_with = "crate::proto::serializers::serialize_vec_u64_to_strings",
464+
deserialize_with = "crate::proto::serializers::deserialize_strings_to_vec_u64"
465+
)
466+
)]
453467
pub bucket_counts: ::prost::alloc::vec::Vec<u64>,
454468
/// explicit_bounds specifies buckets with explicitly defined bounds for values.
455469
///
@@ -503,17 +517,38 @@ pub struct ExponentialHistogramDataPoint {
503517
/// Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January
504518
/// 1970.
505519
#[prost(fixed64, tag = "2")]
520+
#[cfg_attr(
521+
feature = "with-serde",
522+
serde(
523+
serialize_with = "crate::proto::serializers::serialize_u64_to_string",
524+
deserialize_with = "crate::proto::serializers::deserialize_string_to_u64"
525+
)
526+
)]
506527
pub start_time_unix_nano: u64,
507528
/// TimeUnixNano is required, see the detailed comments above Metric.
508529
///
509530
/// Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January
510531
/// 1970.
511532
#[prost(fixed64, tag = "3")]
533+
#[cfg_attr(
534+
feature = "with-serde",
535+
serde(
536+
serialize_with = "crate::proto::serializers::serialize_u64_to_string",
537+
deserialize_with = "crate::proto::serializers::deserialize_string_to_u64"
538+
)
539+
)]
512540
pub time_unix_nano: u64,
513541
/// count is the number of values in the population. Must be
514542
/// non-negative. This value must be equal to the sum of the "bucket_counts"
515543
/// values in the positive and negative Buckets plus the "zero_count" field.
516544
#[prost(fixed64, tag = "4")]
545+
#[cfg_attr(
546+
feature = "with-serde",
547+
serde(
548+
serialize_with = "crate::proto::serializers::serialize_u64_to_string",
549+
deserialize_with = "crate::proto::serializers::deserialize_string_to_u64"
550+
)
551+
)]
517552
pub count: u64,
518553
/// sum of the values in the population. If count is zero then this field
519554
/// must be zero.
@@ -551,6 +586,13 @@ pub struct ExponentialHistogramDataPoint {
551586
/// Implementations MAY consider the zero bucket to have probability
552587
/// mass equal to (zero_count / count).
553588
#[prost(fixed64, tag = "7")]
589+
#[cfg_attr(
590+
feature = "with-serde",
591+
serde(
592+
serialize_with = "crate::proto::serializers::serialize_u64_to_string",
593+
deserialize_with = "crate::proto::serializers::deserialize_string_to_u64"
594+
)
595+
)]
554596
pub zero_count: u64,
555597
/// positive carries the positive range of exponential bucket counts.
556598
#[prost(message, optional, tag = "8")]
@@ -628,15 +670,36 @@ pub struct SummaryDataPoint {
628670
/// Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January
629671
/// 1970.
630672
#[prost(fixed64, tag = "2")]
673+
#[cfg_attr(
674+
feature = "with-serde",
675+
serde(
676+
serialize_with = "crate::proto::serializers::serialize_u64_to_string",
677+
deserialize_with = "crate::proto::serializers::deserialize_string_to_u64"
678+
)
679+
)]
631680
pub start_time_unix_nano: u64,
632681
/// TimeUnixNano is required, see the detailed comments above Metric.
633682
///
634683
/// Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January
635684
/// 1970.
636685
#[prost(fixed64, tag = "3")]
686+
#[cfg_attr(
687+
feature = "with-serde",
688+
serde(
689+
serialize_with = "crate::proto::serializers::serialize_u64_to_string",
690+
deserialize_with = "crate::proto::serializers::deserialize_string_to_u64"
691+
)
692+
)]
637693
pub time_unix_nano: u64,
638694
/// count is the number of values in the population. Must be non-negative.
639695
#[prost(fixed64, tag = "4")]
696+
#[cfg_attr(
697+
feature = "with-serde",
698+
serde(
699+
serialize_with = "crate::proto::serializers::serialize_u64_to_string",
700+
deserialize_with = "crate::proto::serializers::deserialize_string_to_u64"
701+
)
702+
)]
640703
pub count: u64,
641704
/// sum of the values in the population. If count is zero then this field
642705
/// must be zero.
@@ -704,6 +767,13 @@ pub struct Exemplar {
704767
/// Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January
705768
/// 1970.
706769
#[prost(fixed64, tag = "2")]
770+
#[cfg_attr(
771+
feature = "with-serde",
772+
serde(
773+
serialize_with = "crate::proto::serializers::serialize_u64_to_string",
774+
deserialize_with = "crate::proto::serializers::deserialize_string_to_u64"
775+
)
776+
)]
707777
pub time_unix_nano: u64,
708778
/// (Optional) Span ID of the exemplar trace.
709779
/// span_id may be missing if the measurement is not recorded inside a trace

opentelemetry-proto/tests/grpc_build.rs

+40
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,60 @@ fn build_tonic() {
9797
// the proto file uses u64 for timestamp
9898
// Thus, special serializer and deserializer are needed
9999
for path in [
100+
//trace
100101
"trace.v1.Span.start_time_unix_nano",
101102
"trace.v1.Span.end_time_unix_nano",
102103
"trace.v1.Span.Event.time_unix_nano",
104+
//logs
103105
"logs.v1.LogRecord.time_unix_nano",
104106
"logs.v1.LogRecord.observed_time_unix_nano",
107+
//metrics
105108
"metrics.v1.HistogramDataPoint.start_time_unix_nano",
106109
"metrics.v1.HistogramDataPoint.time_unix_nano",
107110
"metrics.v1.NumberDataPoint.start_time_unix_nano",
108111
"metrics.v1.NumberDataPoint.time_unix_nano",
112+
"metrics.v1.ExponentialHistogramDataPoint.start_time_unix_nano",
113+
"metrics.v1.ExponentialHistogramDataPoint.time_unix_nano",
114+
"metrics.v1.SummaryDataPoint.start_time_unix_nano",
115+
"metrics.v1.SummaryDataPoint.time_unix_nano",
116+
"metrics.v1.Exemplar.time_unix_nano",
109117
] {
110118
builder = builder
111119
.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\"))]")
112120
}
113121

122+
// special serializer and deserializer for metrics count
123+
// OTLP/JSON format may uses string for count
124+
// the proto file uses u64 for count
125+
// Thus, special serializer and deserializer are needed
126+
for path in [
127+
// metrics count and bucket fields
128+
"metrics.v1.HistogramDataPoint.count",
129+
"metrics.v1.ExponentialHistogramDataPoint.count",
130+
"metrics.v1.ExponentialHistogramDataPoint.zero_count",
131+
"metrics.v1.SummaryDataPoint.count",
132+
] {
133+
builder = builder.field_attribute(
134+
path,
135+
"#[cfg_attr(feature = \"with-serde\", serde(serialize_with = \"crate::proto::serializers::serialize_u64_to_string\", deserialize_with = \"crate::proto::serializers::deserialize_string_to_u64\"))]",
136+
);
137+
}
138+
139+
// special serializer and deserializer for metrics bucket counts
140+
// OTLP/JSON format may uses string for bucket counts
141+
// the proto file uses u64 for bucket counts
142+
// Thus, special serializer and deserializer are needed
143+
for path in [
144+
"metrics.v1.HistogramDataPoint.bucket_counts",
145+
"metrics.v1.ExponentialHistogramDataPoint.positive.bucket_counts",
146+
"metrics.v1.ExponentialHistogramDataPoint.negative.bucket_counts",
147+
] {
148+
builder = builder.field_attribute(
149+
path,
150+
"#[cfg_attr(feature = \"with-serde\", serde(serialize_with = \"crate::proto::serializers::serialize_vec_u64_to_strings\", deserialize_with = \"crate::proto::serializers::deserialize_strings_to_vec_u64\"))]",
151+
);
152+
}
153+
114154
// special serializer and deserializer for value
115155
// The Value::value field must be hidden
116156
builder = builder

0 commit comments

Comments
 (0)