diff --git a/CHANGELOG.md b/CHANGELOG.md index f09553ce70..60f5968e0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,9 @@ Increment the: * [DEPRECATION] Deprecate ZPAGES [#2291](https://github.com/open-telemetry/opentelemetry-cpp/pull/2291) +* [EXPORTER] Add otel_scope_name and otel_scope_version labels to the prometheus + exporter. + [#2293](https://github.com/open-telemetry/opentelemetry-cpp/pull/2293) ## [1.11.0] 2023-08-21 diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h index ce7f0a1191..8d547f0589 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h @@ -57,6 +57,7 @@ class PrometheusExporterUtils template static void SetData(std::vector values, const opentelemetry::sdk::metrics::PointAttributes &labels, + const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope, ::prometheus::MetricType type, std::chrono::nanoseconds time, ::prometheus::MetricFamily *metric_family); @@ -70,15 +71,18 @@ class PrometheusExporterUtils const std::vector &boundaries, const std::vector &counts, const opentelemetry::sdk::metrics::PointAttributes &labels, + const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope, std::chrono::nanoseconds time, ::prometheus::MetricFamily *metric_family); /** * Set time and labels to metric data */ - static void SetMetricBasic(::prometheus::ClientMetric &metric, - std::chrono::nanoseconds time, - const opentelemetry::sdk::metrics::PointAttributes &labels); + static void SetMetricBasic( + ::prometheus::ClientMetric &metric, + std::chrono::nanoseconds time, + const opentelemetry::sdk::metrics::PointAttributes &labels, + const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope); /** * Convert attribute value to string diff --git a/exporters/prometheus/src/exporter_utils.cc b/exporters/prometheus/src/exporter_utils.cc index 966d665df6..ee99ace35c 100644 --- a/exporters/prometheus/src/exporter_utils.cc +++ b/exporters/prometheus/src/exporter_utils.cc @@ -20,6 +20,10 @@ namespace exporter { namespace metrics { + +static constexpr const char *kScopeNameKey = "otel_scope_name"; +static constexpr const char *kScopeVersionKey = "otel_scope_version"; + /** * Helper function to convert OpenTelemetry metrics data collection * to Prometheus metrics data collection @@ -72,7 +76,7 @@ std::vector PrometheusExporterUtils::TranslateT sum = nostd::get(histogram_point_data.sum_); } SetData(std::vector{sum, (double)histogram_point_data.count_}, boundaries, counts, - point_data_attr.attributes, time, &metric_family); + point_data_attr.attributes, instrumentation_info.scope_, time, &metric_family); } else if (type == prometheus_client::MetricType::Gauge) { @@ -82,14 +86,16 @@ std::vector PrometheusExporterUtils::TranslateT 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); + SetData(values, point_data_attr.attributes, instrumentation_info.scope_, type, time, + &metric_family); } else 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); + SetData(values, point_data_attr.attributes, instrumentation_info.scope_, type, time, + &metric_family); } else { @@ -105,7 +111,8 @@ std::vector PrometheusExporterUtils::TranslateT 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); + SetData(values, point_data_attr.attributes, instrumentation_info.scope_, type, time, + &metric_family); } else { @@ -225,15 +232,17 @@ prometheus_client::MetricType PrometheusExporterUtils::TranslateType( * sum => Prometheus Counter */ template -void PrometheusExporterUtils::SetData(std::vector values, - const metric_sdk::PointAttributes &labels, - prometheus_client::MetricType type, - std::chrono::nanoseconds time, - prometheus_client::MetricFamily *metric_family) +void PrometheusExporterUtils::SetData( + std::vector values, + const metric_sdk::PointAttributes &labels, + const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope, + prometheus_client::MetricType type, + std::chrono::nanoseconds time, + prometheus_client::MetricFamily *metric_family) { metric_family->metric.emplace_back(); prometheus_client::ClientMetric &metric = metric_family->metric.back(); - SetMetricBasic(metric, time, labels); + SetMetricBasic(metric, time, labels, scope); SetValue(values, type, &metric); } @@ -242,40 +251,61 @@ void PrometheusExporterUtils::SetData(std::vector values, * Histogram => Prometheus Histogram */ template -void PrometheusExporterUtils::SetData(std::vector values, - const std::vector &boundaries, - const std::vector &counts, - const metric_sdk::PointAttributes &labels, - std::chrono::nanoseconds time, - prometheus_client::MetricFamily *metric_family) +void PrometheusExporterUtils::SetData( + std::vector values, + const std::vector &boundaries, + const std::vector &counts, + const metric_sdk::PointAttributes &labels, + const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope, + std::chrono::nanoseconds time, + prometheus_client::MetricFamily *metric_family) { metric_family->metric.emplace_back(); prometheus_client::ClientMetric &metric = metric_family->metric.back(); - SetMetricBasic(metric, time, labels); + SetMetricBasic(metric, time, labels, scope); SetValue(values, boundaries, counts, &metric); } /** * Set time and labels to metric data */ -void PrometheusExporterUtils::SetMetricBasic(prometheus_client::ClientMetric &metric, - std::chrono::nanoseconds time, - const metric_sdk::PointAttributes &labels) +void PrometheusExporterUtils::SetMetricBasic( + prometheus_client::ClientMetric &metric, + std::chrono::nanoseconds time, + const metric_sdk::PointAttributes &labels, + const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope) { metric.timestamp_ms = time.count() / 1000000; - // auto label_pairs = ParseLabel(labels); + metric.label.reserve(labels.size() + 2); if (!labels.empty()) { - metric.label.resize(labels.size()); - size_t i = 0; for (auto const &label : labels) { - auto sanitized = SanitizeNames(label.first); - metric.label[i].name = sanitized; - metric.label[i++].value = AttributeValueToString(label.second); + prometheus_client::ClientMetric::Label sanitizedLabel; + sanitizedLabel.name = SanitizeNames(label.first); + sanitizedLabel.value = AttributeValueToString(label.second); + metric.label.push_back(std::move(sanitizedLabel)); } } + if (!scope) + { + return; + } + auto scope_name = scope->GetName(); + if (!scope_name.empty()) + { + metric.label.emplace_back(); + metric.label.back().name = kScopeNameKey; + metric.label.back().value = std::move(scope_name); + } + auto scope_version = scope->GetVersion(); + if (!scope_version.empty()) + { + metric.label.emplace_back(); + metric.label.back().name = kScopeVersionKey; + metric.label.back().value = std::move(scope_version); + } } std::string PrometheusExporterUtils::AttributeValueToString( diff --git a/exporters/prometheus/test/exporter_utils_test.cc b/exporters/prometheus/test/exporter_utils_test.cc index 2222787787..6671f89c3e 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", "description", prometheus_client::MetricType::Counter, 3, vals); } @@ -128,7 +128,7 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerLastValue) auto metric1 = translated[0]; std::vector vals = {10}; - assert_basic(metric1, "library_name", "description", prometheus_client::MetricType::Gauge, 1, + assert_basic(metric1, "library_name", "description", prometheus_client::MetricType::Gauge, 3, vals); } @@ -142,7 +142,7 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusHistogramNormal) auto metric = translated[0]; std::vector vals = {3, 900.5, 4}; - assert_basic(metric, "library_name", "description", prometheus_client::MetricType::Histogram, 1, + assert_basic(metric, "library_name", "description", prometheus_client::MetricType::Histogram, 3, vals); assert_histogram(metric, std::list{10.1, 20.2, 30.2}, {200, 300, 400, 500}); } diff --git a/sdk/include/opentelemetry/sdk/metrics/export/metric_producer.h b/sdk/include/opentelemetry/sdk/metrics/export/metric_producer.h index 2a0c87fcaa..17d8bc0561 100644 --- a/sdk/include/opentelemetry/sdk/metrics/export/metric_producer.h +++ b/sdk/include/opentelemetry/sdk/metrics/export/metric_producer.h @@ -31,7 +31,7 @@ namespace metrics */ struct ScopeMetrics { - const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope_; + const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope_ = nullptr; std::vector metric_data_; };