]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/api/include/opentelemetry/trace/span_context_kv_iterable_view.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / api / include / opentelemetry / trace / span_context_kv_iterable_view.h
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 #pragma once
5
6 #include <iterator>
7 #include <type_traits>
8 #include <utility>
9
10 #include "opentelemetry/common/key_value_iterable_view.h"
11 #include "opentelemetry/nostd/utility.h"
12 #include "opentelemetry/trace/span_context_kv_iterable.h"
13 #include "opentelemetry/version.h"
14
15 OPENTELEMETRY_BEGIN_NAMESPACE
16 namespace trace
17 {
18 // NOTE - code within `detail` namespace implements internal details, and not part
19 // of the public interface.
20 namespace detail
21 {
22 template <class T>
23 inline void take_span_context_kv(SpanContext, common::KeyValueIterableView<T>)
24 {}
25
26 template <class T, nostd::enable_if_t<common::detail::is_key_value_iterable<T>::value> * = nullptr>
27 inline void take_span_context_kv(SpanContext, T &)
28 {}
29
30 inline void take_span_context_kv(
31 SpanContext,
32 std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>)
33 {}
34
35 template <class T>
36 auto is_span_context_kv_iterable_impl(T iterable)
37 -> decltype(take_span_context_kv(std::begin(iterable)->first, std::begin(iterable)->second),
38 nostd::size(iterable),
39 std::true_type{});
40
41 std::false_type is_span_context_kv_iterable_impl(...);
42
43 template <class T>
44 struct is_span_context_kv_iterable
45 {
46 static const bool value =
47 decltype(detail::is_span_context_kv_iterable_impl(std::declval<T>()))::value;
48 };
49 } // namespace detail
50
51 template <class T>
52 class SpanContextKeyValueIterableView final : public SpanContextKeyValueIterable
53 {
54 static_assert(detail::is_span_context_kv_iterable<T>::value,
55 "Must be a context/key-value iterable");
56
57 public:
58 explicit SpanContextKeyValueIterableView(const T &links) noexcept : container_{&links} {}
59
60 bool ForEachKeyValue(
61 nostd::function_ref<bool(SpanContext, const opentelemetry::common::KeyValueIterable &)>
62 callback) const noexcept override
63 {
64 auto iter = std::begin(*container_);
65 auto last = std::end(*container_);
66 for (; iter != last; ++iter)
67 {
68 if (!this->do_callback(iter->first, iter->second, callback))
69 {
70 return false;
71 }
72 }
73 return true;
74 }
75
76 size_t size() const noexcept override { return nostd::size(*container_); }
77
78 private:
79 const T *container_;
80
81 bool do_callback(
82 SpanContext span_context,
83 const common::KeyValueIterable &attributes,
84 nostd::function_ref<bool(SpanContext, const opentelemetry::common::KeyValueIterable &)>
85 callback) const noexcept
86 {
87 if (!callback(span_context, attributes))
88 {
89 return false;
90 }
91 return true;
92 }
93
94 template <class U,
95 nostd::enable_if_t<common::detail::is_key_value_iterable<U>::value> * = nullptr>
96 bool do_callback(SpanContext span_context,
97 const U &attributes,
98 nostd::function_ref<bool(SpanContext, const common::KeyValueIterable &)>
99 callback) const noexcept
100 {
101 return do_callback(span_context, common::KeyValueIterableView<U>(attributes), callback);
102 }
103
104 bool do_callback(
105 SpanContext span_context,
106 std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> attributes,
107 nostd::function_ref<bool(SpanContext, const common::KeyValueIterable &)> callback)
108 const noexcept
109 {
110 return do_callback(span_context,
111 nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
112 attributes.begin(), attributes.end()},
113 callback);
114 }
115 };
116 } // namespace trace
117 OPENTELEMETRY_END_NAMESPACE