From 41416ce21f603a2397895d68ba3263eeb55467ef Mon Sep 17 00:00:00 2001 From: jaelrod <64549955+jaelrod@users.noreply.github.com> Date: Sat, 3 Jun 2023 02:35:24 -0400 Subject: [PATCH] [EXPORTER] set is_monotonic only for instrument type kCounter (#2171) --- exporters/otlp/src/otlp_metric_utils.cc | 3 +- .../test/otlp_metrics_serialization_test.cc | 47 +++++++++++++++++-- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/exporters/otlp/src/otlp_metric_utils.cc b/exporters/otlp/src/otlp_metric_utils.cc index c4ab641567..4f56c2f356 100644 --- a/exporters/otlp/src/otlp_metric_utils.cc +++ b/exporters/otlp/src/otlp_metric_utils.cc @@ -50,7 +50,8 @@ void OtlpMetricUtils::ConvertSumMetric(const metric_sdk::MetricData &metric_data { sum->set_aggregation_temporality( GetProtoAggregationTemporality(metric_data.aggregation_temporality)); - sum->set_is_monotonic(true); + sum->set_is_monotonic(metric_data.instrument_descriptor.type_ == + metric_sdk::InstrumentType::kCounter); auto start_ts = metric_data.start_ts.time_since_epoch().count(); auto ts = metric_data.end_ts.time_since_epoch().count(); for (auto &point_data_with_attributes : metric_data.point_data_attr_) diff --git a/exporters/otlp/test/otlp_metrics_serialization_test.cc b/exporters/otlp/test/otlp_metrics_serialization_test.cc index cc3781c66f..ac0f717b25 100644 --- a/exporters/otlp/test/otlp_metrics_serialization_test.cc +++ b/exporters/otlp/test/otlp_metrics_serialization_test.cc @@ -35,7 +35,34 @@ static metrics_sdk::MetricData CreateSumAggregationData() point_data_attr_1.point_data = s_data_1; point_data_attr_2.attributes = {{"k2", "v2"}}; - point_data_attr_2.point_data = s_data_1; + point_data_attr_2.point_data = s_data_2; + std::vector point_data_attr; + point_data_attr.push_back(point_data_attr_1); + point_data_attr.push_back(point_data_attr_2); + data.point_data_attr_ = std::move(point_data_attr); + return data; +} + +static metrics_sdk::MetricData CreateUpDownCounterAggregationData() +{ + metrics_sdk::MetricData data; + data.start_ts = opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now()); + metrics_sdk::InstrumentDescriptor inst_desc = {"UpDownCounter", "Robot Pose Y", "Meter", + metrics_sdk::InstrumentType::kUpDownCounter, + metrics_sdk::InstrumentValueType::kDouble}; + metrics_sdk::SumPointData s_data_1, s_data_2; + s_data_1.value_ = 1.35; + s_data_2.value_ = 1.37; + + data.aggregation_temporality = metrics_sdk::AggregationTemporality::kCumulative; + data.end_ts = opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now()); + data.instrument_descriptor = inst_desc; + metrics_sdk::PointDataAttributes point_data_attr_1, point_data_attr_2; + point_data_attr_1.attributes = {{"environment_id", "DEV-AYS"}}; + point_data_attr_1.point_data = s_data_1; + + point_data_attr_2.attributes = {{"robot_id", "DEV-AYS-03-02"}}; + point_data_attr_2.point_data = s_data_2; std::vector point_data_attr; point_data_attr.push_back(point_data_attr_1); point_data_attr.push_back(point_data_attr_2); @@ -68,7 +95,7 @@ static metrics_sdk::MetricData CreateHistogramAggregationData() point_data_attr_1.point_data = s_data_1; point_data_attr_2.attributes = {{"k2", "v2"}}; - point_data_attr_2.point_data = s_data_1; + point_data_attr_2.point_data = s_data_2; std::vector point_data_attr; point_data_attr.push_back(point_data_attr_1); point_data_attr.push_back(point_data_attr_2); @@ -95,7 +122,7 @@ static metrics_sdk::MetricData CreateObservableGaugeAggregationData() point_data_attr_1.point_data = s_data_1; point_data_attr_2.attributes = {{"k2", "v2"}}; - point_data_attr_2.point_data = s_data_1; + point_data_attr_2.point_data = s_data_2; std::vector point_data_attr; point_data_attr.push_back(point_data_attr_1); point_data_attr.push_back(point_data_attr_2); @@ -120,6 +147,20 @@ TEST(OtlpMetricSerializationTest, Counter) EXPECT_EQ(1, 1); } +TEST(OtlpMetricSerializationTest, UpDownCounter) +{ + metrics_sdk::MetricData data = CreateUpDownCounterAggregationData(); + opentelemetry::proto::metrics::v1::Sum sum; + otlp_exporter::OtlpMetricUtils::ConvertSumMetric(data, &sum); + EXPECT_EQ(sum.aggregation_temporality(), + proto::metrics::v1::AggregationTemporality::AGGREGATION_TEMPORALITY_CUMULATIVE); + EXPECT_EQ(sum.is_monotonic(), false); + EXPECT_EQ(sum.data_points(0).as_double(), 1.35); + EXPECT_EQ(sum.data_points(1).as_double(), 1.37); + + EXPECT_EQ(1, 1); +} + TEST(OtlpMetricSerializationTest, Histogram) { metrics_sdk::MetricData data = CreateHistogramAggregationData();