]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/jaeger-client-cpp/src/jaegertracing/baggage/BaggageTest.cpp
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / jaeger-client-cpp / src / jaegertracing / baggage / BaggageTest.cpp
1 /*
2 * Copyright (c) 2017 Uber Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <gtest/gtest.h>
18
19 #include "jaegertracing/baggage/BaggageSetter.h"
20 #include "jaegertracing/baggage/RemoteRestrictionManager.h"
21 #include "jaegertracing/baggage/RestrictionManager.h"
22 #include "jaegertracing/baggage/RestrictionsConfig.h"
23 #include "jaegertracing/metrics/Metrics.h"
24 #include "jaegertracing/metrics/InMemoryStatsReporter.h"
25 #include "jaegertracing/testutils/MockAgent.h"
26
27 #include <mutex>
28
29 namespace jaegertracing {
30 namespace baggage {
31 namespace {
32
33 class TestStatsReporter : public metrics::StatsReporter {
34 public:
35
36 using ValueMap = std::unordered_map<std::string, int64_t>;
37
38 virtual ~TestStatsReporter() = default;
39
40 void incCounter(const std::string& name,
41 int64_t delta,
42 const metrics::StatsReporter::TagMap& tags) override
43 {
44 std::lock_guard<std::mutex> lock(_mutex);
45
46 const auto metricName =
47 metrics::Metrics::addTagsToMetricName(name, tags);
48 auto& currentValue = _counters[metricName];
49 currentValue = currentValue + delta;
50 }
51
52 void recordTimer(const std::string& name,
53 int64_t time,
54 const metrics::StatsReporter::TagMap& tags) override
55 {
56 }
57
58 void updateGauge(const std::string& name,
59 int64_t time,
60 const metrics::StatsReporter::TagMap& tags) override
61 {
62 }
63
64 int64_t counterValue(const std::string& name,
65 const metrics::StatsReporter::TagMap& tags) const
66 {
67 std::lock_guard<std::mutex> lock(_mutex);
68
69 const auto metricName =
70 metrics::Metrics::addTagsToMetricName(name, tags);
71 return _counters[metricName];
72 }
73
74 private:
75 mutable ValueMap _counters;
76 mutable std::mutex _mutex;
77 };
78
79 constexpr auto kDefaultMaxValueLength = 8;
80
81 class TestRestrictionManager : public RestrictionManager {
82 public:
83 ~TestRestrictionManager() { close(); }
84
85 Restriction getRestriction(const std::string&,
86 const std::string& key) override
87 {
88 const auto keyAllowed = (!key.empty() && key[0] == 'a');
89 return Restriction(keyAllowed, kDefaultMaxValueLength);
90 }
91 };
92
93 template <typename FieldIterator>
94 void log(FieldIterator, FieldIterator)
95 {
96 }
97
98 } // anonymous namespace
99
100 TEST(Baggage, restrictionManagerTest)
101 {
102 auto logFn = &log<std::vector<Tag>::const_iterator>;
103 TestRestrictionManager manager;
104 auto metrics = metrics::Metrics::makeNullMetrics();
105 BaggageSetter setter(manager, *metrics);
106 Span span(
107 nullptr,
108 SpanContext(TraceID(),
109 123,
110 456,
111 static_cast<unsigned char>(SpanContext::Flag::kSampled),
112 SpanContext::StrMap()));
113 auto baggage = span.context().baggage();
114 setter.setBaggage(span, baggage, "abc", "123", logFn);
115 ASSERT_EQ(1, baggage.size());
116 ASSERT_EQ("123", baggage["abc"]);
117 setter.setBaggage(span, baggage, "bcd", "234", logFn);
118 ASSERT_EQ(1, baggage.size());
119 setter.setBaggage(span, baggage, "abc", "1234567890", logFn);
120 ASSERT_EQ("12345678", baggage["abc"]);
121 }
122
123 TEST(Baggage, testRemoteRestrictionManagerDefaults)
124 {
125 auto logger = logging::nullLogger();
126 auto metrics = metrics::Metrics::makeNullMetrics();
127 RemoteRestrictionManager manager(
128 "test-service",
129 "",
130 false,
131 RemoteRestrictionManager::Clock::duration(),
132 *logger,
133 *metrics);
134 ASSERT_EQ(RemoteRestrictionManager::defaultRefreshInterval(),
135 manager.refreshInterval());
136 ASSERT_EQ(
137 Restriction(true, RemoteRestrictionManager::kDefaultMaxValueLength),
138 manager.getRestriction("test-service", "abc"));
139 }
140
141 TEST(Baggage, testRemoteRestrictionManagerFunctionality)
142 {
143 auto logger = logging::consoleLogger();
144
145 TestStatsReporter testReporter;
146 auto metrics = metrics::Metrics::fromStatsReporter(testReporter);
147
148 auto mockAgent = testutils::MockAgent::make();
149 mockAgent->start();
150 RemoteRestrictionManager manager(
151 "test-service",
152 mockAgent->samplingServerAddress().authority(),
153 true,
154 std::chrono::milliseconds(100),
155 *logger,
156 *metrics);
157 ASSERT_EQ(Restriction(false, 0),
158 manager.getRestriction("test-service", "abc"));
159 mockAgent->addBaggageRestriction("abc", Restriction(true, 1));
160
161 // Wait for the Update Restrictions to occur
162 // This is done by listening to the number of update published via the Metrics
163 // Typically, On Windows with Debug mode, the update happens after 700ms
164 for (int sleepIndex = 0; sleepIndex < 100; ++sleepIndex) {
165 auto counterVal = testReporter.counterValue(
166 "jaeger.baggage-restrictions-update", { { "result", "ok" } });
167 std::this_thread::sleep_for(std::chrono::milliseconds(200));
168 if (counterVal > 0) {
169 break;
170 }
171 }
172
173 ASSERT_EQ(Restriction(true, 1),
174 manager.getRestriction("test-service", "abc"));
175 ASSERT_EQ(Restriction(false, 0),
176 manager.getRestriction("test-service", "bcd"));
177 manager.close();
178 }
179
180 } // namespace baggage
181 } // namespace jaegertracing