|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#include <cstdint> |
| 7 | +#include <initializer_list> |
| 8 | +#include <map> |
| 9 | +#include <string> |
| 10 | +#include <unordered_map> |
| 11 | +#include <utility> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +#include "opentelemetry/common/attribute_value.h" |
| 15 | +#include "opentelemetry/common/key_value_iterable.h" |
| 16 | +#include "opentelemetry/nostd/string_view.h" |
| 17 | +#include "opentelemetry/nostd/variant.h" |
| 18 | +#include "opentelemetry/sdk/common/attribute_utils.h" |
| 19 | +#include "opentelemetry/version.h" |
| 20 | + |
| 21 | +OPENTELEMETRY_BEGIN_NAMESPACE |
| 22 | +namespace sdk |
| 23 | +{ |
| 24 | +namespace common |
| 25 | +{ |
| 26 | + |
| 27 | +OPENTELEMETRY_EXPORT bool AttributeIsValidString(nostd::string_view value) noexcept; |
| 28 | + |
| 29 | +/** |
| 30 | + * Validate if an attribute value is valid. |
| 31 | + */ |
| 32 | +struct AttributeValidator |
| 33 | +{ |
| 34 | + bool operator()(bool /*v*/) noexcept { return true; } |
| 35 | + bool operator()(int32_t /*v*/) noexcept { return true; } |
| 36 | + bool operator()(uint32_t /*v*/) noexcept { return true; } |
| 37 | + bool operator()(int64_t /*v*/) noexcept { return true; } |
| 38 | + bool operator()(uint64_t /*v*/) noexcept { return true; } |
| 39 | + bool operator()(double /*v*/) noexcept { return true; } |
| 40 | + bool operator()(nostd::string_view v) noexcept { return AttributeIsValidString(v); } |
| 41 | + bool operator()(std::string v) noexcept { return AttributeIsValidString(v); } |
| 42 | + bool operator()(const char *v) noexcept { return AttributeIsValidString(v); } |
| 43 | + bool operator()(nostd::span<const uint8_t> /*v*/) noexcept { return true; } |
| 44 | + bool operator()(nostd::span<const bool> /*v*/) noexcept { return true; } |
| 45 | + bool operator()(nostd::span<const int32_t> /*v*/) noexcept { return true; } |
| 46 | + bool operator()(nostd::span<const uint32_t> /*v*/) noexcept { return true; } |
| 47 | + bool operator()(nostd::span<const int64_t> /*v*/) noexcept { return true; } |
| 48 | + bool operator()(nostd::span<const uint64_t> /*v*/) noexcept { return true; } |
| 49 | + bool operator()(nostd::span<const double> /*v*/) noexcept { return true; } |
| 50 | + bool operator()(nostd::span<const nostd::string_view> v) noexcept |
| 51 | + { |
| 52 | + for (const auto &s : v) |
| 53 | + { |
| 54 | + if (!AttributeIsValidString(s)) |
| 55 | + { |
| 56 | + return false; |
| 57 | + } |
| 58 | + } |
| 59 | + return true; |
| 60 | + } |
| 61 | + bool operator()(const std::vector<bool> & /*v*/) noexcept { return true; } |
| 62 | + bool operator()(const std::vector<int32_t> & /*v*/) noexcept { return true; } |
| 63 | + bool operator()(const std::vector<uint32_t> & /*v*/) noexcept { return true; } |
| 64 | + bool operator()(const std::vector<int64_t> & /*v*/) noexcept { return true; } |
| 65 | + bool operator()(const std::vector<double> & /*v*/) noexcept { return true; } |
| 66 | + bool operator()(const std::vector<std::string> &v) |
| 67 | + { |
| 68 | + for (const auto &s : v) |
| 69 | + { |
| 70 | + if (!AttributeIsValidString(s)) |
| 71 | + { |
| 72 | + return false; |
| 73 | + } |
| 74 | + } |
| 75 | + return true; |
| 76 | + } |
| 77 | + bool operator()(const std::vector<uint64_t> & /*v*/) noexcept { return true; } |
| 78 | + bool operator()(const std::vector<uint8_t> & /*v*/) noexcept { return true; } |
| 79 | + |
| 80 | + OPENTELEMETRY_EXPORT static bool IsValid(const OwnedAttributeValue &value) noexcept; |
| 81 | + |
| 82 | + OPENTELEMETRY_EXPORT static bool IsValid( |
| 83 | + const opentelemetry::common::AttributeValue &value) noexcept; |
| 84 | + |
| 85 | + OPENTELEMETRY_EXPORT static bool IsAllValid(const AttributeMap &attributes) noexcept; |
| 86 | + |
| 87 | + OPENTELEMETRY_EXPORT static bool IsAllValid(const OrderedAttributeMap &attributes) noexcept; |
| 88 | + |
| 89 | + OPENTELEMETRY_EXPORT static void Filter(AttributeMap &attributes, nostd::string_view log_hint); |
| 90 | + |
| 91 | + OPENTELEMETRY_EXPORT static void Filter(OrderedAttributeMap &attributes, |
| 92 | + nostd::string_view log_hint); |
| 93 | +}; |
| 94 | + |
| 95 | +/** |
| 96 | + * Supports internal iteration over a collection of key-value pairs and filtering of invalid |
| 97 | + * attributes. |
| 98 | + */ |
| 99 | +class OPENTELEMETRY_EXPORT KeyValueFilterIterable : public opentelemetry::common::KeyValueIterable |
| 100 | +{ |
| 101 | +public: |
| 102 | + KeyValueFilterIterable(const opentelemetry::common::KeyValueIterable &origin, |
| 103 | + opentelemetry::nostd::string_view log_hint) noexcept; |
| 104 | + |
| 105 | + ~KeyValueFilterIterable() override; |
| 106 | + |
| 107 | + bool ForEachKeyValue( |
| 108 | + opentelemetry::nostd::function_ref<bool(opentelemetry::nostd::string_view, |
| 109 | + opentelemetry::common::AttributeValue)> callback) |
| 110 | + const noexcept override; |
| 111 | + |
| 112 | + size_t size() const noexcept override; |
| 113 | + |
| 114 | +private: |
| 115 | + // Pointer to the original KeyValueIterable |
| 116 | + const opentelemetry::common::KeyValueIterable *origin_; |
| 117 | + |
| 118 | + // Size of valid attributes |
| 119 | + mutable size_t size_; |
| 120 | + |
| 121 | + // Log hint for invalid attributes |
| 122 | + opentelemetry::nostd::string_view log_hint_; |
| 123 | +}; |
| 124 | + |
| 125 | +} // namespace common |
| 126 | +} // namespace sdk |
| 127 | +OPENTELEMETRY_END_NAMESPACE |
0 commit comments