-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_span.cpp
More file actions
1081 lines (926 loc) · 34.9 KB
/
test_span.cpp
File metadata and controls
1081 lines (926 loc) · 34.9 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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// These are tests for `Span`. `Span` is a container for labels associated with
// an extent in time. `Span` is also responsible for injecting trace context
// for propagation.
#include <datadog/clock.h>
#include <datadog/hex.h>
#include <datadog/injection_options.h>
#include <datadog/null_collector.h>
#include <datadog/optional.h>
#include <datadog/span.h>
#include <datadog/span_config.h>
#include <datadog/tag_propagation.h>
#include <datadog/trace_segment.h>
#include <datadog/tracer.h>
#include <chrono>
#include <cstdint>
#include <functional>
#include <string>
#include "catch.hpp"
#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;
using namespace std::chrono_literals;
#define TEST_SPAN(x) TEST_CASE(x, "[span]")
TEST_SPAN("set_tag") {
TracerConfig config;
config.service = "testsvc";
const auto collector = std::make_shared<MockCollector>();
config.collector = collector;
config.logger = std::make_shared<MockLogger>();
auto finalized_config = finalize_config(config);
REQUIRE(finalized_config);
Tracer tracer{*finalized_config};
SECTION("tags end up in the collector") {
{
auto span = tracer.create_span();
span.set_tag("foo", "lemon");
span.set_tag("foo.bar", "mint");
span.set_tag("foo.baz", "blueberry");
span.set_tag("_dd.secret.sauce", "thousand islands");
span.set_tag("_dd_not_internal", "");
span.set_tag("_dd.chipmunk", "");
}
REQUIRE(collector->chunks.size() == 1);
const auto& chunk = collector->chunks.front();
REQUIRE(chunk.size() == 1);
const auto& span_ptr = chunk.front();
REQUIRE(span_ptr);
const auto& span = *span_ptr;
REQUIRE(span.tags.at("foo") == "lemon");
REQUIRE(span.tags.at("foo.bar") == "mint");
REQUIRE(span.tags.at("foo.baz") == "blueberry");
REQUIRE(span.tags.at("_dd.secret.sauce") == "thousand islands");
REQUIRE(span.tags.at("_dd_not_internal") == "");
REQUIRE(span.tags.at("_dd.chipmunk") == "");
}
SECTION("tags can be overwritten") {
{
SpanConfig span_config;
span_config.tags = {{"color", "purple"},
{"turtle.depth", "all the way down"},
{"_dd.tag", "written"}};
auto span = tracer.create_span(span_config);
span.set_tag("color", "green");
span.set_tag("bonus", "applied");
span.set_tag("_dd.tag", "overwritten");
}
REQUIRE(collector->chunks.size() == 1);
const auto& chunk = collector->chunks.front();
REQUIRE(chunk.size() == 1);
const auto& span_ptr = chunk.front();
REQUIRE(span_ptr);
const auto& span = *span_ptr;
REQUIRE(span.tags.at("color") == "green");
REQUIRE(span.tags.at("turtle.depth") == "all the way down");
REQUIRE(span.tags.at("bonus") == "applied");
REQUIRE(span.tags.at("_dd.tag") == "overwritten");
}
}
TEST_SPAN("lookup_tag") {
TracerConfig config;
config.service = "testsvc";
config.collector = std::make_shared<MockCollector>();
config.logger = std::make_shared<MockLogger>();
auto finalized_config = finalize_config(config);
REQUIRE(finalized_config);
Tracer tracer{*finalized_config};
SECTION("not found is null") {
auto span = tracer.create_span();
REQUIRE(!span.lookup_tag("nope"));
REQUIRE(!span.lookup_tag("also nope"));
REQUIRE(!span.lookup_tag("_dd.nope"));
}
SECTION("lookup after set") {
auto span = tracer.create_span();
span.set_tag("color", "purple");
span.set_tag("turtle.depth", "all the way down");
span.set_tag("_dd.tag", "found");
REQUIRE(span.lookup_tag("color") == "purple");
REQUIRE(span.lookup_tag("turtle.depth") == "all the way down");
REQUIRE(span.lookup_tag("_dd.tag") == "found");
}
SECTION("lookup after config") {
SpanConfig span_config;
span_config.tags = {
{"color", "purple"},
{"turtle.depth", "all the way down"},
{"_dd.tag", "found"},
};
auto span = tracer.create_span(span_config);
REQUIRE(span.lookup_tag("color") == "purple");
REQUIRE(span.lookup_tag("turtle.depth") == "all the way down");
REQUIRE(span.lookup_tag("_dd.tag") == "found");
}
}
TEST_SPAN("remove_tag") {
TracerConfig config;
config.service = "testsvc";
config.collector = std::make_shared<MockCollector>();
config.logger = std::make_shared<MockLogger>();
auto finalized_config = finalize_config(config);
REQUIRE(finalized_config);
Tracer tracer{*finalized_config};
SECTION("doesn't have to be there already") {
auto span = tracer.create_span();
span.remove_tag("not even there");
span.remove_tag("_dd.tag");
}
SECTION("after removal, lookup yields null") {
SpanConfig span_config;
span_config.tags = {{"mayfly", "carpe diem"}, {"_dd.mayfly", "carpe diem"}};
auto span = tracer.create_span(span_config);
span.set_tag("foo", "bar");
span.remove_tag("mayfly");
span.remove_tag("_dd.mayfly");
span.remove_tag("foo");
REQUIRE(!span.lookup_tag("mayfly"));
REQUIRE(!span.lookup_tag("_dd.mayfly"));
REQUIRE(!span.lookup_tag("foo"));
}
}
TEST_SPAN("set_metric") {
TracerConfig config;
config.service = "testsvc";
const auto collector = std::make_shared<MockCollector>();
config.collector = collector;
config.logger = std::make_shared<MockLogger>();
auto finalized_config = finalize_config(config);
REQUIRE(finalized_config);
Tracer tracer{*finalized_config};
SECTION("metrics end up in the collector") {
{
auto span = tracer.create_span();
span.set_metric("foo", 5.0);
span.set_metric("foo.bar", 3.0);
span.set_metric("foo.baz", 1.0);
span.set_metric("_dd.secret.sauce", 2.0);
span.set_metric("_dd_not_internal", 3.0);
span.set_metric("_dd.chipmunk", 4.0);
}
REQUIRE(collector->chunks.size() == 1);
const auto& chunk = collector->chunks.front();
REQUIRE(chunk.size() == 1);
const auto& span_ptr = chunk.front();
REQUIRE(span_ptr);
const auto& span = *span_ptr;
REQUIRE(span.numeric_tags.at("foo") == 5.0);
REQUIRE(span.numeric_tags.at("foo.bar") == 3.0);
REQUIRE(span.numeric_tags.at("foo.baz") == 1.0);
REQUIRE(span.numeric_tags.at("_dd.secret.sauce") == 2.0);
REQUIRE(span.numeric_tags.at("_dd_not_internal") == 3.0);
REQUIRE(span.numeric_tags.at("_dd.chipmunk") == 4.0);
}
SECTION("metrics can be overwritten") {
{
auto span = tracer.create_span();
span.set_metric("color", 2.0);
span.set_metric("color", 1.0);
span.set_metric("bonus", 6.0);
span.set_metric("bonus", 5.0);
}
REQUIRE(collector->chunks.size() == 1);
const auto& chunk = collector->chunks.front();
REQUIRE(chunk.size() == 1);
const auto& span_ptr = chunk.front();
REQUIRE(span_ptr);
const auto& span = *span_ptr;
REQUIRE(span.numeric_tags.at("color") == 1.0);
REQUIRE(span.numeric_tags.at("bonus") == 5.0);
}
}
TEST_SPAN("lookup_metric") {
TracerConfig config;
config.service = "testsvc";
config.collector = std::make_shared<MockCollector>();
config.logger = std::make_shared<MockLogger>();
auto finalized_config = finalize_config(config);
REQUIRE(finalized_config);
Tracer tracer{*finalized_config};
SECTION("not found is null") {
auto span = tracer.create_span();
REQUIRE(!span.lookup_metric("nope"));
REQUIRE(!span.lookup_metric("also nope"));
REQUIRE(!span.lookup_metric("_dd.nope"));
}
SECTION("lookup after set") {
auto span = tracer.create_span();
span.set_metric("color", 11.0);
span.set_metric("turtle.depth", 6.0);
span.set_metric("_dd.this", 33.0);
REQUIRE(span.lookup_metric("color") == 11.0);
REQUIRE(span.lookup_metric("turtle.depth") == 6.0);
REQUIRE(span.lookup_metric("_dd.this") == 33.0);
}
}
TEST_SPAN("remove_metric") {
TracerConfig config;
config.service = "testsvc";
config.collector = std::make_shared<MockCollector>();
config.logger = std::make_shared<MockLogger>();
auto finalized_config = finalize_config(config);
REQUIRE(finalized_config);
Tracer tracer{*finalized_config};
SECTION("doesn't have to be there already") {
auto span = tracer.create_span();
span.remove_metric("not even there");
}
SECTION("after removal, lookup yields null") {
auto span = tracer.create_span();
span.set_metric("mayfly", 10.0);
span.set_metric("foo", 11.0);
span.set_metric("_dd.metric", 1.0);
span.remove_metric("mayfly");
span.remove_metric("foo");
span.remove_metric("_dd.metric");
REQUIRE(!span.lookup_metric("mayfly"));
REQUIRE(!span.lookup_metric("foo"));
REQUIRE(!span.lookup_metric("_dd.metric"));
}
}
TEST_SPAN("span duration") {
TracerConfig config;
config.service = "testsvc";
auto collector = std::make_shared<MockCollector>();
config.collector = collector;
config.logger = std::make_shared<MockLogger>();
auto finalized_config = finalize_config(config);
REQUIRE(finalized_config);
Tracer tracer{*finalized_config};
SECTION("start time is adjustable") {
{
SpanConfig span_config;
span_config.start = default_clock() - std::chrono::seconds(3);
auto span = tracer.create_span(span_config);
(void)span;
}
REQUIRE(collector->chunks.size() == 1);
const auto& chunk = collector->chunks.front();
REQUIRE(chunk.size() == 1);
const auto& span_ptr = chunk.front();
REQUIRE(span_ptr);
const auto& span = *span_ptr;
REQUIRE(span.duration >= std::chrono::seconds(3));
}
SECTION("end time is adjustable") {
{
auto span = tracer.create_span();
span.set_end_time(span.start_time().tick + std::chrono::seconds(2));
}
REQUIRE(collector->chunks.size() == 1);
const auto& chunk = collector->chunks.front();
REQUIRE(chunk.size() == 1);
const auto& span_ptr = chunk.front();
REQUIRE(span_ptr);
const auto& span = *span_ptr;
REQUIRE(span.duration == std::chrono::seconds(2));
}
}
TEST_SPAN(".error() and .set_error*()") {
struct TestCase {
std::string name;
std::function<void(Span&)> mutate;
bool expected_error;
Optional<StringView> expected_error_message;
Optional<StringView> expected_error_type;
Optional<StringView> expected_error_stack;
};
auto test_case = GENERATE(values<TestCase>(
{{"No error → no error.", [](Span&) {}, false, nullopt, nullopt, nullopt},
{"set_error(true) → error", [](Span& span) { span.set_error(true); },
true, nullopt, nullopt, nullopt},
{"set_error_message → error and error message",
[](Span& span) { span.set_error_message("oops!"); }, true, "oops!",
nullopt, nullopt},
{"set_error_type → error and error type",
[](Span& span) { span.set_error_type("errno"); }, true, nullopt,
"errno", nullopt},
{"set_error_stack → error and error stack",
[](Span& span) { span.set_error_stack("this is C++, fool"); }, true,
nullopt, nullopt, "this is C++, fool"},
{"set all of them → error, error message, error type, and error stack",
[](Span& span) {
span.set_error_message("oops!");
span.set_error_type("errno");
span.set_error_stack("this is C++, fool");
},
true, "oops!", "errno", "this is C++, fool"},
{"set_error(false) → no error, no error tags, and no error stack",
[](Span& span) {
span.set_error_message("this will go away");
span.set_error_type("as will this");
span.set_error_stack("this too");
span.set_error(false);
},
false, nullopt, nullopt, nullopt}}));
TracerConfig config;
config.service = "testsvc";
auto collector = std::make_shared<MockCollector>();
config.collector = collector;
config.logger = std::make_shared<MockLogger>();
auto finalized_config = finalize_config(config);
REQUIRE(finalized_config);
Tracer tracer{*finalized_config};
CAPTURE(test_case.name);
{
auto span = tracer.create_span();
test_case.mutate(span);
REQUIRE(span.error() == test_case.expected_error);
}
REQUIRE(collector->chunks.size() == 1);
const auto& chunk = collector->chunks.front();
REQUIRE(chunk.size() == 1);
const auto& span_ptr = chunk.front();
REQUIRE(span_ptr);
const auto& span = *span_ptr;
auto found = span.tags.find("error.message");
if (test_case.expected_error_message) {
REQUIRE(found != span.tags.end());
REQUIRE(found->second == *test_case.expected_error_message);
} else {
REQUIRE(found == span.tags.end());
}
found = span.tags.find("error.type");
if (test_case.expected_error_type) {
REQUIRE(found != span.tags.end());
REQUIRE(found->second == *test_case.expected_error_type);
} else {
REQUIRE(found == span.tags.end());
}
}
TEST_SPAN("property setters and getters") {
// Verify that modifications made by `Span::set_...` are visible both in the
// corresponding getter method and in the resulting span data sent to the
// collector.
TracerConfig config;
config.service = "testsvc";
auto collector = std::make_shared<MockCollector>();
config.collector = collector;
config.logger = std::make_shared<MockLogger>();
auto finalized_config = finalize_config(config);
REQUIRE(finalized_config);
Tracer tracer{*finalized_config};
SECTION("set_service_name") {
{
auto span = tracer.create_span();
span.set_service_name("wobble");
REQUIRE(span.service_name() == "wobble");
}
auto& span = collector->first_span();
REQUIRE(span.service == "wobble");
}
SECTION("set_service_type") {
{
auto span = tracer.create_span();
span.set_service_type("wobble");
REQUIRE(span.service_type() == "wobble");
}
auto& span = collector->first_span();
REQUIRE(span.service_type == "wobble");
}
SECTION("set_name") {
{
auto span = tracer.create_span();
span.set_name("wobble");
REQUIRE(span.name() == "wobble");
}
auto& span = collector->first_span();
REQUIRE(span.name == "wobble");
}
SECTION("set_resource_name") {
{
auto span = tracer.create_span();
span.set_resource_name("wobble");
REQUIRE(span.resource_name() == "wobble");
}
auto& span = collector->first_span();
REQUIRE(span.resource == "wobble");
}
}
// Trace context injection is implemented in `TraceSegment`, but it's part of
// the interface of `Span`, so the test is here.
TEST_SPAN("injection") {
TracerConfig config;
config.service = "testsvc";
config.collector = std::make_shared<MockCollector>();
config.logger = std::make_shared<MockLogger>();
config.injection_styles = {PropagationStyle::DATADOG, PropagationStyle::B3};
auto finalized_config = finalize_config(config);
REQUIRE(finalized_config);
// Override the tracer's ID generator to always return a fixed value.
struct Generator : public IDGenerator {
const std::uint64_t id;
explicit Generator(std::uint64_t id) : id(id) {}
TraceID trace_id(const TimePoint&) const override { return TraceID(id); }
std::uint64_t span_id() const override { return id; }
};
Tracer tracer{*finalized_config, std::make_shared<Generator>(42)};
SECTION("APM Disabled cancel context propagation") {
config.tracing_enabled = false;
auto finalized_disable_config = finalize_config(config);
REQUIRE(finalized_disable_config);
Tracer apm_disabled_tracer{*finalized_disable_config};
auto apm_span = tracer.create_span();
MockDictWriter writer;
apm_span.inject(writer);
REQUIRE(writer.items.empty() == false);
// Consume the span that MUST be kept for service liveness.
{ apm_disabled_tracer.create_span(); }
auto span = apm_disabled_tracer.create_span();
// reuse the same writer. Since span generated from apm_disabled_tracer is
// not marked by a product injection should be cancelled.
span.inject(writer);
CHECK(writer.items.empty());
}
SECTION("trace ID, parent ID ,and sampling priority") {
auto span = tracer.create_span();
REQUIRE(span.trace_id() == 42);
REQUIRE(span.id() == 42);
const int priority = 3; // 😱
span.trace_segment().override_sampling_priority(priority);
MockDictWriter writer;
span.inject(writer);
const auto& headers = writer.items;
REQUIRE(headers.at("x-datadog-trace-id") == "42");
REQUIRE(headers.at("x-datadog-parent-id") == "42");
REQUIRE(headers.at("x-datadog-sampling-priority") == "3");
REQUIRE(headers.at("x-b3-traceid") == "000000000000002a");
REQUIRE(headers.at("x-b3-spanid") == "000000000000002a");
REQUIRE(headers.at("x-b3-sampled") == "1");
}
SECTION("origin and trace tags") {
SECTION("empty trace tags") {
const std::unordered_map<std::string, std::string> headers{
{"x-datadog-trace-id", "123"},
{"x-datadog-sampling-priority", "0"},
{"x-datadog-origin", "Egypt"},
{"x-datadog-tags", ""}};
MockDictReader reader{headers};
auto span = tracer.extract_span(reader);
REQUIRE(span);
MockDictWriter writer;
span->inject(writer);
REQUIRE(writer.items.at("x-datadog-origin") == "Egypt");
// empty trace tags → x-datadog-tags is not set
REQUIRE(writer.items.count("x-datadog-tags") == 0);
}
SECTION("lots of trace tags") {
const std::string trace_tags =
"foo=bar,34=43,54-46=my-number,_dd.p.not_excluded=foo";
const std::unordered_map<std::string, std::string> headers{
{"x-datadog-trace-id", "123"},
{"x-datadog-sampling-priority", "0"},
{"x-datadog-origin", "Egypt"},
{"x-datadog-tags", trace_tags}};
MockDictReader reader{headers};
auto span = tracer.extract_span(reader);
REQUIRE(span);
MockDictWriter writer;
span->inject(writer);
REQUIRE(writer.items.at("x-datadog-origin") == "Egypt");
REQUIRE(writer.items.count("x-datadog-tags") == 1);
const auto output = decode_tags(writer.items.at("x-datadog-tags"));
const auto input = decode_tags(trace_tags);
REQUIRE(output);
REQUIRE(input);
// Trace tags that don't begin with "_dd.p." are excluded from the parsed
// trace tags, so check only that the output is a subset of the input.
REQUIRE_THAT(*input, ContainsSubset(*output));
}
}
SECTION("trace source is not set (default)") {
auto span = tracer.create_span();
MockDictWriter writer;
span.inject(writer);
const auto& headers = writer.items;
REQUIRE(headers.count("x-datadog-tags") > 0);
// When there is no trace source, there should be no `_dd.p.ts` tag.
const auto decoded_tags = decode_tags(headers.at("x-datadog-tags"));
REQUIRE(decoded_tags);
auto found = std::find_if(
decoded_tags->begin(), decoded_tags->end(), [](const auto& tag) {
return tag.first == tags::internal::trace_source;
});
CHECK(found == decoded_tags->end());
}
SECTION("trace source is propagated") {
auto span = tracer.create_span();
span.set_source(Source::database_monitoring);
MockDictWriter writer;
span.inject(writer);
const auto& headers = writer.items;
REQUIRE(headers.count("x-datadog-tags") > 0);
const auto decoded_tags = decode_tags(headers.at("x-datadog-tags"));
REQUIRE(decoded_tags);
auto found = std::find_if(
decoded_tags->begin(), decoded_tags->end(), [](const auto& tag) {
return tag.first == tags::internal::trace_source;
});
REQUIRE(found != decoded_tags->cend());
CHECK(found->second == to_tag(Source::database_monitoring));
}
}
TEST_SPAN("injection can be disabled using the \"none\" style") {
TracerConfig config;
config.service = "testsvc";
config.name = "spanny";
config.collector = std::make_shared<MockCollector>();
config.logger = std::make_shared<MockLogger>();
config.injection_styles = {PropagationStyle::NONE};
const auto finalized_config = finalize_config(config);
REQUIRE(finalized_config);
Tracer tracer{*finalized_config};
const auto span = tracer.create_span();
MockDictWriter writer;
span.inject(writer);
const std::unordered_map<std::string, std::string> empty;
REQUIRE(writer.items == empty);
}
TEST_SPAN("injecting W3C traceparent header") {
TracerConfig config;
config.service = "testsvc";
config.collector = std::make_shared<NullCollector>();
config.logger = std::make_shared<NullLogger>();
config.injection_styles = {PropagationStyle::W3C};
SECTION("extracted from W3C traceparent") {
config.extraction_styles = {PropagationStyle::W3C};
const auto finalized_config = finalize_config(config);
REQUIRE(finalized_config);
// Override the tracer's ID generator to always return `expected_parent_id`.
constexpr std::uint64_t expected_parent_id = 0xcafebabe;
struct Generator : public IDGenerator {
const std::uint64_t id;
explicit Generator(std::uint64_t id) : id(id) {}
TraceID trace_id(const TimePoint&) const override { return TraceID(id); }
std::uint64_t span_id() const override { return id; }
};
Tracer tracer{*finalized_config,
std::make_shared<Generator>(expected_parent_id)};
const std::unordered_map<std::string, std::string> input_headers{
// https://www.w3.org/TR/trace-context/#examples-of-http-traceparent-headers
{"traceparent",
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"},
};
const MockDictReader reader{input_headers};
const auto maybe_span = tracer.extract_span(reader);
REQUIRE(maybe_span);
const auto& span = *maybe_span;
REQUIRE(span.id() == expected_parent_id);
MockDictWriter writer;
span.inject(writer);
const auto& output_headers = writer.items;
const auto found = output_headers.find("traceparent");
REQUIRE(found != output_headers.end());
// The "00000000cafebabe" is the zero-padded `expected_parent_id`.
const StringView expected =
"00-4bf92f3577b34da6a3ce929d0e0e4736-00000000cafebabe-01";
REQUIRE(found->second == expected);
}
SECTION("not extracted from W3C traceparent") {
const auto finalized_config = finalize_config(config);
REQUIRE(finalized_config);
// Override the tracer's ID generator to always return a fixed value.
constexpr std::uint64_t expected_id = 0xcafebabe;
struct Generator : public IDGenerator {
const std::uint64_t id;
explicit Generator(std::uint64_t id) : id(id) {}
TraceID trace_id(const TimePoint&) const override { return TraceID(id); }
std::uint64_t span_id() const override { return id; }
};
Tracer tracer{*finalized_config, std::make_shared<Generator>(expected_id)};
auto span = tracer.create_span();
// Let's test the effect sampling priority plays on the resulting
// traceparent, too.
struct TestCase {
int sampling_priority;
std::string expected_flags;
};
auto test_case = GENERATE(
values<TestCase>({{-1, "00"}, {0, "00"}, {1, "01"}, {2, "01"}}));
CAPTURE(test_case.sampling_priority);
CAPTURE(test_case.expected_flags);
span.trace_segment().override_sampling_priority(
test_case.sampling_priority);
MockDictWriter writer;
span.inject(writer);
const auto& output_headers = writer.items;
const auto found = output_headers.find("traceparent");
REQUIRE(found != output_headers.end());
// The "cafebabe"s come from `expected_id`.
const std::string expected =
"00-000000000000000000000000cafebabe-00000000cafebabe-" +
test_case.expected_flags;
REQUIRE(found->second == expected);
}
}
TEST_SPAN("injecting W3C tracestate header") {
// Concerns:
// - the basics:
// - sampling priority
// - origin
// - trace tags
// - parent id
// - extra fields (extracted from W3C)
// - all of the above
// - character substitutions:
// - in origin
// - in trace tag key
// - in trace tag value
// - special tilde ("~") behavior
// - length limit:
// - at origin
// - at a trace tag
// - at the extra fields (extracted from W3C)
TracerConfig config;
config.service = "testsvc";
// The order of the extraction styles doesn't matter for this test, because
// it'll either be one or the other in the test cases.
config.extraction_styles = {PropagationStyle::DATADOG, PropagationStyle::W3C};
config.injection_styles = {PropagationStyle::W3C};
// If one of these test cases results in a local sampling decision, let it be
// "drop."
config.trace_sampler.sample_rate = 0.0;
const auto logger = std::make_shared<MockLogger>();
config.logger = logger;
config.collector = std::make_shared<NullCollector>();
const auto finalized_config = finalize_config(config);
REQUIRE(finalized_config);
Tracer tracer{*finalized_config};
struct TestCase {
int line;
std::string name;
std::unordered_map<std::string, std::string> input_headers;
std::string expected_tracestate;
};
static const auto traceparent_drop =
"00-00000000000000000000000000000001-0000000000000001-00";
auto test_case = GENERATE(values<TestCase>({
{__LINE__,
"sampling priority",
{
{"x-datadog-trace-id", "1"},
{"x-datadog-parent-id", "1"},
{"x-datadog-sampling-priority", "2"},
},
"dd=s:2;p:$parent_id"},
{__LINE__,
"origin",
{
{"x-datadog-trace-id", "1"},
{"x-datadog-parent-id", "1"},
{"x-datadog-origin", "France"},
},
// The "s:-1" and "t.ksr:0" comes from the 0% sample rate.
"dd=s:-1;p:$parent_id;o:France;t.ksr:0"},
{__LINE__,
"trace tags",
{
{"x-datadog-trace-id", "1"},
{"x-datadog-parent-id", "1"},
{"x-datadog-tags", "_dd.p.foo=x,_dd.p.bar=y,ignored=wrong_prefix"},
},
// The "s:-1" and "t.ksr:0" comes from the 0% sample rate.
"dd=s:-1;p:$parent_id;t.foo:x;t.bar:y;t.ksr:0"},
{__LINE__,
"extra fields",
{
{"traceparent", traceparent_drop},
{"tracestate", "dd=foo:bar;boing:boing"},
},
// The "s:0" comes from the sampling decision in `traceparent_drop`.
"dd=s:0;p:$parent_id;foo:bar;boing:boing"},
{__LINE__,
"all of the above",
{
{"traceparent", traceparent_drop},
{"tracestate", "dd=o:France;t.foo:x;t.bar:y;foo:bar;boing:boing"},
},
// The "s:0" comes from the sampling decision in `traceparent_drop`.
"dd=s:0;p:$parent_id;o:France;t.foo:x;t.bar:y;foo:bar;boing:boing"},
{
__LINE__,
"replace invalid characters in origin",
{
{"x-datadog-trace-id", "1"},
{"x-datadog-parent-id", "1"},
{"x-datadog-origin", "France, is a country=nation; so is 台北."},
},
// The "s:-1" comes from the 0% sample rate.
"dd=s:-1;p:$parent_id;o:France_ is a country~nation_ so is "
"______.;t.ksr:0",
},
{__LINE__,
"replace invalid characters in trace tag key",
{
{"x-datadog-trace-id", "1"},
{"x-datadog-parent-id", "1"},
{"x-datadog-tags", "_dd.p.a;d台北x =foo,_dd.p.ok=bar"},
},
// The "s:-1" comes from the 0% sample rate.
"dd=s:-1;p:$parent_id;t.a_d______x_:foo;t.ok:bar;t.ksr:0"},
{__LINE__,
"replace invalid characters in trace tag value",
{
{"x-datadog-trace-id", "1"},
{"x-datadog-parent-id", "1"},
{"x-datadog-tags", "_dd.p.wacky=hello fr~d; how are คุณ?"},
},
// The "s:-1" comes from the 0% sample rate.
"dd=s:-1;p:$parent_id;t.wacky:hello fr_d_ how are "
"_________?;t.ksr:0"},
{__LINE__,
"replace equal signs with tildes in trace tag value",
{
{"x-datadog-trace-id", "1"},
{"x-datadog-parent-id", "1"},
{"x-datadog-tags", "_dd.p.base64_thingy=d2Fra2EhIHdhaw=="},
},
// The "s:-1" comes from the 0% sample rate.
"dd=s:-1;p:$parent_id;t.base64_thingy:d2Fra2EhIHdhaw~~;t.ksr:0"},
{__LINE__,
"oversized origin truncates it and subsequent fields",
{
{"x-datadog-trace-id", "1"},
{"x-datadog-parent-id", "1"},
{"x-datadog-origin",
"long cat is "
"looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
"ooo"
"oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
"ooo"
"oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
"ooo"
"ooooooooooooooooooooooooooooooooong"},
{"x-datadog-tags", "_dd.p.foo=bar,_dd.p.honk=honk"},
},
// The "s:-1" comes from the 0% sample rate.
"dd=s:-1;p:$parent_id"},
{__LINE__,
"oversized trace tag truncates it and subsequent fields",
{
{"x-datadog-trace-id", "1"},
{"x-datadog-parent-id", "1"},
{"x-datadog-tags",
"_dd.p.foo=bar,_dd.p.long_cat_is="
"looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
"ooo"
"oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
"ooo"
"oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
"ooo"
"ooooooooooooooooooong,_dd.p.lost=forever"},
},
// The "s:-1" comes from the 0% sample rate.
"dd=s:-1;p:$parent_id;t.foo:bar"},
{__LINE__,
"oversized extra field truncates itself and subsequent fields",
{
{"traceparent", traceparent_drop},
{"tracestate",
"dd=foo:bar;long_cat_is:"
"looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
"ooo"
"oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
"ooo"
"oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
"ooo"
"ooooooooooooooooooooooooooooooooong;lost:forever"},
},
// The "s:0" comes from the sampling decision in `traceparent_drop`.
"dd=s:0;p:$parent_id;foo:bar"},
{__LINE__,
"non-Datadog tracestate",
{
{"traceparent", traceparent_drop},
{"tracestate", "foo=bar,boing=boing"},
},
// The "s:0" comes from the sampling decision in `traceparent_drop`.
"dd=s:0;p:$parent_id,foo=bar,boing=boing"},
}));
CAPTURE(test_case.name);
CAPTURE(test_case.line);
CAPTURE(test_case.input_headers);
CAPTURE(test_case.expected_tracestate);
CAPTURE(logger->entries);
MockDictReader reader{test_case.input_headers};
const auto span = tracer.extract_span(reader);
REQUIRE(span);
MockDictWriter writer;
span->inject(writer);
CAPTURE(writer.items);
const auto found = writer.items.find("tracestate");
REQUIRE(found != writer.items.end());
test_case.expected_tracestate.replace(
test_case.expected_tracestate.find("$parent_id"),
sizeof("$parent_id") - 1, hex_padded(span->id()));
REQUIRE(found->second == test_case.expected_tracestate);
REQUIRE(logger->error_count() == 0);
}
TEST_SPAN("128-bit trace ID injection") {
TracerConfig config;
config.service = "testsvc";
config.logger = std::make_shared<MockLogger>();
config.generate_128bit_trace_ids = true;
std::vector<PropagationStyle> injection_styles{
PropagationStyle::W3C, PropagationStyle::DATADOG, PropagationStyle::B3};
config.injection_styles = injection_styles;
const auto finalized = finalize_config(config);
REQUIRE(finalized);
class MockIDGenerator : public IDGenerator {
const TraceID trace_id_;
public:
explicit MockIDGenerator(TraceID trace_id) : trace_id_(trace_id) {}
TraceID trace_id(const TimePoint&) const override { return trace_id_; }
// `span_id` won't be called, because root spans use the lower part of
// `trace_id` for the span ID.
std::uint64_t span_id() const override { return 42; }
};
const TraceID trace_id{0xcafebabecafebabeULL, 0xdeadbeefdeadbeefULL};
Tracer tracer{*finalized, std::make_shared<MockIDGenerator>(trace_id)};
auto span = tracer.create_span();
span.trace_segment().override_sampling_priority(2);
MockDictWriter writer;
span.inject(writer);
// PropagationStyle::DATADOG
auto found = writer.items.find("x-datadog-trace-id");
REQUIRE(found != writer.items.end());
REQUIRE(found->second == std::to_string(trace_id.low));
found = writer.items.find("x-datadog-tags");
REQUIRE(found != writer.items.end());
REQUIRE(found->second.find("_dd.p.tid=deadbeefdeadbeef") !=
std::string::npos);
// PropagationStyle::W3C
found = writer.items.find("traceparent");
REQUIRE(found != writer.items.end());
REQUIRE(found->second ==
"00-deadbeefdeadbeefcafebabecafebabe-cafebabecafebabe-01");
// PropagationStyle::B3
found = writer.items.find("x-b3-traceid");
REQUIRE(found != writer.items.end());