From 423a1a22608a3c1a6249f0e6895558109fd76b97 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 10 Jun 2022 13:46:23 -0700 Subject: [PATCH] fix histogram (#1440) --- .../exporters/prometheus/exporter_utils.h | 13 ++- exporters/prometheus/src/exporter_utils.cc | 88 ++++++++++--------- 2 files changed, 53 insertions(+), 48 deletions(-) diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h index cfbbb9c48b..c8df4f6cfd 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h @@ -57,7 +57,7 @@ class PrometheusExporterUtils */ template static void SetData(std::vector values, - const std::string &labels, + const opentelemetry::sdk::metrics::PointAttributes &labels, ::prometheus::MetricType type, std::chrono::nanoseconds time, ::prometheus::MetricFamily *metric_family); @@ -70,7 +70,7 @@ class PrometheusExporterUtils static void SetData(std::vector values, const opentelemetry::sdk::metrics::ListType &boundaries, const std::vector &counts, - const std::string &labels, + const opentelemetry::sdk::metrics::PointAttributes &labels, std::chrono::nanoseconds time, ::prometheus::MetricFamily *metric_family); @@ -79,14 +79,13 @@ class PrometheusExporterUtils */ static void SetMetricBasic(::prometheus::ClientMetric &metric, std::chrono::nanoseconds time, - const std::string &labels); + const opentelemetry::sdk::metrics::PointAttributes &labels); /** - * Parse a string of labels (key:value) into a vector of pairs - * {,} - * {l1:v1,l2:v2,...,} + * Convert attribute value to string */ - static std::vector> ParseLabel(std::string labels); + static std::string AttributeValueToString( + const opentelemetry::sdk::common::OwnedAttributeValue &value); /** * Handle Counter and Gauge. diff --git a/exporters/prometheus/src/exporter_utils.cc b/exporters/prometheus/src/exporter_utils.cc index 9979c9bebd..383925f988 100644 --- a/exporters/prometheus/src/exporter_utils.cc +++ b/exporters/prometheus/src/exporter_utils.cc @@ -10,6 +10,8 @@ # include "opentelemetry/exporters/prometheus/exporter_utils.h" # include "opentelemetry/sdk/metrics/export/metric_producer.h" +# include "opentelemetry/sdk/common/global_log_handler.h" + namespace prometheus_client = ::prometheus; namespace metric_sdk = opentelemetry::sdk::metrics; @@ -62,14 +64,14 @@ std::vector PrometheusExporterUtils::TranslateT auto counts = histogram_point_data.counts_; SetData(std::vector{nostd::get(histogram_point_data.sum_), (double)histogram_point_data.count_}, - boundaries, counts, "", time, &metric_family); + boundaries, counts, point_data_attr.attributes, time, &metric_family); } else // Counter, Untyped { auto sum_point_data = nostd::get(point_data_attr.point_data); std::vector values{sum_point_data.value_}; - SetData(values, "", type, time, &metric_family); + SetData(values, point_data_attr.attributes, type, time, &metric_family); } } output.emplace_back(metric_family); @@ -98,6 +100,7 @@ std::string PrometheusExporterUtils::SanitizeNames(std::string name) metric_sdk::AggregationType PrometheusExporterUtils::getAggregationType( const metric_sdk::PointType &point_type) { + if (nostd::holds_alternative(point_type)) { return metric_sdk::AggregationType::kSum; @@ -140,7 +143,7 @@ prometheus_client::MetricType PrometheusExporterUtils::TranslateType( */ template void PrometheusExporterUtils::SetData(std::vector values, - const std::string &labels, + const metric_sdk::PointAttributes &labels, prometheus_client::MetricType type, std::chrono::nanoseconds time, prometheus_client::MetricFamily *metric_family) @@ -159,7 +162,7 @@ template void PrometheusExporterUtils::SetData(std::vector values, const opentelemetry::sdk::metrics::ListType &boundaries, const std::vector &counts, - const std::string &labels, + const metric_sdk::PointAttributes &labels, std::chrono::nanoseconds time, prometheus_client::MetricFamily *metric_family) { @@ -181,59 +184,62 @@ void PrometheusExporterUtils::SetData(std::vector values, */ void PrometheusExporterUtils::SetMetricBasic(prometheus_client::ClientMetric &metric, std::chrono::nanoseconds time, - const std::string &labels) + const metric_sdk::PointAttributes &labels) { metric.timestamp_ms = time.count() / 1000000; - auto label_pairs = ParseLabel(labels); - if (!label_pairs.empty()) + // auto label_pairs = ParseLabel(labels); + if (!labels.empty()) { - metric.label.resize(label_pairs.size()); - for (size_t i = 0; i < label_pairs.size(); ++i) + metric.label.resize(labels.size()); + size_t i = 0; + for (auto const &label : labels) { - auto origin_name = label_pairs[i].first; - auto sanitized = SanitizeNames(origin_name); - metric.label[i].name = sanitized; - metric.label[i].value = label_pairs[i].second; + auto sanitized = SanitizeNames(label.first); + metric.label[i].name = sanitized; + metric.label[i++].value = AttributeValueToString(label.second); } } }; -/** - * Parse a string of labels (key:value) into a vector of pairs - * {,} - * {l1:v1,l2:v2,...,} - */ -std::vector> PrometheusExporterUtils::ParseLabel( - std::string labels) +std::string PrometheusExporterUtils::AttributeValueToString( + const opentelemetry::sdk::common::OwnedAttributeValue &value) { - if (labels.size() < 3) + std::string result; + if (nostd::holds_alternative(value)) { - return {}; + result = nostd::get(value) ? "true" : "false"; } - labels = labels.substr(1, labels.size() - 2); - - std::vector paired_labels; - std::stringstream s_stream(labels); - while (s_stream.good()) + else if (nostd::holds_alternative(value)) { - std::string substr; - getline(s_stream, substr, ','); // get first string delimited by comma - if (!substr.empty()) - { - paired_labels.push_back(substr); - } + result = std::to_string(nostd::get(value)); } - - std::vector> result; - for (auto &paired : paired_labels) + else if (nostd::holds_alternative(value)) { - std::size_t split_index = paired.find(':'); - std::string label = paired.substr(0, split_index); - std::string value = paired.substr(split_index + 1); - result.emplace_back(std::pair(label, value)); + result = std::to_string(nostd::get(value)); + } + else if (nostd::holds_alternative(value)) + { + result = std::to_string(nostd::get(value)); + } + else if (nostd::holds_alternative(value)) + { + result = std::to_string(nostd::get(value)); + } + else if (nostd::holds_alternative(value)) + { + result = std::to_string(nostd::get(value)); + } + else if (nostd::holds_alternative(value)) + { + result = nostd::get(value); + } + else + { + OTEL_INTERNAL_LOG_WARN( + "[Prometheus Exporter] AttributeValueToString - " + " Nested attributes not supported - ignored"); } - return result; }