File: span_context_kv_iterable_view.h

package info (click to toggle)
opentelemetry-cpp 1.23.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,372 kB
  • sloc: cpp: 96,239; sh: 1,766; makefile: 36; python: 31
file content (119 lines) | stat: -rw-r--r-- 3,795 bytes parent folder | download | duplicates (10)
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <iterator>
#include <type_traits>
#include <utility>

#include "opentelemetry/common/key_value_iterable_view.h"
#include "opentelemetry/nostd/function_ref.h"
#include "opentelemetry/nostd/span.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/nostd/utility.h"
#include "opentelemetry/trace/span_context.h"
#include "opentelemetry/trace/span_context_kv_iterable.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace trace
{
// NOTE - code within `detail` namespace implements internal details, and not part
// of the public interface.
namespace detail
{
template <class T>
inline void take_span_context_kv(SpanContext, opentelemetry::common::KeyValueIterableView<T>)
{}

template <class T, nostd::enable_if_t<common::detail::is_key_value_iterable<T>::value> * = nullptr>
inline void take_span_context_kv(SpanContext, T &)
{}

inline void take_span_context_kv(
    SpanContext,
    std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>)
{}

template <class T>
auto is_span_context_kv_iterable_impl(T iterable)
    -> decltype(take_span_context_kv(std::begin(iterable)->first, std::begin(iterable)->second),
                nostd::size(iterable),
                std::true_type{});

std::false_type is_span_context_kv_iterable_impl(...);

template <class T>
struct is_span_context_kv_iterable
{
  static const bool value =
      decltype(detail::is_span_context_kv_iterable_impl(std::declval<T>()))::value;
};
}  // namespace detail

template <class T>
class SpanContextKeyValueIterableView final : public SpanContextKeyValueIterable
{
  static_assert(detail::is_span_context_kv_iterable<T>::value,
                "Must be a context/key-value iterable");

public:
  explicit SpanContextKeyValueIterableView(const T &links) noexcept : container_{&links} {}

  bool ForEachKeyValue(nostd::function_ref<bool(SpanContext, const common::KeyValueIterable &)>
                           callback) const noexcept override
  {
    auto iter = std::begin(*container_);
    auto last = std::end(*container_);
    for (; iter != last; ++iter)
    {
      if (!this->do_callback(iter->first, iter->second, callback))
      {
        return false;
      }
    }
    return true;
  }

  size_t size() const noexcept override { return nostd::size(*container_); }

private:
  const T *container_;

  bool do_callback(SpanContext span_context,
                   const common::KeyValueIterable &attributes,
                   nostd::function_ref<bool(SpanContext, const common::KeyValueIterable &)>
                       callback) const noexcept
  {
    if (!callback(span_context, attributes))
    {
      return false;
    }
    return true;
  }

  template <class U,
            nostd::enable_if_t<common::detail::is_key_value_iterable<U>::value> * = nullptr>
  bool do_callback(SpanContext span_context,
                   const U &attributes,
                   nostd::function_ref<bool(SpanContext, const common::KeyValueIterable &)>
                       callback) const noexcept
  {
    return do_callback(span_context, common::KeyValueIterableView<U>(attributes), callback);
  }

  bool do_callback(
      SpanContext span_context,
      std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> attributes,
      nostd::function_ref<bool(SpanContext, const common::KeyValueIterable &)> callback)
      const noexcept
  {
    return do_callback(span_context,
                       nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
                           attributes.begin(), attributes.end()},
                       callback);
  }
};
}  // namespace trace
OPENTELEMETRY_END_NAMESPACE