]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Copyright The OpenTelemetry Authors |
2 | // SPDX-License-Identifier: Apache-2.0 | |
3 | ||
4 | #pragma once | |
5 | ||
6 | #include <initializer_list> | |
7 | #include <memory> | |
8 | #include <vector> | |
9 | #include "opentelemetry/context/propagation/text_map_propagator.h" | |
10 | ||
11 | OPENTELEMETRY_BEGIN_NAMESPACE | |
12 | namespace context | |
13 | { | |
14 | namespace propagation | |
15 | { | |
16 | ||
17 | class CompositePropagator : public TextMapPropagator | |
18 | { | |
19 | public: | |
20 | CompositePropagator(std::vector<std::unique_ptr<TextMapPropagator>> propagators) | |
21 | : propagators_(std::move(propagators)) | |
22 | {} | |
23 | ||
24 | /** | |
25 | * Run each of the configured propagators with the given context and carrier. | |
26 | * Propagators are run in the order they are configured, so if multiple | |
27 | * propagators write the same carrier key, the propagator later in the list | |
28 | * will "win". | |
29 | * | |
30 | * @param carrier Carrier into which context will be injected | |
31 | * @param context Context to inject | |
32 | * | |
33 | */ | |
34 | ||
35 | void Inject(TextMapCarrier &carrier, const context::Context &context) noexcept override | |
36 | { | |
37 | for (auto &p : propagators_) | |
38 | { | |
39 | p->Inject(carrier, context); | |
40 | } | |
41 | } | |
42 | ||
43 | /** | |
44 | * Run each of the configured propagators with the given context and carrier. | |
45 | * Propagators are run in the order they are configured, so if multiple | |
46 | * propagators write the same context key, the propagator later in the list | |
47 | * will "win". | |
48 | * | |
49 | * @param carrier Carrier from which to extract context | |
50 | * @param context Context to add values to | |
51 | */ | |
52 | context::Context Extract(const TextMapCarrier &carrier, | |
53 | context::Context &context) noexcept override | |
54 | { | |
55 | auto first = true; | |
56 | context::Context tmp_context; | |
57 | for (auto &p : propagators_) | |
58 | { | |
59 | if (first) | |
60 | { | |
61 | tmp_context = p->Extract(carrier, context); | |
62 | first = false; | |
63 | } | |
64 | else | |
65 | { | |
66 | tmp_context = p->Extract(carrier, tmp_context); | |
67 | } | |
68 | } | |
69 | return propagators_.size() ? tmp_context : context; | |
70 | } | |
71 | ||
72 | /** | |
73 | * Invoke callback with fields set to carrier by `inject` method for all the | |
74 | * configured propagators | |
75 | * Returns true if all invocation return true | |
76 | */ | |
77 | bool Fields(nostd::function_ref<bool(nostd::string_view)> callback) const noexcept override | |
78 | { | |
79 | bool status = true; | |
80 | for (auto &p : propagators_) | |
81 | { | |
82 | status = status && p->Fields(callback); | |
83 | } | |
84 | return status; | |
85 | } | |
86 | ||
87 | private: | |
88 | std::vector<std::unique_ptr<TextMapPropagator>> propagators_; | |
89 | }; | |
90 | } // namespace propagation | |
91 | } // namespace context | |
92 | OPENTELEMETRY_END_NAMESPACE; |