Skip to content

Commit

Permalink
Fix metrics unit test memory leack (#1533)
Browse files Browse the repository at this point in the history
  • Loading branch information
esigo authored Jul 31, 2022
1 parent 9e6fd1e commit d452e8e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 3 additions & 1 deletion sdk/test/metrics/async_metric_storage_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ TEST_P(WritableMetricStorageTestFixture, TestAggregation)
collectors.push_back(collector);
size_t count_attributes = 0;

std::unique_ptr<DefaultAttributesProcessor> default_attributes_rocessor{
new DefaultAttributesProcessor{}};
opentelemetry::sdk::metrics::AsyncMetricStorage<long> 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) {
Expand Down
4 changes: 2 additions & 2 deletions sdk/test/metrics/metric_reader_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ TEST(MetricReaderTest, BasicTests)

std::unique_ptr<MetricReader> metric_reader2(new MockMetricReader(aggr_temporality));
std::shared_ptr<MeterContext> meter_context2(new MeterContext());
MetricProducer *metric_producer =
new MetricCollector(std::move(meter_context2), std::move(metric_reader2));
std::shared_ptr<MetricProducer> metric_producer{
new MetricCollector(std::move(meter_context2), std::move(metric_reader2))};
EXPECT_NO_THROW(metric_producer->Collect([](ResourceMetrics &metric_data) { return true; }));
}
#endif
11 changes: 8 additions & 3 deletions sdk/test/metrics/sync_metric_storage_test.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <memory>
#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 <gtest/gtest.h>
Expand Down Expand Up @@ -40,8 +41,10 @@ TEST_P(WritableMetricStorageTestFixture, LongSumAggregation)
std::map<std::string, std::string> attributes_get = {{"RequestType", "GET"}};
std::map<std::string, std::string> attributes_put = {{"RequestType", "PUT"}};

std::unique_ptr<DefaultAttributesProcessor> 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<std::map<std::string, std::string>>(attributes_get),
Expand Down Expand Up @@ -146,8 +149,10 @@ TEST_P(WritableMetricStorageTestFixture, DoubleSumAggregation)
std::map<std::string, std::string> attributes_get = {{"RequestType", "GET"}};
std::map<std::string, std::string> attributes_put = {{"RequestType", "PUT"}};

std::unique_ptr<DefaultAttributesProcessor> 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,
Expand Down

0 comments on commit d452e8e

Please sign in to comment.