Program Listing for File attributes_processor.h
↰ Return to documentation for file (/home/docs/checkouts/readthedocs.org/user_builds/opentelemetry-cpp/checkouts/latest/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h
)
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <string>
#include <unordered_map>
#include "opentelemetry/common/key_value_iterable.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/sdk/common/attribute_utils.h"
#include "opentelemetry/sdk/metrics/instruments.h"
#include "opentelemetry/version.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
using MetricAttributes = opentelemetry::sdk::common::OrderedAttributeMap;
class AttributesProcessor
{
public:
// Process the metric instrument attributes.
// @returns integer with individual bits set if they are to be filtered.
virtual MetricAttributes process(
const opentelemetry::common::KeyValueIterable &attributes) const noexcept = 0;
virtual bool isPresent(nostd::string_view key) const noexcept = 0;
virtual ~AttributesProcessor() = default;
};
class DefaultAttributesProcessor : public AttributesProcessor
{
public:
MetricAttributes process(
const opentelemetry::common::KeyValueIterable &attributes) const noexcept override
{
MetricAttributes result(attributes);
return result;
}
bool isPresent(nostd::string_view /*key*/) const noexcept override { return true; }
};
class FilteringAttributesProcessor : public AttributesProcessor
{
public:
FilteringAttributesProcessor(
const std::unordered_map<std::string, bool> allowed_attribute_keys = {})
: allowed_attribute_keys_(std::move(allowed_attribute_keys))
{}
MetricAttributes process(
const opentelemetry::common::KeyValueIterable &attributes) const noexcept override
{
MetricAttributes result;
attributes.ForEachKeyValue(
[&](nostd::string_view key, opentelemetry::common::AttributeValue value) noexcept {
if (allowed_attribute_keys_.find(key.data()) != allowed_attribute_keys_.end())
{
result.SetAttribute(key, value);
return true;
}
return true;
});
return result;
}
bool isPresent(nostd::string_view key) const noexcept override
{
return (allowed_attribute_keys_.find(key.data()) != allowed_attribute_keys_.end());
}
private:
std::unordered_map<std::string, bool> allowed_attribute_keys_;
};
} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE