diff --git a/exporters/prometheus/src/exporter_utils.cc b/exporters/prometheus/src/exporter_utils.cc index f20ba5ef9e..8a577f689b 100644 --- a/exporters/prometheus/src/exporter_utils.cc +++ b/exporters/prometheus/src/exporter_utils.cc @@ -276,7 +276,7 @@ void PrometheusExporterUtils::SetMetricBasic( 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); @@ -284,17 +284,16 @@ void PrometheusExporterUtils::SetMetricBasic( metric.label[i++].value = AttributeValueToString(label.second); } } + 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; } 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; }