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/src/collector.cc b/exporters/prometheus/src/collector.cc index 33b9a9b8c8..949c2915d9 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) {} /** 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..fe07ad1bc5 100644 --- a/exporters/prometheus/src/exporter_options.cc +++ b/exporters/prometheus/src/exporter_options.cc @@ -25,7 +25,20 @@ inline const std::string GetPrometheusDefaultHttpEndpoint() return exists ? endpoint : kPrometheusEndpointDefault; } -PrometheusExporterOptions::PrometheusExporterOptions() : url(GetPrometheusDefaultHttpEndpoint()) {} +inline bool GetPrometheusPopulateOtelScope() +{ + constexpr char kPrometheusPopulateOtelScope[] = "PROMETHEUS_EXPORTER_POPULATE_OTEL_SCOPE"; + + bool setting; + auto exists = + opentelemetry::sdk::common::GetBoolEnvironmentVariable(kPrometheusPopulateOtelScope, setting); + + return exists ? setting : true; +} + +PrometheusExporterOptions::PrometheusExporterOptions() + : url(GetPrometheusDefaultHttpEndpoint()), populate_otel_scope(GetPrometheusPopulateOtelScope()) +{} } // namespace metrics } // namespace exporter 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