From e8afbb8eac5bd2abb96643c36bef5818a416dbea Mon Sep 17 00:00:00 2001 From: Tim Wojtulewicz Date: Sat, 16 Dec 2023 09:37:13 -0700 Subject: [PATCH] [EXPORTER] Add option to disable Prometheus otel_scope_name and otel_scope_version attributes (#2451) --- .../exporters/prometheus/collector.h | 5 +++- .../exporters/prometheus/exporter_options.h | 3 ++ .../exporters/prometheus/exporter_utils.h | 5 +++- exporters/prometheus/src/collector.cc | 11 ++++--- exporters/prometheus/src/exporter.cc | 2 +- exporters/prometheus/src/exporter_options.cc | 30 ++++++++++++++++++- exporters/prometheus/src/exporter_utils.cc | 23 +++++++------- exporters/prometheus/test/collector_test.cc | 2 +- 8 files changed, 62 insertions(+), 19 deletions(-) diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/collector.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/collector.h index 5dfa983088..ee257a5547 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/collector.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/collector.h @@ -31,7 +31,9 @@ class PrometheusCollector : public prometheus_client::Collectable * This constructor initializes the collection for metrics to export * in this class with default capacity */ - explicit PrometheusCollector(sdk::metrics::MetricReader *reader, bool populate_target_info); + explicit PrometheusCollector(sdk::metrics::MetricReader *reader, + bool populate_target_info, + bool populate_otel_scope); /** * Collects all metrics data from metricsToCollect collection. @@ -43,6 +45,7 @@ class PrometheusCollector : public prometheus_client::Collectable private: sdk::metrics::MetricReader *reader_; bool populate_target_info_; + bool populate_otel_scope_; /* * Lock when operating the metricsToCollect collection diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_options.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_options.h index 3f36d780ee..61e7fb3de0 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_options.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_options.h @@ -25,6 +25,9 @@ struct PrometheusExporterOptions // Populating target_info bool populate_target_info = true; + + // Populating otel_scope_name/otel_scope_labels attributes + bool populate_otel_scope = true; }; } // namespace metrics diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h index fa3eedd2c4..bab75b3e10 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h @@ -28,11 +28,14 @@ class PrometheusExporterUtils * * @param records a collection of metrics in OpenTelemetry * @param populate_target_info whether to populate target_info + * @param populate_otel_scope whether to populate otel_scope_name and otel_scope_version + * attributes * @return a collection of translated metrics that is acceptable by Prometheus */ static std::vector<::prometheus::MetricFamily> TranslateToPrometheus( const sdk::metrics::ResourceMetrics &data, - bool populate_target_info = true); + bool populate_target_info = true, + bool populate_otel_scope = true); private: /** diff --git a/exporters/prometheus/src/collector.cc b/exporters/prometheus/src/collector.cc index 33b9a9b8c8..9e91744624 100644 --- a/exporters/prometheus/src/collector.cc +++ b/exporters/prometheus/src/collector.cc @@ -18,8 +18,11 @@ namespace metrics * in this class with default capacity */ PrometheusCollector::PrometheusCollector(sdk::metrics::MetricReader *reader, - bool populate_target_info) - : reader_(reader), populate_target_info_(populate_target_info) + bool populate_target_info, + bool populate_otel_scope) + : reader_(reader), + populate_target_info_(populate_target_info), + populate_otel_scope_(populate_otel_scope) {} /** @@ -41,8 +44,8 @@ std::vector PrometheusCollector::Collect() cons std::vector result; reader_->Collect([&result, this](sdk::metrics::ResourceMetrics &metric_data) { - auto prometheus_metric_data = - PrometheusExporterUtils::TranslateToPrometheus(metric_data, this->populate_target_info_); + auto prometheus_metric_data = PrometheusExporterUtils::TranslateToPrometheus( + metric_data, this->populate_target_info_, this->populate_otel_scope_); for (auto &data : prometheus_metric_data) result.emplace_back(data); return true; diff --git a/exporters/prometheus/src/exporter.cc b/exporters/prometheus/src/exporter.cc index 6022c2c33b..07f1494000 100644 --- a/exporters/prometheus/src/exporter.cc +++ b/exporters/prometheus/src/exporter.cc @@ -31,7 +31,7 @@ PrometheusExporter::PrometheusExporter(const PrometheusExporterOptions &options) return; } collector_ = std::shared_ptr( - new PrometheusCollector(this, options_.populate_target_info)); + new PrometheusCollector(this, options_.populate_target_info, options_.populate_otel_scope)); exposer_->RegisterCollectable(collector_); } diff --git a/exporters/prometheus/src/exporter_options.cc b/exporters/prometheus/src/exporter_options.cc index 507f119eb7..0a7814c166 100644 --- a/exporters/prometheus/src/exporter_options.cc +++ b/exporters/prometheus/src/exporter_options.cc @@ -25,7 +25,35 @@ inline const std::string GetPrometheusDefaultHttpEndpoint() return exists ? endpoint : kPrometheusEndpointDefault; } -PrometheusExporterOptions::PrometheusExporterOptions() : url(GetPrometheusDefaultHttpEndpoint()) {} +inline bool GetPrometheusPopulateOtelScope() +{ + constexpr char kPrometheusPopulateOtelScope[] = + "OTEL_CPP_PROMETHEUS_EXPORTER_POPULATE_OTEL_SCOPE"; + + bool setting; + auto exists = + opentelemetry::sdk::common::GetBoolEnvironmentVariable(kPrometheusPopulateOtelScope, setting); + + return exists ? setting : true; +} + +inline bool GetPrometheusPopulateTargetInfo() +{ + constexpr char kPrometheusPopulateTargetInfo[] = + "OTEL_CPP_PROMETHEUS_EXPORTER_POPULATE_TARGET_INFO"; + + bool setting; + auto exists = opentelemetry::sdk::common::GetBoolEnvironmentVariable( + kPrometheusPopulateTargetInfo, setting); + + return exists ? setting : true; +} + +PrometheusExporterOptions::PrometheusExporterOptions() + : url(GetPrometheusDefaultHttpEndpoint()), + populate_target_info(GetPrometheusPopulateTargetInfo()), + populate_otel_scope(GetPrometheusPopulateOtelScope()) +{} } // namespace metrics } // namespace exporter diff --git a/exporters/prometheus/src/exporter_utils.cc b/exporters/prometheus/src/exporter_utils.cc index 4b1ee054c4..511e481c44 100644 --- a/exporters/prometheus/src/exporter_utils.cc +++ b/exporters/prometheus/src/exporter_utils.cc @@ -105,7 +105,8 @@ std::string SanitizeLabel(std::string label_key) */ std::vector PrometheusExporterUtils::TranslateToPrometheus( const sdk::metrics::ResourceMetrics &data, - bool populate_target_info) + bool populate_target_info, + bool populate_otel_scope) { // initialize output vector @@ -126,7 +127,7 @@ std::vector PrometheusExporterUtils::TranslateT { SetTarget(data, data.scope_metric_data_.begin()->metric_data_.begin()->end_ts.time_since_epoch(), - (*data.scope_metric_data_.begin()).scope_, &output); + populate_otel_scope ? (*data.scope_metric_data_.begin()).scope_ : nullptr, &output); } for (const auto &instrumentation_info : data.scope_metric_data_) @@ -149,6 +150,9 @@ std::vector PrometheusExporterUtils::TranslateT metric_family.name = MapToPrometheusName(metric_data.instrument_descriptor.name_, metric_data.instrument_descriptor.unit_, type); metric_family.type = type; + const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope = + populate_otel_scope ? instrumentation_info.scope_ : nullptr; + for (const auto &point_data_attr : metric_data.point_data_attr_) { if (type == prometheus_client::MetricType::Histogram) // Histogram @@ -167,8 +171,7 @@ std::vector PrometheusExporterUtils::TranslateT sum = static_cast(nostd::get(histogram_point_data.sum_)); } SetData(std::vector{sum, (double)histogram_point_data.count_}, boundaries, counts, - point_data_attr.attributes, instrumentation_info.scope_, time, &metric_family, - data.resource_); + point_data_attr.attributes, scope, time, &metric_family, data.resource_); } else if (type == prometheus_client::MetricType::Gauge) { @@ -178,16 +181,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, instrumentation_info.scope_, type, time, - &metric_family, data.resource_); + SetData(values, point_data_attr.attributes, scope, type, time, &metric_family, + data.resource_); } 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, instrumentation_info.scope_, type, time, - &metric_family, data.resource_); + SetData(values, point_data_attr.attributes, scope, type, time, &metric_family, + data.resource_); } else { @@ -203,8 +206,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, instrumentation_info.scope_, type, time, - &metric_family, data.resource_); + SetData(values, point_data_attr.attributes, scope, type, time, &metric_family, + data.resource_); } else { diff --git a/exporters/prometheus/test/collector_test.cc b/exporters/prometheus/test/collector_test.cc index 578e01d8f8..4708b80aaa 100644 --- a/exporters/prometheus/test/collector_test.cc +++ b/exporters/prometheus/test/collector_test.cc @@ -73,7 +73,7 @@ TEST(PrometheusCollector, BasicTests) MockMetricReader *reader = new MockMetricReader(); MockMetricProducer *producer = new MockMetricProducer(); reader->SetMetricProducer(producer); - PrometheusCollector collector(reader, true); + PrometheusCollector collector(reader, true, true); auto data = collector.Collect(); // Collection size should be the same as the size