Skip to content

Commit

Permalink
[EXPORTER] set is_monotonic only for instrument type kCounter (#2171)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaelrod authored Jun 3, 2023
1 parent f5fd906 commit 41416ce
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
3 changes: 2 additions & 1 deletion exporters/otlp/src/otlp_metric_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_)
Expand Down
47 changes: 44 additions & 3 deletions exporters/otlp/test/otlp_metrics_serialization_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<metrics_sdk::PointDataAttributes> 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<metrics_sdk::PointDataAttributes> point_data_attr;
point_data_attr.push_back(point_data_attr_1);
point_data_attr.push_back(point_data_attr_2);
Expand Down Expand Up @@ -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<metrics_sdk::PointDataAttributes> point_data_attr;
point_data_attr.push_back(point_data_attr_1);
point_data_attr.push_back(point_data_attr_2);
Expand All @@ -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<metrics_sdk::PointDataAttributes> point_data_attr;
point_data_attr.push_back(point_data_attr_1);
point_data_attr.push_back(point_data_attr_2);
Expand All @@ -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();
Expand Down

0 comments on commit 41416ce

Please sign in to comment.