1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <stddef.h>
#include <initializer_list>
#include <iterator>
#include <type_traits>
#include <utility>
#include <vector>
#include "opentelemetry/common/attribute_value.h"
#include "opentelemetry/common/key_value_iterable.h"
#include "opentelemetry/nostd/function_ref.h"
#include "opentelemetry/nostd/span.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/nostd/type_traits.h"
#include "opentelemetry/nostd/utility.h"
#include "opentelemetry/version.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace common
{
// NOTE - code within `detail` namespace implements internal details, and not part
// of the public interface.
namespace detail
{
inline void take_key_value(nostd::string_view, common::AttributeValue) {}
template <class T>
auto is_key_value_iterable_impl(T iterable)
-> decltype(take_key_value(std::begin(iterable)->first, std::begin(iterable)->second),
nostd::size(iterable),
std::true_type{});
std::false_type is_key_value_iterable_impl(...);
template <class T>
struct is_key_value_iterable
{
static const bool value = decltype(detail::is_key_value_iterable_impl(std::declval<T>()))::value;
};
} // namespace detail
/**
* @brief Container for key-value pairs that can transform every value in it to one of types
* listed in common::AttributeValue. It may contain value types that are not directly map'able
* to primitive value types. In that case the `ForEachKeyValue` method acts as a transform to
* convert the value type to one listed under AtributeValue (bool, int32_t, int64_t, uint32_t,
* uint64_t, double, nostd::string_view, or arrays of primite types). For example, if UUID,
* GUID, or UTF-16 string type is passed as one of values stored inside this container, the
* container itself may provide a custom implementation of `ForEachKeyValue` to transform the
* 'non-standard' type to one of the standard types.
*/
template <class T>
class KeyValueIterableView final : public KeyValueIterable
{
public:
explicit KeyValueIterableView(const T &container) noexcept : container_{&container} {}
// KeyValueIterable
bool ForEachKeyValue(nostd::function_ref<bool(nostd::string_view, common::AttributeValue)>
callback) const noexcept override
{
auto iter = std::begin(*container_);
auto last = std::end(*container_);
for (; iter != last; ++iter)
{
if (!callback(iter->first, iter->second))
{
return false;
}
}
return true;
}
size_t size() const noexcept override { return nostd::size(*container_); }
private:
const T *container_;
};
template <class T, nostd::enable_if_t<detail::is_key_value_iterable<T>::value> * = nullptr>
KeyValueIterableView<T> MakeKeyValueIterableView(const T &container) noexcept
{
return KeyValueIterableView<T>(container);
}
/**
* Utility function to help to make a attribute view from initializer_list
*
* @param attributes The initializer_list of key-value pairs
* @return nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>
*/
inline static nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>
MakeAttributes(std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>
attributes) noexcept
{
return nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
attributes.begin(), attributes.end()};
}
/**
* Utility function to help to make a attribute view from a span
*
* @param attributes The span of key-value pairs
* @return nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>
*/
inline static nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>
MakeAttributes(
nostd::span<const std::pair<nostd::string_view, common::AttributeValue>> attributes) noexcept
{
return attributes;
}
/**
* Utility function to help to make a attribute view from a KeyValueIterable
*
* @param attributes The KeyValueIterable of key-value pairs
* @return common::KeyValueIterable
*/
inline static const common::KeyValueIterable &MakeAttributes(
const common::KeyValueIterable &attributes) noexcept
{
return attributes;
}
/**
* Utility function to help to make a attribute view from a key-value iterable object
*
* @tparam ArgumentType Expected to be ArgumentType
* @param attributes The key-value iterable object
* @return nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>
*/
template <
class ArgumentType,
nostd::enable_if_t<common::detail::is_key_value_iterable<ArgumentType>::value> * = nullptr>
inline static common::KeyValueIterableView<ArgumentType> MakeAttributes(
const ArgumentType &attributes) noexcept
{
return common::KeyValueIterableView<ArgumentType>(attributes);
}
} // namespace common
OPENTELEMETRY_END_NAMESPACE
|