Skip to content

Commit 37a57da

Browse files
authored
[SDK] Reduce lock contention in SyncMetricStorage for concurrent metric recording (#3959)
1 parent 20430d0 commit 37a57da

File tree

3 files changed

+7
-40
lines changed

3 files changed

+7
-40
lines changed

sdk/include/opentelemetry/sdk/metrics/state/attributes_hashmap.h

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010
#include <unordered_map>
1111
#include <utility>
1212

13-
#include "opentelemetry/common/key_value_iterable.h"
1413
#include "opentelemetry/nostd/function_ref.h"
1514
#include "opentelemetry/sdk/common/attribute_utils.h"
1615
#include "opentelemetry/sdk/common/attributemap_hash.h"
1716
#include "opentelemetry/sdk/metrics/aggregation/aggregation.h"
1817
#include "opentelemetry/sdk/metrics/state/filtered_ordered_attribute_map.h"
19-
#include "opentelemetry/sdk/metrics/view/attributes_processor.h"
2018
#include "opentelemetry/version.h"
2119

2220
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -80,30 +78,6 @@ class AttributesHashMapWithCustomHash
8078
* If not present, it uses the provided callback to generate
8179
* value and store in the hash
8280
*/
83-
Aggregation *GetOrSetDefault(
84-
const opentelemetry::common::KeyValueIterable &attributes,
85-
const AttributesProcessor *attributes_processor,
86-
nostd::function_ref<std::unique_ptr<Aggregation>()> aggregation_callback)
87-
{
88-
// TODO: avoid constructing MetricAttributes from KeyValueIterable for
89-
// hash_map_.find which is a heavy operation
90-
MetricAttributes attr{attributes, attributes_processor};
91-
92-
auto it = hash_map_.find(attr);
93-
if (it != hash_map_.end())
94-
{
95-
return it->second.get();
96-
}
97-
98-
if (IsOverflowAttributes(attr))
99-
{
100-
return GetOrSetOveflowAttributes(aggregation_callback);
101-
}
102-
103-
auto result = hash_map_.emplace(std::move(attr), aggregation_callback());
104-
return result.first->second.get();
105-
}
106-
10781
Aggregation *GetOrSetDefault(
10882
const MetricAttributes &attributes,
10983
nostd::function_ref<std::unique_ptr<Aggregation>()> aggregation_callback)
@@ -144,13 +118,6 @@ class AttributesHashMapWithCustomHash
144118
/**
145119
* Set the value for given key, overwriting the value if already present
146120
*/
147-
void Set(const opentelemetry::common::KeyValueIterable &attributes,
148-
const AttributesProcessor *attributes_processor,
149-
std::unique_ptr<Aggregation> aggr)
150-
{
151-
Set(MetricAttributes{attributes, attributes_processor}, std::move(aggr));
152-
}
153-
154121
void Set(const MetricAttributes &attributes, std::unique_ptr<Aggregation> aggr)
155122
{
156123
auto it = hash_map_.find(attributes);

sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,10 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage
119119
}
120120
#endif
121121

122+
MetricAttributes attr{attributes, attributes_processor_.get()};
122123
std::lock_guard<opentelemetry::common::SpinLockMutex> guard(attribute_hashmap_lock_);
123-
attributes_hashmap_
124-
->GetOrSetDefault(attributes, attributes_processor_.get(), create_default_aggregation_)
124+
// cppcheck-suppress accessMoved
125+
attributes_hashmap_->GetOrSetDefault(std::move(attr), create_default_aggregation_)
125126
->Aggregate(value);
126127
}
127128

@@ -160,9 +161,10 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage
160161
std::chrono::system_clock::now());
161162
}
162163
#endif
164+
MetricAttributes attr{attributes, attributes_processor_.get()};
163165
std::lock_guard<opentelemetry::common::SpinLockMutex> guard(attribute_hashmap_lock_);
164-
attributes_hashmap_
165-
->GetOrSetDefault(attributes, attributes_processor_.get(), create_default_aggregation_)
166+
// cppcheck-suppress accessMoved
167+
attributes_hashmap_->GetOrSetDefault(std::move(attr), create_default_aggregation_)
166168
->Aggregate(value);
167169
}
168170

sdk/test/metrics/async_metric_storage_test.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@
2525
#include "opentelemetry/sdk/metrics/state/attributes_hashmap.h"
2626
#include "opentelemetry/sdk/metrics/state/filtered_ordered_attribute_map.h"
2727
#include "opentelemetry/sdk/metrics/state/metric_collector.h"
28+
#include "opentelemetry/sdk/metrics/view/attributes_processor.h"
2829

2930
#ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
30-
# include "opentelemetry/sdk/metrics/data/exemplar_data.h"
3131
# include "opentelemetry/sdk/metrics/exemplar/filter_type.h"
3232
# include "opentelemetry/sdk/metrics/exemplar/reservoir.h"
33-
#else
34-
# include "opentelemetry/sdk/metrics/view/attributes_processor.h"
3533
#endif
3634

3735
using namespace opentelemetry::sdk::metrics;

0 commit comments

Comments
 (0)