From f0109eb98968d5d3de650bc990a9bc2d59e5b313 Mon Sep 17 00:00:00 2001 From: Ehsan Saei <71217171+esigo@users.noreply.github.com> Date: Wed, 10 Aug 2022 21:28:54 +0200 Subject: [PATCH] Prometheus exporter support Gauge Type (#1553) --- exporters/prometheus/src/exporter_utils.cc | 41 +++++++++++++++++-- .../prometheus/test/exporter_utils_test.cc | 31 ++++++++++++-- 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/exporters/prometheus/src/exporter_utils.cc b/exporters/prometheus/src/exporter_utils.cc index 80f45d823e..44f4395ece 100644 --- a/exporters/prometheus/src/exporter_utils.cc +++ b/exporters/prometheus/src/exporter_utils.cc @@ -5,6 +5,7 @@ # include # include # include +# include "prometheus/metric_family.h" # include # include "opentelemetry/exporters/prometheus/exporter_utils.h" @@ -74,12 +75,38 @@ std::vector PrometheusExporterUtils::TranslateT SetData(std::vector{sum, (double)histogram_point_data.count_}, boundaries, counts, point_data_attr.attributes, time, &metric_family); } + else if (type == prometheus_client::MetricType::Gauge) + { + if (nostd::holds_alternative( + point_data_attr.point_data)) + { + auto last_value_point_data = + nostd::get(point_data_attr.point_data); + std::vector values{last_value_point_data.value_}; + SetData(values, point_data_attr.attributes, type, time, &metric_family); + } + else + { + OTEL_INTERNAL_LOG_WARN( + "[Prometheus Exporter] TranslateToPrometheus - " + "invalid LastValuePointData type"); + } + } else // Counter, Untyped { - auto sum_point_data = - nostd::get(point_data_attr.point_data); - std::vector values{sum_point_data.value_}; - SetData(values, point_data_attr.attributes, type, time, &metric_family); + if (nostd::holds_alternative(point_data_attr.point_data)) + { + auto sum_point_data = + nostd::get(point_data_attr.point_data); + std::vector values{sum_point_data.value_}; + SetData(values, point_data_attr.attributes, type, time, &metric_family); + } + else + { + OTEL_INTERNAL_LOG_WARN( + "[Prometheus Exporter] TranslateToPrometheus - " + "invalid SumPointData type"); + } } } output.emplace_back(metric_family); @@ -140,6 +167,8 @@ prometheus_client::MetricType PrometheusExporterUtils::TranslateType( return prometheus_client::MetricType::Counter; case metric_sdk::AggregationType::kHistogram: return prometheus_client::MetricType::Histogram; + case metric_sdk::AggregationType::kLastValue: + return prometheus_client::MetricType::Gauge; default: return prometheus_client::MetricType::Untyped; } @@ -276,6 +305,10 @@ void PrometheusExporterUtils::SetValue(std::vector values, metric->counter.value = value; break; } + case prometheus_client::MetricType::Gauge: { + metric->gauge.value = value; + break; + } case prometheus_client::MetricType::Untyped: { metric->untyped.value = value; break; diff --git a/exporters/prometheus/test/exporter_utils_test.cc b/exporters/prometheus/test/exporter_utils_test.cc index cb09b27964..2f103b702b 100644 --- a/exporters/prometheus/test/exporter_utils_test.cc +++ b/exporters/prometheus/test/exporter_utils_test.cc @@ -43,10 +43,11 @@ void assert_basic(prometheus_client::MetricFamily &metric, ASSERT_EQ(buckets.size(), vals[2]); break; } - case prometheus_client::MetricType::Gauge: - // Gauge type not supported - ASSERT_TRUE(false); + case prometheus_client::MetricType::Gauge: { + ASSERT_DOUBLE_EQ(metric_data.gauge.value, vals[0]); break; + } + break; case prometheus_client::MetricType::Summary: // Summary type not supported ASSERT_TRUE(false); @@ -110,6 +111,30 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerCounter) vals); } +TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerLastValue) +{ + std::vector> collection; + + collection.emplace_back(new metric_sdk::ResourceMetrics{CreateLastValuePointData()}); + + auto translated = PrometheusExporterUtils::TranslateToPrometheus(collection); + ASSERT_EQ(translated.size(), collection.size()); + + auto metric1 = translated[0]; + std::vector vals = {10}; + assert_basic(metric1, "library_name", "description", prometheus_client::MetricType::Gauge, 1, + vals); + + collection.emplace_back(new metric_sdk::ResourceMetrics{CreateLastValuePointData()}); + + translated = PrometheusExporterUtils::TranslateToPrometheus(collection); + ASSERT_EQ(translated.size(), collection.size()); + + auto metric2 = translated[1]; + assert_basic(metric2, "library_name", "description", prometheus_client::MetricType::Gauge, 1, + vals); +} + TEST(PrometheusExporterUtils, TranslateToPrometheusHistogramNormal) { std::vector> collection;