From a36cee15e24fad4b518ea6fb60cf2e293e32c0e6 Mon Sep 17 00:00:00 2001 From: David Ashpole Date: Thu, 21 Sep 2023 15:54:01 +0000 Subject: [PATCH] address comments --- exporters/prometheus/src/exporter_utils.cc | 32 ++++++++++++------- .../sdk/metrics/export/metric_producer.h | 15 ++++++++- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/exporters/prometheus/src/exporter_utils.cc b/exporters/prometheus/src/exporter_utils.cc index f20ba5ef9e..a033b17254 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 @@ -273,30 +277,36 @@ void PrometheusExporterUtils::SetMetricBasic( { metric.timestamp_ms = time.count() / 1000000; - size_t i = 0; if (!labels.empty()) { - metric.label.resize(labels.size()); + metric.label.reserve(labels.size() + 2); 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.resize(i + 1); - metric.label[i].name = "otel_scope_name"; - metric.label[i++].value = scope_name; + prometheus_client::ClientMetric::Label label; + label.name = std::move(kScopeNameKey); + label.value = std::move(scope_name); + metric.label.push_back(std::move(label)); } auto scope_version = scope->GetVersion(); if (!scope_version.empty()) { - metric.label.resize(i + 1); - metric.label[i].name = "otel_scope_version"; - metric.label[i++].value = scope_version; + prometheus_client::ClientMetric::Label label; + label.name = std::move(kScopeNameKey); + label.value = std::move(scope_version); + metric.label.push_back(std::move(label)); } } diff --git a/sdk/include/opentelemetry/sdk/metrics/export/metric_producer.h b/sdk/include/opentelemetry/sdk/metrics/export/metric_producer.h index 2a0c87fcaa..b05298de34 100644 --- a/sdk/include/opentelemetry/sdk/metrics/export/metric_producer.h +++ b/sdk/include/opentelemetry/sdk/metrics/export/metric_producer.h @@ -31,8 +31,21 @@ namespace metrics */ struct ScopeMetrics { - const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope_; + const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope_ = nullptr; std::vector metric_data_; + + template + inline ScopeMetrics(ScopePtr &&scope, MetricDataType &&metric) + : scope_{std::move(scope)}, metric_data_{std::move(metric)} + {} + + inline ScopeMetrics() {} + inline ScopeMetrics(const ScopeMetrics &) = default; + inline ScopeMetrics(ScopeMetrics &&) = default; + + inline ScopeMetrics &operator=(const ScopeMetrics &) = default; + + inline ScopeMetrics &operator=(ScopeMetrics &&) = default; }; struct ResourceMetrics