From 6ec0788454a78f3fabc79a22ca9cee627131a632 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sat, 30 Jul 2022 21:42:37 +0000 Subject: [PATCH] Fix metrics unit test memory leack --- sdk/test/metrics/async_metric_storage_test.cc | 4 +++- sdk/test/metrics/metric_reader_test.cc | 4 ++-- sdk/test/metrics/sync_metric_storage_test.cc | 11 ++++++++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/sdk/test/metrics/async_metric_storage_test.cc b/sdk/test/metrics/async_metric_storage_test.cc index 49728e1d8b..8eb55ecd7b 100644 --- a/sdk/test/metrics/async_metric_storage_test.cc +++ b/sdk/test/metrics/async_metric_storage_test.cc @@ -94,9 +94,11 @@ TEST_P(WritableMetricStorageTestFixture, TestAggregation) collectors.push_back(collector); size_t count_attributes = 0; + std::unique_ptr default_attributes_rocessor{ + new DefaultAttributesProcessor{}}; opentelemetry::sdk::metrics::AsyncMetricStorage storage(instr_desc, AggregationType::kSum, MeasurementFetcher::Fetcher, - new DefaultAttributesProcessor()); + default_attributes_rocessor.get()); storage.Collect(collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData data) { diff --git a/sdk/test/metrics/metric_reader_test.cc b/sdk/test/metrics/metric_reader_test.cc index 413e917d7d..3023122bb0 100644 --- a/sdk/test/metrics/metric_reader_test.cc +++ b/sdk/test/metrics/metric_reader_test.cc @@ -32,8 +32,8 @@ TEST(MetricReaderTest, BasicTests) std::unique_ptr metric_reader2(new MockMetricReader(aggr_temporality)); std::shared_ptr meter_context2(new MeterContext()); - MetricProducer *metric_producer = - new MetricCollector(std::move(meter_context2), std::move(metric_reader2)); + std::shared_ptr metric_producer{ + new MetricCollector(std::move(meter_context2), std::move(metric_reader2))}; EXPECT_NO_THROW(metric_producer->Collect([](ResourceMetrics &metric_data) { return true; })); } #endif diff --git a/sdk/test/metrics/sync_metric_storage_test.cc b/sdk/test/metrics/sync_metric_storage_test.cc index 7dfc4f9475..8d4ee99262 100644 --- a/sdk/test/metrics/sync_metric_storage_test.cc +++ b/sdk/test/metrics/sync_metric_storage_test.cc @@ -1,11 +1,12 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include #ifndef ENABLE_METRICS_PREVIEW -# include "opentelemetry/sdk/metrics/state/sync_metric_storage.h" # include "opentelemetry/common/key_value_iterable_view.h" # include "opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h" # include "opentelemetry/sdk/metrics/instruments.h" +# include "opentelemetry/sdk/metrics/state/sync_metric_storage.h" # include "opentelemetry/sdk/metrics/view/attributes_processor.h" # include @@ -40,8 +41,10 @@ TEST_P(WritableMetricStorageTestFixture, LongSumAggregation) std::map attributes_get = {{"RequestType", "GET"}}; std::map attributes_put = {{"RequestType", "PUT"}}; + std::unique_ptr default_attributes_processor{ + new DefaultAttributesProcessor{}}; opentelemetry::sdk::metrics::SyncMetricStorage storage( - instr_desc, AggregationType::kSum, new DefaultAttributesProcessor(), + instr_desc, AggregationType::kSum, default_attributes_processor.get(), NoExemplarReservoir::GetNoExemplarReservoir()); storage.RecordLong(10l, KeyValueIterableView>(attributes_get), @@ -146,8 +149,10 @@ TEST_P(WritableMetricStorageTestFixture, DoubleSumAggregation) std::map attributes_get = {{"RequestType", "GET"}}; std::map attributes_put = {{"RequestType", "PUT"}}; + std::unique_ptr default_attributes_processor{ + new DefaultAttributesProcessor{}}; opentelemetry::sdk::metrics::SyncMetricStorage storage( - instr_desc, AggregationType::kSum, new DefaultAttributesProcessor(), + instr_desc, AggregationType::kSum, default_attributes_processor.get(), NoExemplarReservoir::GetNoExemplarReservoir()); storage.RecordDouble(10.0,