-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtest_trace_sampler.cpp
More file actions
272 lines (223 loc) · 8.44 KB
/
test_trace_sampler.cpp
File metadata and controls
272 lines (223 loc) · 8.44 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <datadog/clock.h>
#include <datadog/id_generator.h>
#include <datadog/rate.h>
#include <datadog/sampling_priority.h>
#include <datadog/tracer.h>
#include <datadog/tracer_config.h>
#include <chrono>
#include <limits>
#include <map>
#include <ostream>
#include "mocks/collectors.h"
#include "null_logger.h"
#include "test.h"
namespace std {
// This is for printing the mapping between sampling priority and trace count
// when a test below fails.
std::ostream& operator<<(std::ostream& stream,
const std::map<int, std::size_t>& counts) {
stream << "{";
auto iter = counts.begin();
if (iter != counts.end()) {
stream << '\"' << iter->first << "\": " << iter->second;
for (++iter; iter != counts.end(); ++iter) {
stream << ", \"" << iter->first << "\": " << iter->second;
}
}
return stream << "}";
}
} // namespace std
namespace {
Rate assert_rate(double rate) {
// If `rate` is not valid, `std::variant` will throw an exception.
return *Rate::from(rate);
}
} // namespace
using namespace datadog::tracing;
TEST_CASE("trace sampling rule sample rate") {
// For a configured global sample rate, verify that the average proportion of
// traces kept matches the rate.
struct TestCase {
std::string name;
double sample_rate;
};
auto test_case = GENERATE(values<TestCase>({{"drop all", 0.0},
{"keep all", 1.0},
{"half", 0.5},
{"keep few", 0.01},
{"keep most", 0.99}}));
CAPTURE(test_case.name);
CAPTURE(test_case.sample_rate);
const std::size_t num_iterations = 10'000;
TracerConfig config;
config.service = "testsvc";
config.trace_sampler.sample_rate = test_case.sample_rate;
// Plenty of head room so that the limiter doesn't throttle us.
config.trace_sampler.max_per_second = num_iterations * 2;
const auto collector = std::make_shared<PriorityCountingCollector>();
config.collector = collector;
config.logger = std::make_shared<NullLogger>();
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
for (std::size_t i = 0; i < num_iterations; ++i) {
auto span = tracer.create_span();
(void)span;
}
auto& priority_counts = collector->sampling_priority_count;
CAPTURE(priority_counts);
// Some of the traces will have priority -1 ("user drop") and others will have
// priority 2 ("user keep"), but no other values.
REQUIRE(priority_counts.size() <= 2);
// I assume that there have been enough trials that not _all_ traces are kept
// or dropped purely due to chance. That could happen only if the sample rate
// were 0% or 100%, respectively.
REQUIRE((test_case.sample_rate == 0.0 ||
priority_counts.count(int(SamplingPriority::USER_KEEP))));
REQUIRE((test_case.sample_rate == 1.0 ||
priority_counts.count(int(SamplingPriority::USER_DROP))));
REQUIRE(collector->total_count() == num_iterations);
const double rate_kept = collector->ratio_of(SamplingPriority::USER_KEEP);
REQUIRE(rate_kept == Approx(test_case.sample_rate).margin(0.05));
}
TEST_CASE("trace sampling rate limiter") {
// Verify that the average proportion of traces kept over the course of a
// second does not exceed that allowed by the configured limit.
struct TestCase {
std::string name;
double max_per_second;
std::size_t burst_size;
std::size_t expected_kept_count;
};
auto test_case = GENERATE(values<TestCase>({{"allow one", 1.0, 100, 1},
{"allow all", 100.0, 100, 100},
{"allow some", 10.0, 100, 10}}));
CAPTURE(test_case.name);
CAPTURE(test_case.max_per_second);
CAPTURE(test_case.burst_size);
CAPTURE(test_case.expected_kept_count);
TracerConfig config;
config.service = "testsvc";
config.trace_sampler.sample_rate = 1.0;
config.trace_sampler.max_per_second = test_case.max_per_second;
const auto collector = std::make_shared<PriorityCountingCollector>();
config.collector = collector;
config.logger = std::make_shared<NullLogger>();
TimePoint current_time = default_clock();
// Modify `current_time` to advance the clock.
auto clock = [¤t_time]() { return current_time; };
auto finalized = finalize_config(config, clock);
REQUIRE(finalized);
Tracer tracer{*finalized};
for (std::size_t i = 0; i < test_case.burst_size; ++i) {
auto span = tracer.create_span();
(void)span;
}
REQUIRE(collector->total_count() == test_case.burst_size);
REQUIRE(collector->count_of(SamplingPriority::USER_KEEP) ==
test_case.expected_kept_count);
// Now verify that there is a "cooldown period" of one second, after which
// the limiter will permit some more traces. How many it permits depends
// on how "over budget" it was, but it will allow at least one.
collector->sampling_priority_count.clear();
current_time += std::chrono::seconds(1);
{
auto span = tracer.create_span();
(void)span;
}
REQUIRE(collector->count_of(SamplingPriority::USER_KEEP) == 1);
}
TEST_CASE("priority sampling") {
// Verify that a `TraceSampler` not otherwise configured will use whichever
// sample rates are sent back to it by the collector (Datadog Agent).
const std::size_t num_iterations = 10'000;
struct TestCase {
std::string name;
std::string service_key;
double sample_rate;
double expected_rate;
};
auto test_case = GENERATE(values<TestCase>(
{{"default rate", CollectorResponse::key_of_default_rate, 0.5, 0.5},
{"testsvc on dev", "service:testsvc,env:dev", 0.5, 0.5},
{"no match uses default of 100%", "service:unrelated,env:foo", 0.25,
1.0}}));
TracerConfig config;
config.service = "testsvc";
config.environment = "dev";
// plenty of head room
config.trace_sampler.max_per_second = 2 * num_iterations;
const auto collector =
std::make_shared<PriorityCountingCollectorWithResponse>();
config.collector = collector;
config.logger = std::make_shared<NullLogger>();
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
collector->response.sample_rate_by_key[test_case.service_key] =
assert_rate(test_case.sample_rate);
for (std::size_t i = 0; i < num_iterations; ++i) {
auto span = tracer.create_span();
(void)span;
}
REQUIRE(collector->total_count() == num_iterations);
// Priority sampling uses sampling priority 1 ("auto keep").
REQUIRE(collector->ratio_of(SamplingPriority::AUTO_KEEP) ==
Approx(test_case.expected_rate).margin(0.05));
}
TEST_CASE("sampling rules") {
TracerConfig config;
config.service = "testsvc";
const auto collector = std::make_shared<PriorityCountingCollector>();
config.collector = collector;
config.logger = std::make_shared<NullLogger>();
SECTION("no rule matches → priority sampling") {
TraceSamplerConfig::Rule rule;
rule.service = "foosvc";
config.trace_sampler.rules.push_back(rule);
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
{
auto span = tracer.create_span();
(void)span;
}
REQUIRE(collector->total_count() == 1);
REQUIRE(collector->count_of(SamplingPriority::AUTO_KEEP) +
collector->count_of(SamplingPriority::AUTO_DROP) ==
1);
}
SECTION("matches first rule") {
TraceSamplerConfig::Rule rule;
rule.service = "testsvc";
rule.sample_rate = 1.0; // this is also the default
config.trace_sampler.rules.push_back(rule);
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
{
auto span = tracer.create_span();
(void)span;
}
REQUIRE(collector->total_count() == 1);
REQUIRE(collector->count_of(SamplingPriority::USER_KEEP) == 1);
}
SECTION("matches second rule") {
TraceSamplerConfig::Rule rule;
rule.service = "foosvc";
rule.sample_rate = 1.0; // this is also the default
config.trace_sampler.rules.push_back(rule);
rule.service = "testsvc";
rule.sample_rate = 0.0;
config.trace_sampler.rules.push_back(rule);
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
{
auto span = tracer.create_span();
(void)span;
}
REQUIRE(collector->total_count() == 1);
REQUIRE(collector->count_of(SamplingPriority::USER_DROP) == 1);
}
}