File: composite_propagator.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 (93 lines) | stat: -rw-r--r-- 2,482 bytes parent folder | download | duplicates (3)
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <memory>
#include <vector>

#include "opentelemetry/context/propagation/text_map_propagator.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace context
{
namespace propagation
{

class CompositePropagator : public TextMapPropagator
{
public:
  CompositePropagator(std::vector<std::unique_ptr<TextMapPropagator>> propagators)
      : propagators_(std::move(propagators))
  {}

  /**
   * Run each of the configured propagators with the given context and carrier.
   * Propagators are run in the order they are configured, so if multiple
   * propagators write the same carrier key, the propagator later in the list
   * will "win".
   *
   * @param carrier Carrier into which context will be injected
   * @param context Context to inject
   *
   */

  void Inject(TextMapCarrier &carrier, const context::Context &context) noexcept override
  {
    for (auto &p : propagators_)
    {
      p->Inject(carrier, context);
    }
  }

  /**
   * Run each of the configured propagators with the given context and carrier.
   * Propagators are run in the order they are configured, so if multiple
   * propagators write the same context key, the propagator later in the list
   * will "win".
   *
   * @param carrier Carrier from which to extract context
   * @param context Context to add values to
   */
  context::Context Extract(const TextMapCarrier &carrier,
                           context::Context &context) noexcept override
  {
    auto first = true;
    context::Context tmp_context;
    for (auto &p : propagators_)
    {
      if (first)
      {
        tmp_context = p->Extract(carrier, context);
        first       = false;
      }
      else
      {
        tmp_context = p->Extract(carrier, tmp_context);
      }
    }
    return propagators_.size() ? tmp_context : context;
  }

  /**
   * Invoke callback with  fields set to carrier by `inject` method for all the
   * configured propagators
   * Returns true if all invocation return true
   */
  bool Fields(nostd::function_ref<bool(nostd::string_view)> callback) const noexcept override
  {
    bool status = true;
    for (auto &p : propagators_)
    {
      status = status && p->Fields(callback);
    }
    return status;
  }

private:
  std::vector<std::unique_ptr<TextMapPropagator>> propagators_;
};
}  // namespace propagation
}  // namespace context
OPENTELEMETRY_END_NAMESPACE