Program Listing for File attributes_hashmap.h
↰ Return to documentation for file (/home/docs/checkouts/readthedocs.org/user_builds/opentelemetry-cpp/checkouts/v1.8.1/sdk/include/opentelemetry/sdk/metrics/state/attributes_hashmap.h
)
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "opentelemetry/nostd/function_ref.h"
#include "opentelemetry/sdk/common/attribute_utils.h"
#include "opentelemetry/sdk/common/attributemap_hash.h"
#include "opentelemetry/sdk/metrics/aggregation/aggregation.h"
#include "opentelemetry/sdk/metrics/instruments.h"
#include "opentelemetry/version.h"
#include <functional>
#include <memory>
#include <unordered_map>
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
using opentelemetry::sdk::common::OrderedAttributeMap;
class AttributeHashGenerator
{
public:
size_t operator()(const MetricAttributes &attributes) const
{
return opentelemetry::sdk::common::GetHashForAttributeMap(attributes);
}
};
class AttributesHashMap
{
public:
Aggregation *Get(const MetricAttributes &attributes) const
{
auto it = hash_map_.find(attributes);
if (it != hash_map_.end())
{
return it->second.get();
}
return nullptr;
}
bool Has(const MetricAttributes &attributes) const
{
return (hash_map_.find(attributes) == hash_map_.end()) ? false : true;
}
Aggregation *GetOrSetDefault(const MetricAttributes &attributes,
std::function<std::unique_ptr<Aggregation>()> aggregation_callback)
{
auto it = hash_map_.find(attributes);
if (it != hash_map_.end())
{
return it->second.get();
}
hash_map_[attributes] = aggregation_callback();
return hash_map_[attributes].get();
}
void Set(const MetricAttributes &attributes, std::unique_ptr<Aggregation> value)
{
hash_map_[attributes] = std::move(value);
}
bool GetAllEnteries(
nostd::function_ref<bool(const MetricAttributes &, Aggregation &)> callback) const
{
for (auto &kv : hash_map_)
{
if (!callback(kv.first, *(kv.second.get())))
{
return false; // callback is not prepared to consume data
}
}
return true;
}
size_t Size() { return hash_map_.size(); }
private:
std::unordered_map<MetricAttributes, std::unique_ptr<Aggregation>, AttributeHashGenerator>
hash_map_;
};
} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE