2 * Copyright (c) 2017 Uber Technologies, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
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"
29 namespace jaegertracing
{
33 class TestStatsReporter
: public metrics::StatsReporter
{
36 using ValueMap
= std::unordered_map
<std::string
, int64_t>;
38 virtual ~TestStatsReporter() = default;
40 void incCounter(const std::string
& name
,
42 const metrics::StatsReporter::TagMap
& tags
) override
44 std::lock_guard
<std::mutex
> lock(_mutex
);
46 const auto metricName
=
47 metrics::Metrics::addTagsToMetricName(name
, tags
);
48 auto& currentValue
= _counters
[metricName
];
49 currentValue
= currentValue
+ delta
;
52 void recordTimer(const std::string
& name
,
54 const metrics::StatsReporter::TagMap
& tags
) override
58 void updateGauge(const std::string
& name
,
60 const metrics::StatsReporter::TagMap
& tags
) override
64 int64_t counterValue(const std::string
& name
,
65 const metrics::StatsReporter::TagMap
& tags
) const
67 std::lock_guard
<std::mutex
> lock(_mutex
);
69 const auto metricName
=
70 metrics::Metrics::addTagsToMetricName(name
, tags
);
71 return _counters
[metricName
];
75 mutable ValueMap _counters
;
76 mutable std::mutex _mutex
;
79 constexpr auto kDefaultMaxValueLength
= 8;
81 class TestRestrictionManager
: public RestrictionManager
{
83 ~TestRestrictionManager() { close(); }
85 Restriction
getRestriction(const std::string
&,
86 const std::string
& key
) override
88 const auto keyAllowed
= (!key
.empty() && key
[0] == 'a');
89 return Restriction(keyAllowed
, kDefaultMaxValueLength
);
93 template <typename FieldIterator
>
94 void log(FieldIterator
, FieldIterator
)
98 } // anonymous namespace
100 TEST(Baggage
, restrictionManagerTest
)
102 auto logFn
= &log
<std::vector
<Tag
>::const_iterator
>;
103 TestRestrictionManager manager
;
104 auto metrics
= metrics::Metrics::makeNullMetrics();
105 BaggageSetter
setter(manager
, *metrics
);
108 SpanContext(TraceID(),
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"]);
123 TEST(Baggage
, testRemoteRestrictionManagerDefaults
)
125 auto logger
= logging::nullLogger();
126 auto metrics
= metrics::Metrics::makeNullMetrics();
127 RemoteRestrictionManager
manager(
131 RemoteRestrictionManager::Clock::duration(),
134 ASSERT_EQ(RemoteRestrictionManager::defaultRefreshInterval(),
135 manager
.refreshInterval());
137 Restriction(true, RemoteRestrictionManager::kDefaultMaxValueLength
),
138 manager
.getRestriction("test-service", "abc"));
141 TEST(Baggage
, testRemoteRestrictionManagerFunctionality
)
143 auto logger
= logging::consoleLogger();
145 TestStatsReporter testReporter
;
146 auto metrics
= metrics::Metrics::fromStatsReporter(testReporter
);
148 auto mockAgent
= testutils::MockAgent::make();
150 RemoteRestrictionManager
manager(
152 mockAgent
->samplingServerAddress().authority(),
154 std::chrono::milliseconds(100),
157 ASSERT_EQ(Restriction(false, 0),
158 manager
.getRestriction("test-service", "abc"));
159 mockAgent
->addBaggageRestriction("abc", Restriction(true, 1));
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) {
173 ASSERT_EQ(Restriction(true, 1),
174 manager
.getRestriction("test-service", "abc"));
175 ASSERT_EQ(Restriction(false, 0),
176 manager
.getRestriction("test-service", "bcd"));
180 } // namespace baggage
181 } // namespace jaegertracing