From 401fcb8b2fe78cc6f76eba1fa8a171caa1951359 Mon Sep 17 00:00:00 2001 From: Alexey Shakula Date: Sat, 3 Dec 2022 08:24:44 -0500 Subject: [PATCH 1/2] minor metrics handling optimizations [avoid few unnecessary copies & empty exports] --- sdk/src/metrics/state/metric_collector.cc | 12 ++++++++---- sdk/src/metrics/state/temporal_metric_storage.cc | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/sdk/src/metrics/state/metric_collector.cc b/sdk/src/metrics/state/metric_collector.cc index cc1883275b..86cda3eb5e 100644 --- a/sdk/src/metrics/state/metric_collector.cc +++ b/sdk/src/metrics/state/metric_collector.cc @@ -40,10 +40,14 @@ bool MetricCollector::Collect( ResourceMetrics resource_metrics; meter_context_->ForEachMeter([&](std::shared_ptr meter) noexcept { auto collection_ts = std::chrono::system_clock::now(); - ScopeMetrics scope_metrics; - scope_metrics.metric_data_ = meter->Collect(this, collection_ts); - scope_metrics.scope_ = meter->GetInstrumentationScope(); - resource_metrics.scope_metric_data_.push_back(scope_metrics); + auto metric_data = meter->Collect(this, collection_ts); + if (!metric_data.empty ()) + { + ScopeMetrics scope_metrics; + scope_metrics.metric_data_ = std::move (metric_data); + scope_metrics.scope_ = meter->GetInstrumentationScope(); + resource_metrics.scope_metric_data_.emplace_back(std::move (scope_metrics)); + } return true; }); resource_metrics.resource_ = &meter_context_->GetResource(); diff --git a/sdk/src/metrics/state/temporal_metric_storage.cc b/sdk/src/metrics/state/temporal_metric_storage.cc index 0875d86912..921d0c057c 100644 --- a/sdk/src/metrics/state/temporal_metric_storage.cc +++ b/sdk/src/metrics/state/temporal_metric_storage.cc @@ -126,7 +126,7 @@ bool TemporalMetricStorage::buildMetrics(CollectorHandle *collector, PointDataAttributes point_data_attr; point_data_attr.point_data = aggregation.ToPoint(); point_data_attr.attributes = attributes; - metric_data.point_data_attr_.push_back(point_data_attr); + metric_data.point_data_attr_.emplace_back(std::move (point_data_attr)); return true; }); return callback(metric_data); From 711b2ba42d672f0a0ab072ce4d7a9d46af6cb0b8 Mon Sep 17 00:00:00 2001 From: Alexey Shakula Date: Tue, 3 Jan 2023 08:38:40 -0500 Subject: [PATCH 2/2] cosmetic [code format] --- sdk/src/metrics/state/metric_collector.cc | 8 ++++---- sdk/src/metrics/state/temporal_metric_storage.cc | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/sdk/src/metrics/state/metric_collector.cc b/sdk/src/metrics/state/metric_collector.cc index 86cda3eb5e..f0535a905d 100644 --- a/sdk/src/metrics/state/metric_collector.cc +++ b/sdk/src/metrics/state/metric_collector.cc @@ -40,13 +40,13 @@ bool MetricCollector::Collect( ResourceMetrics resource_metrics; meter_context_->ForEachMeter([&](std::shared_ptr meter) noexcept { auto collection_ts = std::chrono::system_clock::now(); - auto metric_data = meter->Collect(this, collection_ts); - if (!metric_data.empty ()) + auto metric_data = meter->Collect(this, collection_ts); + if (!metric_data.empty()) { ScopeMetrics scope_metrics; - scope_metrics.metric_data_ = std::move (metric_data); + scope_metrics.metric_data_ = std::move(metric_data); scope_metrics.scope_ = meter->GetInstrumentationScope(); - resource_metrics.scope_metric_data_.emplace_back(std::move (scope_metrics)); + resource_metrics.scope_metric_data_.emplace_back(std::move(scope_metrics)); } return true; }); diff --git a/sdk/src/metrics/state/temporal_metric_storage.cc b/sdk/src/metrics/state/temporal_metric_storage.cc index 921d0c057c..f9c281b0c0 100644 --- a/sdk/src/metrics/state/temporal_metric_storage.cc +++ b/sdk/src/metrics/state/temporal_metric_storage.cc @@ -126,7 +126,7 @@ bool TemporalMetricStorage::buildMetrics(CollectorHandle *collector, PointDataAttributes point_data_attr; point_data_attr.point_data = aggregation.ToPoint(); point_data_attr.attributes = attributes; - metric_data.point_data_attr_.emplace_back(std::move (point_data_attr)); + metric_data.point_data_attr_.emplace_back(std::move(point_data_attr)); return true; }); return callback(metric_data);