Skip to content

Commit 4e4d8de

Browse files
authored
[Metrics SDK] Use nostd::function_ref in AttributesHashMap (open-telemetry#3393)
1 parent 9451f0e commit 4e4d8de

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Increment the:
1515

1616
## [Unreleased]
1717

18+
* [Metrics SDK] Use nostd::function_ref in AttributesHashMap
19+
[#3393](https://github.com/open-telemetry/opentelemetry-cpp/pull/3393)
20+
1821
* [SDK] Base2 exponential histogram aggregation
1922
[#3175](https://github.com/open-telemetry/opentelemetry-cpp/pull/3346)
2023

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ class AttributesHashMapWithCustomHash
7474
* If not present, it uses the provided callback to generate
7575
* value and store in the hash
7676
*/
77-
Aggregation *GetOrSetDefault(const opentelemetry::common::KeyValueIterable &attributes,
78-
const AttributesProcessor *attributes_processor,
79-
std::function<std::unique_ptr<Aggregation>()> aggregation_callback)
77+
Aggregation *GetOrSetDefault(
78+
const opentelemetry::common::KeyValueIterable &attributes,
79+
const AttributesProcessor *attributes_processor,
80+
nostd::function_ref<std::unique_ptr<Aggregation>()> aggregation_callback)
8081
{
8182
// TODO: avoid constructing MetricAttributes from KeyValueIterable for
8283
// hash_map_.find which is a heavy operation
@@ -97,8 +98,9 @@ class AttributesHashMapWithCustomHash
9798
return result.first->second.get();
9899
}
99100

100-
Aggregation *GetOrSetDefault(const MetricAttributes &attributes,
101-
std::function<std::unique_ptr<Aggregation>()> aggregation_callback)
101+
Aggregation *GetOrSetDefault(
102+
const MetricAttributes &attributes,
103+
nostd::function_ref<std::unique_ptr<Aggregation>()> aggregation_callback)
102104
{
103105
auto it = hash_map_.find(attributes);
104106
if (it != hash_map_.end())
@@ -115,8 +117,9 @@ class AttributesHashMapWithCustomHash
115117
return hash_map_[attributes].get();
116118
}
117119

118-
Aggregation *GetOrSetDefault(MetricAttributes &&attributes,
119-
std::function<std::unique_ptr<Aggregation>()> aggregation_callback)
120+
Aggregation *GetOrSetDefault(
121+
MetricAttributes &&attributes,
122+
nostd::function_ref<std::unique_ptr<Aggregation>()> aggregation_callback)
120123
{
121124
auto it = hash_map_.find(attributes);
122125
if (it != hash_map_.end())
@@ -207,7 +210,7 @@ class AttributesHashMapWithCustomHash
207210
size_t attributes_limit_;
208211

209212
Aggregation *GetOrSetOveflowAttributes(
210-
std::function<std::unique_ptr<Aggregation>()> aggregation_callback)
213+
nostd::function_ref<std::unique_ptr<Aggregation>()> aggregation_callback)
211214
{
212215
auto agg = aggregation_callback();
213216
return GetOrSetOveflowAttributes(std::move(agg));

sdk/test/metrics/attributes_hashmap_benchmark.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <thread>
1212
#include <vector>
1313

14+
#include "opentelemetry/nostd/function_ref.h"
1415
#include "opentelemetry/sdk/metrics/aggregation/aggregation.h"
1516
#include "opentelemetry/sdk/metrics/aggregation/drop_aggregation.h"
1617
#include "opentelemetry/sdk/metrics/state/attributes_hashmap.h"

0 commit comments

Comments
 (0)