Skip to content

Commit a80beb2

Browse files
update data type based on data from otel collector
1 parent a8303ab commit a80beb2

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

src/handlers/http/otel/opentelemetry.proto.metrics.v1.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ pub struct ExponentialHistogramDataPoint {
412412
/// count is the number of values in the population. Must be
413413
/// non-negative. This value must be equal to the sum of the "bucket_counts"
414414
/// values in the positive and negative Buckets plus the "zero_count" field.
415-
pub count: Option<u64>,
415+
pub count: Option<String>,
416416
/// sum of the values in the population. If count is zero then this field
417417
/// must be zero.
418418
///
@@ -516,7 +516,7 @@ pub struct SummaryDataPoint {
516516
/// 1970.
517517
pub time_unix_nano: Option<String>,
518518
/// count is the number of values in the population. Must be non-negative.
519-
pub count: Option<u64>,
519+
pub count: Option<String>,
520520
/// sum of the values in the population. If count is zero then this field
521521
/// must be zero.
522522
///
@@ -535,7 +535,7 @@ pub struct SummaryDataPoint {
535535
}
536536
/// Nested message and enum types in `SummaryDataPoint`.
537537
pub mod summary_data_point {
538-
use serde::{Deserialize, Serialize};
538+
use serde::{Deserialize, Deserializer, Serialize};
539539
/// Represents the value at a given quantile of a distribution.
540540
///
541541
/// To record Min and Max values following conventions are used:
@@ -553,8 +553,34 @@ pub mod summary_data_point {
553553
/// The value at the given quantile of a distribution.
554554
///
555555
/// Quantile values must NOT be negative.
556+
#[serde(deserialize_with = "deserialize_f64_or_nan")]
556557
pub value: Option<f64>,
557558
}
559+
560+
fn deserialize_f64_or_nan<'de, D>(deserializer: D) -> Result<Option<f64>, D::Error> where D: Deserializer<'de>,
561+
{
562+
struct StringOrFloatVisitor;
563+
impl<'de> serde::de::Visitor<'de> for StringOrFloatVisitor
564+
{
565+
type Value = Option<f64>;
566+
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result
567+
{
568+
formatter.write_str("a string or a floating-point number")
569+
}
570+
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: serde::de::Error,
571+
{
572+
if value == "NaN"
573+
{
574+
Ok(Some(f64::NAN))
575+
} else {
576+
value.parse::<f64>().map(Some).map_err(E::custom)
577+
} }
578+
fn visit_f64<E>(self, value: f64) -> Result<Self::Value, E> where E: serde::de::Error,
579+
{
580+
Ok(Some(value))
581+
}
582+
}
583+
deserializer.deserialize_any(StringOrFloatVisitor) }
558584
}
559585
/// A representation of an exemplar, which is a sample input measurement.
560586
/// Exemplars also hold information about the environment when the measurement

src/handlers/http/otel_metrics.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ fn flatten_exp_histogram(exp_histogram: &ExponentialHistogram) -> Vec<BTreeMap<S
601601
if data_point.count.is_some() {
602602
data_point_json.insert(
603603
"exponential_histogram_data_point_count".to_string(),
604-
Value::Number(serde_json::Number::from(data_point.count.unwrap())),
604+
Value::String(data_point.count.as_ref().unwrap().to_string()),
605605
);
606606
}
607607

@@ -791,7 +791,7 @@ fn flatten_summary(summary: &Summary) -> Vec<BTreeMap<String, Value>> {
791791
if data_point.count.is_some() {
792792
data_point_json.insert(
793793
"summary_data_point_count".to_string(),
794-
Value::Number(serde_json::Number::from(data_point.count.unwrap())),
794+
Value::String(data_point.count.as_ref().unwrap().to_string()),
795795
);
796796
}
797797

@@ -803,10 +803,11 @@ fn flatten_summary(summary: &Summary) -> Vec<BTreeMap<String, Value>> {
803803
}
804804

805805
if let Some(quantile_values) = data_point.quantile_values.as_ref() {
806+
let mut quantile_ctr = 1;
806807
for quantile_value in quantile_values.iter() {
807808
if quantile_value.quantile.is_some() {
808809
data_point_json.insert(
809-
"summary_quantile_value_quantile".to_string(),
810+
format!("summary_quantile_value_quantile_{}", quantile_ctr),
810811
Value::Number(
811812
serde_json::Number::from_f64(quantile_value.quantile.unwrap())
812813
.unwrap(),
@@ -816,13 +817,12 @@ fn flatten_summary(summary: &Summary) -> Vec<BTreeMap<String, Value>> {
816817

817818
if quantile_value.value.is_some() {
818819
data_point_json.insert(
819-
"summary_quantile_value_value".to_string(),
820-
Value::Number(
821-
serde_json::Number::from_f64(quantile_value.value.unwrap())
822-
.unwrap(),
823-
),
820+
format!("summary_quantile_value_value_{}", quantile_ctr),
821+
Value::String(quantile_value.value.as_ref().unwrap().to_string()),
824822
);
825823
}
824+
825+
quantile_ctr += 1;
826826
}
827827
}
828828

0 commit comments

Comments
 (0)