From d23b9021fb1f77252850ef64a809be6a432e9919 Mon Sep 17 00:00:00 2001 From: Punya Biswal Date: Fri, 22 Sep 2023 14:40:31 -0400 Subject: [PATCH] [exporters/prometheus] Omit underscore suffix for unitless metrics Fixes #2317 --- CHANGELOG.md | 2 ++ exporters/prometheus/src/exporter_utils.cc | 6 +++++- exporters/prometheus/test/exporter_utils_test.cc | 9 +++++++++ exporters/prometheus/test/prometheus_test_helper.h | 4 ++-- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f09553ce70..d397b51ab2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ Increment the: * [DEPRECATION] Deprecate ZPAGES [#2291](https://github.com/open-telemetry/opentelemetry-cpp/pull/2291) +* [EXPORTER] Omit underscore suffix for unitless metrics + [#2323](https://github.com/open-telemetry/opentelemetry-cpp/pull/2323) ## [1.11.0] 2023-08-21 diff --git a/exporters/prometheus/src/exporter_utils.cc b/exporters/prometheus/src/exporter_utils.cc index 966d665df6..e115500f57 100644 --- a/exporters/prometheus/src/exporter_utils.cc +++ b/exporters/prometheus/src/exporter_utils.cc @@ -42,7 +42,11 @@ std::vector PrometheusExporterUtils::TranslateT auto unit = metric_data.instrument_descriptor.unit_; auto sanitized = SanitizeNames(origin_name); prometheus_client::MetricFamily metric_family; - metric_family.name = sanitized + "_" + unit; + metric_family.name = sanitized; + if (!unit.empty()) + { + metric_family.name += "_" + unit; + }; metric_family.help = metric_data.instrument_descriptor.description_; auto time = metric_data.end_ts.time_since_epoch(); for (const auto &point_data_attr : metric_data.point_data_attr_) diff --git a/exporters/prometheus/test/exporter_utils_test.cc b/exporters/prometheus/test/exporter_utils_test.cc index 2222787787..679c291382 100644 --- a/exporters/prometheus/test/exporter_utils_test.cc +++ b/exporters/prometheus/test/exporter_utils_test.cc @@ -157,4 +157,13 @@ TEST(PrometheusExporterUtils, SanitizeName) ASSERT_EQ(exporter::metrics::SanitizeNameTester::sanitize("name?__name:"), "name_name:"); } +TEST(PrometheusExporterUtils, HandleMissingUnit) +{ + TestDataPoints dp; + metric_sdk::ResourceMetrics data = dp.CreateSumPointData(""); + std::vector family = + PrometheusExporterUtils::TranslateToPrometheus(data); + ASSERT_EQ(family.begin()->name, "library_name"); +} + OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/prometheus/test/prometheus_test_helper.h b/exporters/prometheus/test/prometheus_test_helper.h index d42f66c6eb..34ccfe8bcd 100644 --- a/exporters/prometheus/test/prometheus_test_helper.h +++ b/exporters/prometheus/test/prometheus_test_helper.h @@ -25,7 +25,7 @@ struct TestDataPoints /** * Helper function to create ResourceMetrics */ - inline metric_sdk::ResourceMetrics CreateSumPointData() + inline metric_sdk::ResourceMetrics CreateSumPointData(std::string unit = "unit") { metric_sdk::SumPointData sum_point_data{}; sum_point_data.value_ = 10.0; @@ -34,7 +34,7 @@ struct TestDataPoints metric_sdk::ResourceMetrics data; data.resource_ = &resource; metric_sdk::MetricData metric_data{ - metric_sdk::InstrumentDescriptor{"library_name", "description", "unit", + metric_sdk::InstrumentDescriptor{"library_name", "description", unit, metric_sdk::InstrumentType::kCounter, metric_sdk::InstrumentValueType::kDouble}, metric_sdk::AggregationTemporality::kDelta, opentelemetry::common::SystemTimestamp{},