-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_trace_segment.cpp
More file actions
612 lines (543 loc) · 20.3 KB
/
test_trace_segment.cpp
File metadata and controls
612 lines (543 loc) · 20.3 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#include <datadog/null_collector.h>
#include <datadog/optional.h>
#include <datadog/platform_util.h>
#include <datadog/rate.h>
#include <datadog/tags.h>
#include <datadog/trace_segment.h>
#include <datadog/tracer.h>
#include <datadog/tracer_config.h>
#include <cstdio>
#include <regex>
#include <string>
#include <vector>
#include "matchers.h"
#include "mocks/collectors.h"
#include "mocks/dict_readers.h"
#include "mocks/dict_writers.h"
#include "mocks/loggers.h"
#include "null_logger.h"
#include "test.h"
using namespace datadog::tracing;
namespace {
Rate assert_rate(double rate) {
// If `rate` is not valid, `std::variant` will throw an exception.
return *Rate::from(rate);
}
} // namespace
TEST_CASE("TraceSegment accessors") {
TracerConfig config;
config.service = "testsvc";
const auto collector = std::make_shared<MockCollector>();
config.collector = collector;
config.logger = std::make_shared<MockLogger>();
SECTION("hostname") {
const bool report_hostname = GENERATE(true, false);
config.report_hostname = report_hostname;
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
auto span = tracer.create_span();
auto hostname = span.trace_segment().hostname();
if (report_hostname) {
REQUIRE(hostname);
} else {
REQUIRE(!hostname);
}
}
SECTION("defaults") {
const std::unordered_map<std::string, std::string> tags{{"hello", "world"},
{"foo", "bar"}};
config.name = "wobble";
config.service_type = "fake";
config.version = "v0";
config.environment = "test";
config.tags = tags;
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
auto span = tracer.create_span();
const auto span_default = span.trace_segment().defaults();
CHECK(span_default.service == "testsvc");
CHECK(span_default.name == "wobble");
CHECK(span_default.service_type == "fake");
CHECK(span_default.version == "v0");
CHECK(span_default.environment == "test");
CHECK(span_default.tags == tags);
}
SECTION("origin") {
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
const std::unordered_map<std::string, std::string> headers{
{"x-datadog-trace-id", "123"},
{"x-datadog-parent-id", "456"},
{"x-datadog-origin", "Unalaska"}};
MockDictReader reader{headers};
auto span = tracer.extract_span(reader);
REQUIRE(span);
REQUIRE(span->trace_segment().origin() == "Unalaska");
}
SECTION("sampling_decision") {
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
SECTION("default create_span → no decision") {
auto span = tracer.create_span();
auto decision = span.trace_segment().sampling_decision();
REQUIRE(!decision);
}
SECTION("after injecting at least once → local decision") {
auto span = tracer.create_span();
MockDictWriter writer;
span.inject(writer);
auto decision = span.trace_segment().sampling_decision();
REQUIRE(decision);
REQUIRE(decision->origin == SamplingDecision::Origin::LOCAL);
}
SECTION("extracted priority → extracted decision") {
const std::unordered_map<std::string, std::string> headers{
{"x-datadog-trace-id", "123"},
{"x-datadog-parent-id", "456"},
{"x-datadog-sampling-priority", "7"}}; // 😯
MockDictReader reader{headers};
auto span = tracer.extract_span(reader);
REQUIRE(span);
auto decision = span->trace_segment().sampling_decision();
REQUIRE(decision);
REQUIRE(decision->origin == SamplingDecision::Origin::EXTRACTED);
}
SECTION("override on segment → local decision") {
auto span = tracer.create_span();
span.trace_segment().override_sampling_priority(-10); // 😵
auto decision = span.trace_segment().sampling_decision();
REQUIRE(decision);
REQUIRE(decision->origin == SamplingDecision::Origin::LOCAL);
}
}
SECTION("logger") {
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
auto span = tracer.create_span();
REQUIRE(&span.trace_segment().logger() == config.logger.get());
}
}
TEST_CASE("When Collector::send fails, TraceSegment logs the error.") {
TracerConfig config;
config.service = "testsvc";
const auto collector = std::make_shared<FailureCollector>();
config.collector = collector;
const auto logger = std::make_shared<MockLogger>();
config.logger = logger;
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
{
// The only span, created and then destroyed, so that the `TraceSegment`
// will `.send` it to the `Collector`, which will fail.
auto span = tracer.create_span();
(void)span;
}
REQUIRE(logger->error_count() == 1);
REQUIRE(logger->first_error().code == collector->failure.code);
}
TEST_CASE("TraceSegment finalization of spans") {
TracerConfig config;
config.service = "testsvc";
const auto collector = std::make_shared<MockCollector>();
config.collector = collector;
config.logger = std::make_shared<MockLogger>();
SECTION("root span") {
SECTION(
"'inject_max_size' propagation error if X-Datadog-Tags oversized on "
"inject") {
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
// Make a very large X-Datadog-Tags value.
std::string trace_tags_value = "foo=bar";
for (int i = 0; i < 10'000; ++i) {
trace_tags_value += ',';
trace_tags_value += "_dd.p.";
trace_tags_value += std::to_string(i);
trace_tags_value += '=';
trace_tags_value += std::to_string(2 * i);
}
const std::unordered_map<std::string, std::string> headers{
{"x-datadog-trace-id", "123"},
{"x-datadog-parent-id", "456"},
{"x-datadog-tags", trace_tags_value}};
MockDictReader reader{headers};
{
auto span = tracer.extract_span(reader);
REQUIRE(span);
// Injecting the oversized X-Datadog-Tags will make `TraceSegment` note
// an error, which it will later tag on the root span.
MockDictWriter writer;
span->inject(writer);
REQUIRE(writer.items.count("x-datadog-tags") == 0);
}
REQUIRE(collector->first_span().tags.at(
tags::internal::propagation_error) == "inject_max_size");
}
SECTION("sampling priority") {
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
SECTION("create trace -> priority in root span") {
{
auto root = tracer.create_span();
(void)root;
}
REQUIRE(collector->span_count() == 1);
const auto& span = collector->first_span();
REQUIRE(span.numeric_tags.count(tags::internal::sampling_priority) ==
1);
// The value depends on the trace ID, so we won't check it here.
}
SECTION(
"extracted sampling priority -> local root sampling priority same as "
"extracted") {
auto sampling_priority = GENERATE(-1, 0, 1, 2);
const std::unordered_map<std::string, std::string> headers{
{"x-datadog-trace-id", "123"},
{"x-datadog-parent-id", "456"},
{"x-datadog-sampling-priority", std::to_string(sampling_priority)},
};
MockDictReader reader{headers};
{ auto span = tracer.extract_span(reader); }
REQUIRE(collector->span_count() == 1);
REQUIRE(collector->first_span().numeric_tags.at(
tags::internal::sampling_priority) == sampling_priority);
}
SECTION(
"override sampling priority -> local root sampling priority same as "
"override") {
auto sampling_priority = GENERATE(-1, 0, 1, 2);
{
auto root = tracer.create_span();
root.trace_segment().override_sampling_priority(sampling_priority);
}
REQUIRE(collector->span_count() == 1);
REQUIRE(collector->first_span().numeric_tags.at(
tags::internal::sampling_priority) == sampling_priority);
}
SECTION(
"inject span -> injected priority is the same as that sent to agent "
"in local root span") {
MockDictWriter writer;
{
auto root = tracer.create_span();
root.inject(writer);
}
REQUIRE(collector->span_count() == 1);
REQUIRE(std::to_string(int(collector->first_span().numeric_tags.at(
tags::internal::sampling_priority))) ==
writer.items.at("x-datadog-sampling-priority"));
}
}
SECTION("hostname") {
config.report_hostname = true;
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
{
auto root = tracer.create_span();
(void)root;
}
REQUIRE(collector->span_count() == 1);
REQUIRE(collector->first_span().tags.at(tags::internal::hostname) ==
get_hostname());
}
SECTION("x-datadog-tags") {
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
const std::unordered_map<std::string, std::string> headers{
{"x-datadog-trace-id", "123"},
{"x-datadog-parent-id", "456"},
{"x-datadog-tags", "_dd.p.one=1,_dd.p.two=2,three=3"},
};
MockDictReader reader{headers};
{
auto span = tracer.extract_span(reader);
(void)span;
}
const std::unordered_map<std::string, std::string> filtered{
{"_dd.p.one", "1"}, {"_dd.p.two", "2"}};
REQUIRE(collector->span_count() == 1);
const auto& span = collector->first_span();
// "three" will be discarded, but not the other two.
REQUIRE(span.tags.count("three") == 0);
REQUIRE_THAT(span.tags, ContainsSubset(filtered));
// "_dd.p.dm" will be added, because we made a sampling decision.
REQUIRE(span.tags.count("_dd.p.dm") == 1);
// "_dd.p.ksr" is NOT set because this uses the DEFAULT mechanism (no
// agent configuration received yet).
REQUIRE(span.tags.count("_dd.p.ksr") == 0);
}
SECTION("rate tags") {
SECTION("default mechanism (100%) -> agent psr tag on first trace") {
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
{
auto span = tracer.create_span();
(void)span;
}
REQUIRE(collector->span_count() == 1);
const auto& span = collector->first_span();
REQUIRE(span.numeric_tags.at(tags::internal::agent_sample_rate) == 1.0);
// ksr is NOT set for the DEFAULT mechanism.
REQUIRE(span.tags.count(tags::internal::ksr) == 0);
}
SECTION(
"agent catch-all response @100% -> agent psr tag on second trace") {
const auto collector_response =
std::make_shared<MockCollectorWithResponse>();
collector_response->response
.sample_rate_by_key[CollectorResponse::key_of_default_rate] =
assert_rate(1.0);
config.collector = collector_response;
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
// First trace doesn't have a collector-specified sample rate.
{
auto span = tracer.create_span();
(void)span;
}
{
REQUIRE(collector_response->span_count() == 1);
const auto& span = collector_response->first_span();
// ksr is NOT set for the DEFAULT mechanism.
CHECK(span.tags.count(tags::internal::ksr) == 0);
}
collector_response->chunks.clear();
// Second trace will use the rate from `collector->response`.
{
auto span = tracer.create_span();
(void)span;
}
{
REQUIRE(collector_response->span_count() == 1);
const auto& span = collector_response->first_span();
CHECK(span.numeric_tags.at(tags::internal::agent_sample_rate) == 1.0);
CHECK(span.tags.at(tags::internal::ksr) == "1");
}
}
SECTION("rules (implicit and explicit)") {
// When sample rate is 100%, the sampler will consult the limiter.
// When sample rate is 0%, it won't. We test both cases.
auto sample_rate = GENERATE(0.0, 1.0);
SECTION("global sample rate") {
config.trace_sampler.sample_rate = sample_rate;
}
SECTION("sampling rule") {
TraceSamplerConfig::Rule rule;
rule.service = "testsvc";
rule.sample_rate = sample_rate;
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->span_count() == 1);
const auto& span = collector->first_span();
REQUIRE(span.numeric_tags.at(tags::internal::rule_sample_rate) ==
sample_rate);
{
char buf[32];
std::snprintf(buf, sizeof(buf), "%.6g", sample_rate);
CHECK(span.tags.at(tags::internal::ksr) == std::string(buf));
}
if (sample_rate == 1.0) {
REQUIRE(span.numeric_tags.at(
tags::internal::rule_limiter_sample_rate) == 1.0);
} else {
REQUIRE(sample_rate == 0.0);
REQUIRE(span.numeric_tags.count(
tags::internal::rule_limiter_sample_rate) == 0);
}
}
}
} // root span
SECTION(
"every span tagged with: _dd.origin, process_id, language, resource-id") {
const auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
std::unordered_map<std::string, std::string> headers;
headers["x-datadog-trace-id"] = "123";
headers["x-datadog-parent-id"] = "456";
headers["x-datadog-origin"] = "พัทยา";
MockDictReader reader{headers};
auto maybe_span = tracer.extract_span(reader);
REQUIRE(maybe_span);
{
std::vector<Span> spans;
spans.push_back(std::move(*maybe_span));
// Create some descendants.
for (int i = 0; i < 10; ++i) {
spans.push_back(spans.front().create_child());
spans.push_back(spans.back().create_child());
}
}
const int process_id = get_process_id();
// clang-format off
const char uuid_pattern[] =
"[0-9a-f]{8}"
"-"
"[0-9a-f]{4}"
"-"
"[0-9a-f]{4}"
"-"
"[0-9a-f]{4}"
"-"
"[0-9a-f]{12}";
// clang-format on
const std::regex uuid_regex{uuid_pattern};
REQUIRE(collector->span_count() == 2 * 10 + 1);
for (const auto& chunk : collector->chunks) {
for (const auto& span : chunk) {
REQUIRE(span);
auto found_string = span->tags.find(tags::internal::origin);
REQUIRE(found_string != span->tags.end());
REQUIRE(found_string->second == "พัทยา");
found_string = span->tags.find(tags::internal::language);
REQUIRE(found_string != span->tags.end());
REQUIRE(found_string->second == "cpp");
found_string = span->tags.find(tags::internal::runtime_id);
REQUIRE(found_string != span->tags.end());
const auto found_uuid = found_string->second;
CAPTURE(found_uuid);
REQUIRE(std::regex_match(found_uuid, uuid_regex));
const auto found_number =
span->numeric_tags.find(tags::internal::process_id);
REQUIRE(found_number != span->numeric_tags.end());
REQUIRE(found_number->second == process_id);
}
}
}
} // span finalizers
TEST_CASE("independent of Tracer") {
// This test verifies that a `TraceSegment` (via the `Span`s that refer to it)
// can continue to operate even after the `Tracer` that created it is
// destroyed.
//
// Primarily, the test checks that the code doesn't crash in this scenario.
TracerConfig config;
config.service = "testsvc";
config.name = "do.thing";
config.collector = std::make_shared<NullCollector>();
config.logger = std::make_shared<NullLogger>();
auto maybe_tracer = finalize_config(config);
REQUIRE(maybe_tracer);
Optional<Tracer> tracer{*maybe_tracer};
Span root = tracer->create_span();
Span child = root.create_child();
tracer.reset();
}
TEST_CASE("http.endpoint population") {
TracerConfig config;
config.service = "testsvc";
const auto collector = std::make_shared<MockCollector>();
config.collector = collector;
config.logger = std::make_shared<MockLogger>();
SECTION("DISABLED -> never adds http.endpoint") {
// default is disabled
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
{
auto span = tracer.create_span();
span.set_tag(tags::http_url, "http://example.com/users/12?x=y");
}
REQUIRE(collector->span_count() == 1);
const auto& span = collector->first_span();
REQUIRE(span.tags.count(tags::http_endpoint) == 0);
}
SECTION("FALLBACK mode -> adds only when http.route is absent") {
config.resource_renaming_enabled = {true};
config.resource_renaming_always_simplified_endpoint = {false};
auto finalized = finalize_config(config);
REQUIRE(finalized);
REQUIRE(finalized->resource_renaming_mode ==
HttpEndpointCalculationMode::FALLBACK);
SECTION("route absent -> endpoint added from url path") {
Tracer tracer{*finalized};
{
auto span = tracer.create_span();
span.set_tag(tags::http_url, "http://example.com/users/12?x=y");
}
REQUIRE(collector->span_count() == 1);
const auto& span = collector->first_span();
REQUIRE(span.tags.count(tags::http_endpoint) == 1);
CHECK(span.tags.at(tags::http_endpoint) == "/users/{param:int}");
}
SECTION("route present -> endpoint not added") {
collector->chunks.clear();
Tracer tracer{*finalized};
{
auto span = tracer.create_span();
span.set_tag(tags::http_url, "http://example.com/users/12");
span.set_tag(tags::http_route, "/users/:id");
}
REQUIRE(collector->span_count() == 1);
const auto& span = collector->first_span();
REQUIRE(span.tags.count(tags::http_endpoint) == 0);
}
}
SECTION("ALWAYS_CALCULATE -> adds even when http.route is present") {
config.resource_renaming_enabled = {true};
config.resource_renaming_always_simplified_endpoint = {true};
auto finalized = finalize_config(config);
REQUIRE(finalized);
REQUIRE(finalized->resource_renaming_mode ==
HttpEndpointCalculationMode::ALWAYS_CALCULATE);
Tracer tracer{*finalized};
{
auto span = tracer.create_span();
span.set_tag(tags::http_url, "http://example.com/notes/99");
span.set_tag(tags::http_route, "/notes/:id");
}
REQUIRE(collector->span_count() == 1);
const auto& span = collector->first_span();
REQUIRE(span.tags.count(tags::http_endpoint) == 1);
CHECK(span.tags.at(tags::http_endpoint) == "/notes/{param:int}");
}
SECTION("http.url absent -> never adds") {
config.resource_renaming_enabled = {true};
config.resource_renaming_always_simplified_endpoint = {true};
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
{
auto span = tracer.create_span();
// no http.url
}
REQUIRE(collector->span_count() == 1);
const auto& span = collector->first_span();
REQUIRE(span.tags.count(tags::http_endpoint) == 0);
}
SECTION("pre-existing http.endpoint is preserved") {
config.resource_renaming_enabled = {true};
config.resource_renaming_always_simplified_endpoint = {true};
auto finalized = finalize_config(config);
REQUIRE(finalized);
Tracer tracer{*finalized};
{
auto span = tracer.create_span();
span.set_tag(tags::http_url, "http://example.com/widgets/123");
span.set_tag(tags::http_endpoint, "/pre/set");
}
REQUIRE(collector->span_count() == 1);
const auto& span = collector->first_span();
REQUIRE(span.tags.count(tags::http_endpoint) == 1);
CHECK(span.tags.at(tags::http_endpoint) == "/pre/set");
}
}