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 949c2915d9..9e91744624 100644 --- a/exporters/prometheus/src/collector.cc +++ b/exporters/prometheus/src/collector.cc @@ -44,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_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 {