From bc16feb6cf0aefd4f04687e35320184112186a8d Mon Sep 17 00:00:00 2001 From: David Ashpole Date: Fri, 1 Sep 2023 17:39:55 +0000 Subject: [PATCH] add _total suffix to prometheus counters --- CHANGELOG.md | 3 ++ .../exporters/prometheus/exporter_utils.h | 2 + exporters/prometheus/src/exporter_utils.cc | 41 +++++++++++++------ .../prometheus/test/exporter_utils_test.cc | 2 +- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c4ce6f45f..71ac83b1a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,9 @@ Increment the: ## [Unreleased] +* [EXPORTER] Add _total suffixes to Prometheus counters + [#2288](https://github.com/open-telemetry/opentelemetry-cpp/pull/2288) + ## [1.11.0] 2023-08-21 * [BUILD] Fix more cases for symbol name for 32-bit win32 DLL build diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h index ce7f0a1191..9ca7a5d7e1 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h @@ -44,6 +44,8 @@ class PrometheusExporterUtils static opentelemetry::sdk::metrics::AggregationType getAggregationType( const opentelemetry::sdk::metrics::PointType &point_type); + inline bool endsWith(std::string const & value, std::string const & ending); + /** * Translate the OTel metric type to Prometheus metric type */ diff --git a/exporters/prometheus/src/exporter_utils.cc b/exporters/prometheus/src/exporter_utils.cc index 966d665df6..a44c1b64b3 100644 --- a/exporters/prometheus/src/exporter_utils.cc +++ b/exporters/prometheus/src/exporter_utils.cc @@ -38,24 +38,35 @@ std::vector PrometheusExporterUtils::TranslateT { for (const auto &metric_data : instrumentation_info.metric_data_) { - auto origin_name = metric_data.instrument_descriptor.name_; - auto unit = metric_data.instrument_descriptor.unit_; - auto sanitized = SanitizeNames(origin_name); + if (metric_data.point_data_attr_.empty()) { + continue; + } prometheus_client::MetricFamily metric_family; + auto front = metric_data.point_data_attr_.front(); + auto kind = getAggregationType(front.point_data); + bool is_monotonic = true; + if (kind == sdk::metrics::AggregationType::kSum) + { + is_monotonic = + nostd::get(front.point_data).is_monotonic_; + } + auto type = TranslateType(kind, is_monotonic); + metric_family.type = type; + auto origin_name = metric_data.instrument_descriptor.name_; + auto unit = metric_data.instrument_descriptor.unit_; + auto sanitized = SanitizeNames(origin_name); + if (type == prometheus_client::MetricType::Counter && endsWith(sanitized,"_total")) { + // trim _total from counters, since it will be appended after the unit. + sanitized = sanitized.substr(0, sanitized.length() - sizeof("_total")); + } metric_family.name = sanitized + "_" + unit; + if (type == prometheus_client::MetricType::Counter) { + metric_family.name += "_total"; + } 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_) { - auto kind = getAggregationType(point_data_attr.point_data); - bool is_monotonic = true; - if (kind == sdk::metrics::AggregationType::kSum) - { - is_monotonic = - nostd::get(point_data_attr.point_data).is_monotonic_; - } - const prometheus_client::MetricType type = TranslateType(kind, is_monotonic); - metric_family.type = type; if (type == prometheus_client::MetricType::Histogram) // Histogram { auto histogram_point_data = @@ -121,6 +132,12 @@ std::vector PrometheusExporterUtils::TranslateT return output; } +inline bool PrometheusExporterUtils::endsWith(std::string const & value, std::string const & ending) +{ + if (ending.size() > value.size()) return false; + return std::equal(ending.rbegin(), ending.rend(), value.rbegin()); +} + /** * Sanitize the given metric name or label according to Prometheus rule. * diff --git a/exporters/prometheus/test/exporter_utils_test.cc b/exporters/prometheus/test/exporter_utils_test.cc index 2eac7a6d8b..8e01c9a687 100644 --- a/exporters/prometheus/test/exporter_utils_test.cc +++ b/exporters/prometheus/test/exporter_utils_test.cc @@ -114,7 +114,7 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerCounter) auto metric1 = translated[0]; std::vector vals = {10}; - assert_basic(metric1, "library_name", "description", prometheus_client::MetricType::Counter, 1, + assert_basic(metric1, "library_name_total", "description", prometheus_client::MetricType::Counter, 1, vals); }