-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_trace_sampling.py
More file actions
656 lines (587 loc) · 28.1 KB
/
test_trace_sampling.py
File metadata and controls
656 lines (587 loc) · 28.1 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
import json
import pytest
import random
from utils.docker_fixtures.spec.trace import find_only_span, find_span_in_traces
from utils.docker_fixtures.spec.trace import SAMPLING_PRIORITY_KEY, SAMPLING_RULE_PRIORITY_RATE, ORIGIN
from utils.docker_fixtures.spec.trace import MANUAL_KEEP_KEY
from utils import rfc, scenarios, features
from utils.docker_fixtures import TestAgentAPI
from .conftest import APMLibrary
@features.trace_sampling
@features.adaptive_sampling
@scenarios.parametric
@rfc("https://docs.google.com/document/d/1HRbi1DrBjL_KGeONrPgH7lblgqSLGlV5Ox1p4RL97xM/")
class Test_Trace_Sampling_Basic:
@pytest.mark.parametrize(
"library_env",
[
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[
{"service": "webserver.non-matching", "sample_rate": 0},
{"service": "webserver", "sample_rate": 1},
]
),
},
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[{"name": "web.request.non-matching", "sample_rate": 0}, {"name": "web.request", "sample_rate": 1}]
),
},
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[
{"service": "webserver.non-matching", "name": "web.request", "sample_rate": 0},
{"service": "webserver", "name": "web.request.non-matching", "sample_rate": 0},
{"service": "webserver", "name": "web.request", "sample_rate": 1},
]
),
},
],
)
def test_trace_sampled_by_trace_sampling_rule_exact_match(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Test that a trace is sampled by the exact matching trace sampling rule"""
with test_library, test_library.dd_start_span(name="web.request", service="webserver"):
pass
span = find_only_span(test_agent.wait_for_num_traces(1))
assert span["metrics"].get(SAMPLING_PRIORITY_KEY) == 2
assert span["metrics"].get(SAMPLING_RULE_PRIORITY_RATE) == 1.0
@pytest.mark.parametrize(
"library_env",
[
{
"DD_TRACE_SAMPLE_RATE": 1,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[{"service": "webserver", "name": "web.request", "sample_rate": 0}]
),
"DD_TRACE_STATS_COMPUTATION_ENABLED": "false",
}
],
)
def test_trace_dropped_by_trace_sampling_rule(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Test that a trace is dropped by the matching defined trace sampling rule"""
with test_library, test_library.dd_start_span(name="web.request", service="webserver"):
pass
span = find_only_span(test_agent.wait_for_num_traces(1))
assert span["metrics"].get(SAMPLING_PRIORITY_KEY) == -1
assert span["metrics"].get(SAMPLING_RULE_PRIORITY_RATE) == 0.0
@pytest.mark.parametrize(
"library_env",
[
{
"DD_TRACE_SAMPLE_RATE": 1,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[{"service": "webserver", "resource": "drop-me", "sample_rate": 0}, {"sample_rate": 1}]
),
}
],
)
def test_trace_kept_in_spite_trace_sampling_rule(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Test that a trace is being kept with manual.keep despite of the matching defined trace sampling rule"""
with test_library, test_library.dd_start_span(name="web.request", service="webserver") as s1:
s1.set_meta(MANUAL_KEEP_KEY, "1")
s1.set_meta("resource.name", "drop-me")
span = find_only_span(test_agent.wait_for_num_traces(1))
assert span["metrics"].get(SAMPLING_PRIORITY_KEY) == 2
@features.trace_sampling
@features.adaptive_sampling
@scenarios.parametric
@rfc("https://docs.google.com/document/d/1S9pufnJjrsxH6pRbpigdYFwA5JjSdZ6iLZ-9E7PoAic/")
class Test_Trace_Sampling_Globs:
@pytest.mark.parametrize(
"library_env",
[
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[{"service": "web.non-matching*", "sample_rate": 0}, {"service": "web*", "sample_rate": 1}]
),
},
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[{"name": "web.non-matching*", "sample_rate": 0}, {"name": "web.*", "sample_rate": 1}]
),
},
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[
{"service": "webserv?r.non-matching", "name": "web.req*", "sample_rate": 0},
{"service": "webserv?r", "name": "web.req*.non-matching", "sample_rate": 0},
{"service": "webserv?r", "name": "web.req*", "sample_rate": 1},
]
),
},
],
)
def test_trace_sampled_by_trace_sampling_rule_glob_match(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Test that a trace is sampled by the glob matching trace sampling rule"""
with test_library, test_library.dd_start_span(name="web.request", service="webserver"):
pass
span = find_only_span(test_agent.wait_for_num_traces(1))
assert span["metrics"].get(SAMPLING_PRIORITY_KEY) == 2
assert span["metrics"].get(SAMPLING_RULE_PRIORITY_RATE) == 1.0
@pytest.mark.parametrize(
"library_env",
[
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[
{"name": "wEb.rEquEst", "sample_rate": 1},
]
),
},
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps([{"service": "wEbSerVer", "sample_rate": 1}]),
},
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps([{"resource": "/rAnDom", "sample_rate": 1}]),
},
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps([{"tags": {"key": "vAlUe"}, "sample_rate": 1}]),
},
],
)
def test_field_case_insensitivity(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Tests that sampling rule field values are case insensitive"""
with (
test_library,
test_library.dd_start_span(
name="web.request", service="webserver", resource="/random", tags=[("key", "value")]
),
):
pass
span = find_only_span(test_agent.wait_for_num_traces(1))
assert span["metrics"].get(SAMPLING_PRIORITY_KEY) == 2
assert span["metrics"].get(SAMPLING_RULE_PRIORITY_RATE) == 1.0
@pytest.mark.parametrize(
"library_env",
[
{
"DD_TRACE_SAMPLE_RATE": 1,
"DD_TRACE_SAMPLING_RULES": json.dumps([{"service": "w?bs?rv?r", "name": "web.*", "sample_rate": 0}]),
"DD_TRACE_STATS_COMPUTATION_ENABLED": "false",
}
],
)
def test_trace_dropped_by_trace_sampling_rule(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Test that a trace is dropped by the matching defined trace sampling rule"""
with test_library, test_library.dd_start_span(name="web.request", service="webserver"):
pass
span = find_only_span(test_agent.wait_for_num_traces(1))
assert span["metrics"].get(SAMPLING_PRIORITY_KEY) == -1
assert span["metrics"].get(SAMPLING_RULE_PRIORITY_RATE) == 0.0
@features.trace_sampling
@features.adaptive_sampling
@scenarios.parametric
@rfc("https://docs.google.com/document/d/1S9pufnJjrsxH6pRbpigdYFwA5JjSdZ6iLZ-9E7PoAic/")
class Test_Trace_Sampling_Globs_Feb2024_Revision:
@pytest.mark.parametrize(
"library_env",
[
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[{"service": "web.non-matching*", "sample_rate": 0}, {"service": "web*", "sample_rate": 1}]
),
},
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[{"name": "web.non-matching*", "sample_rate": 0}, {"name": "wEb.*", "sample_rate": 1}]
),
},
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[
{"service": "webserv?r.non-matching", "name": "wEb.req*", "sample_rate": 0},
{"service": "webserv?r", "name": "web.req*.non-matching", "sample_rate": 0},
{"service": "webserv?r", "name": "web.req*", "sample_rate": 1},
]
),
},
],
)
def test_trace_sampled_by_trace_sampling_rule_insensitive_glob_match(
self, test_agent: TestAgentAPI, test_library: APMLibrary
):
"""Test that a trace is sampled by the glob matching trace sampling rule"""
with test_library, test_library.dd_start_span(name="web.request", service="webserver"):
pass
span = find_only_span(test_agent.wait_for_num_traces(1))
assert span["metrics"].get(SAMPLING_PRIORITY_KEY) == 2
assert span["metrics"].get(SAMPLING_RULE_PRIORITY_RATE) == 1.0
@features.trace_sampling
@features.adaptive_sampling
@scenarios.parametric
@rfc("https://docs.google.com/document/d/1S9pufnJjrsxH6pRbpigdYFwA5JjSdZ6iLZ-9E7PoAic/")
class Test_Trace_Sampling_Resource:
@pytest.mark.parametrize(
"library_env",
[
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[{"resource": "/bar.non-matching", "sample_rate": 0}, {"resource": "/?ar", "sample_rate": 1}]
),
},
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[
{"name": "web.request.non-matching", "resource": "/bar", "sample_rate": 0},
{"name": "web.request", "resource": "/bar.non-matching", "sample_rate": 0},
{"name": "web.request", "resource": "/b*", "sample_rate": 1},
]
),
},
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[
{"service": "webserver.non-matching", "resource": "/bar", "sample_rate": 0},
{"service": "webserver", "resource": "/bar.non-matching", "sample_rate": 0},
{"service": "webserver", "resource": "/bar", "sample_rate": 1},
]
),
},
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[
{
"service": "webserver.non-matching",
"name": "web.request",
"resource": "/bar",
"sample_rate": 0,
},
{
"service": "webserver",
"name": "web.request.non-matching",
"resource": "/bar",
"sample_rate": 0,
},
{
"service": "webserver",
"name": "web.request",
"resource": "/bar.non-matching",
"sample_rate": 0,
},
{"service": "webserver", "name": "web.request", "resource": "/b?r", "sample_rate": 1},
]
),
},
],
)
def test_trace_sampled_by_trace_sampling_rule_exact_match(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Test that a trace is sampled by the exact matching trace sampling rule"""
with test_library, test_library.dd_start_span(name="web.request", service="webserver", resource="/bar"):
pass
span = find_only_span(test_agent.wait_for_num_traces(1))
assert span["metrics"].get(SAMPLING_PRIORITY_KEY) == 2
assert span["metrics"].get(SAMPLING_RULE_PRIORITY_RATE) == 1.0
@pytest.mark.parametrize(
"library_env",
[
{
"DD_TRACE_SAMPLE_RATE": 1,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[
{"service": "non-matching", "sample_rate": 1},
{"name": "non-matching", "sample_rate": 1},
{"resource": "non-matching", "sample_rate": 1},
{"service": "webserver", "name": "web.request", "resource": "/bar", "sample_rate": 0},
]
),
"DD_TRACE_STATS_COMPUTATION_ENABLED": "false",
}
],
)
def test_trace_dropped_by_trace_sampling_rule(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Test that a trace is dropped by the matching trace sampling rule"""
with test_library, test_library.dd_start_span(name="web.request", service="webserver", resource="/bar"):
pass
span = find_only_span(test_agent.wait_for_num_traces(1))
assert span["metrics"].get(SAMPLING_PRIORITY_KEY) == -1
assert span["metrics"].get(SAMPLING_RULE_PRIORITY_RATE) == 0.0
@features.trace_sampling
@features.adaptive_sampling
@scenarios.parametric
@rfc("https://docs.google.com/document/d/1S9pufnJjrsxH6pRbpigdYFwA5JjSdZ6iLZ-9E7PoAic/")
class Test_Trace_Sampling_Tags:
@pytest.mark.parametrize(
"library_env",
[
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[{"tags": {"tag1": "non-matching"}, "sample_rate": 0}, {"tags": {"tag1": "val1"}, "sample_rate": 1}]
),
},
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[
{"tags": {"tag1": "non-matching"}, "sample_rate": 0},
{"tags": {"tag2": "non-matching"}, "sample_rate": 0},
{"tags": {"tag1": "non-matching", "tag2": "val2"}, "sample_rate": 0},
{"tags": {"tag1": "val1", "tag2": "non-matching"}, "sample_rate": 0},
{"tags": {"tag1": "val1", "tag2": "val2"}, "sample_rate": 1},
]
),
},
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[{"tags": {"tag1": "v?r*"}, "sample_rate": 0}, {"tags": {"tag1": "val?"}, "sample_rate": 1}]
),
},
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[
{"service": "webs?rver.non-matching", "sample_rate": 0},
{"name": "web.request.non-matching", "sample_rate": 0},
{"resource": "/ba*.non-matching", "sample_rate": 0},
{"tags": {"tag1": "v?l1", "tag2": "va*.non-matching"}, "sample_rate": 0},
{
"service": "webs?rver",
"name": "web.request",
"resource": "/ba*",
"tags": {"tag1": "v?l1", "tag2": "val*"},
"sample_rate": 1,
},
]
),
},
],
)
def test_trace_sampled_by_trace_sampling_rule_tags(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Test that a trace is sampled by the matching trace sampling rule"""
with (
test_library,
test_library.dd_start_span(name="web.request", service="webserver", resource="/bar") as main_span,
):
main_span.set_meta("tag1", "val1")
main_span.set_meta("tag2", "val2")
span = find_only_span(test_agent.wait_for_num_traces(1))
assert span["metrics"].get(SAMPLING_PRIORITY_KEY) == 2
assert span["metrics"].get(SAMPLING_RULE_PRIORITY_RATE) == 1.0
@pytest.mark.parametrize(
"library_env",
[
{
"DD_TRACE_SAMPLE_RATE": 1,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[
{"tags": {"tag1": "v?l1", "tag2": "non-matching"}, "sample_rate": 1},
{"tags": {"tag1": "v?l1", "tag2": "val*"}, "sample_rate": 0},
]
),
"DD_TRACE_STATS_COMPUTATION_ENABLED": "false",
},
],
)
def test_trace_dropped_by_trace_sampling_rule_tags(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Test that a trace is dropped by the matching trace sampling rule"""
with (
test_library,
test_library.dd_start_span(name="web.request", service="webserver", resource="/bar") as main_span,
):
main_span.set_meta("tag1", "val1")
main_span.set_meta("tag2", "val2")
span = find_only_span(test_agent.wait_for_num_traces(1))
assert span["metrics"].get(SAMPLING_PRIORITY_KEY) == -1
assert span["metrics"].get(SAMPLING_RULE_PRIORITY_RATE) == 0.0
def tag_sampling_env(tag_glob_pattern: str) -> dict:
return {
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[{"tags": {"tag": tag_glob_pattern}, "sample_rate": 1.0}, {"sample_rate": 0}]
),
"DD_TRACE_STATS_COMPUTATION_ENABLED": "false",
}
@scenarios.parametric
@rfc("https://docs.google.com/document/d/1S9pufnJjrsxH6pRbpigdYFwA5JjSdZ6iLZ-9E7PoAic/")
@features.trace_sampling
@features.adaptive_sampling
class Test_Trace_Sampling_Tags_Feb2024_Revision:
def assert_matching_span(
self,
test_agent: TestAgentAPI,
trace_id: int,
span_id: int | str,
name: str | None = None,
service: str | None = None,
):
matching_span = find_span_in_traces(test_agent.wait_for_num_traces(1), trace_id, span_id)
assert matching_span["metrics"].get(SAMPLING_PRIORITY_KEY) == 2
assert matching_span["metrics"].get(SAMPLING_RULE_PRIORITY_RATE) == 1.0
if name is not None:
assert matching_span["name"] == name
if service is not None:
assert matching_span["service"] == service
def assert_mismatching_span(
self,
test_agent: TestAgentAPI,
trace_id: int,
span_id: int | str,
name: str | None = None,
service: str | None = None,
):
mismatching_span = find_span_in_traces(test_agent.wait_for_num_traces(1), trace_id, span_id)
assert mismatching_span["metrics"].get(SAMPLING_PRIORITY_KEY) == -1
assert mismatching_span["metrics"].get(SAMPLING_RULE_PRIORITY_RATE) == 0.0
if name is not None:
assert mismatching_span["name"] == name
if service is not None:
assert mismatching_span["service"] == service
@pytest.mark.parametrize(
"library_env",
[
tag_sampling_env("foo"),
tag_sampling_env("fo*"),
tag_sampling_env("f??"),
tag_sampling_env("?o*"),
tag_sampling_env("???"),
tag_sampling_env("*"),
tag_sampling_env("**"),
],
)
def test_globs_same_casing(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Test tag matching with string of matching case"""
with test_library, test_library.dd_start_span(name="matching-span", service="test") as span:
span.set_meta("tag", "foo")
self.assert_matching_span(test_agent, span.trace_id, span.span_id, name="matching-span", service="test")
@pytest.mark.parametrize(
"library_env",
[tag_sampling_env("Foo"), tag_sampling_env("Fo*"), tag_sampling_env("F??"), tag_sampling_env("?O*")],
)
def test_globs_different_casing(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Test tag matching with string of matching case"""
with test_library, test_library.dd_start_span(name="matching-span", service="test") as span:
span.set_meta("tag", "foo")
self.assert_matching_span(test_agent, span.trace_id, span.span_id, name="matching-span", service="test")
@pytest.mark.parametrize("library_env", [tag_sampling_env("[abc]")])
def test_no_set_support(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Test verifying that common glob set extension is NOT supported"""
with test_library, test_library.dd_start_span(name="matching-span", service="test") as span:
span.set_meta("tag", "[abc]")
self.assert_matching_span(test_agent, span.trace_id, span.span_id, name="matching-span", service="test")
@pytest.mark.parametrize("library_env", [tag_sampling_env("[a-c]")])
def test_no_range_support(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Test verifying that common glob range extension is NOT supported"""
with test_library, test_library.dd_start_span(name="matching-span", service="test") as span:
span.set_meta("tag", "[a-c]")
self.assert_matching_span(test_agent, span.trace_id, span.span_id, name="matching-span", service="test")
@pytest.mark.parametrize("library_env", [tag_sampling_env("^(foo|bar)[]\\$")])
def test_regex_special_chars(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Test verifying that regex special chars doesn't break glob matching"""
with test_library, test_library.dd_start_span(name="matching-span", service="test") as span:
span.set_meta("tag", "^(foo|bar)[]\\$")
self.assert_matching_span(test_agent, span.trace_id, span.span_id, name="matching-span", service="test")
@pytest.mark.parametrize("library_env", [tag_sampling_env("*"), tag_sampling_env("**"), tag_sampling_env("***")])
def test_meta_existence(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Tests that any patterns are equivalent to an existence check for meta"""
with test_library, test_library.dd_start_span(name="matching-span", service="test") as span:
span.set_meta("tag", random.choice(["foo", "bar", "baz", "quux"]))
self.assert_matching_span(test_agent, span.trace_id, span.span_id, name="matching-span", service="test")
@pytest.mark.parametrize("library_env", [tag_sampling_env("*"), tag_sampling_env("**"), tag_sampling_env("***")])
@pytest.mark.parametrize("tag_value", [-100, -0.5, 0, 5, 1000])
def test_metric_existence(self, test_agent: TestAgentAPI, test_library: APMLibrary, tag_value: float):
"""Tests that any patterns are equivalent to an existence check for metrics"""
with test_library, test_library.dd_start_span(name="matching-span", service="test") as span:
span.set_metric("tag", tag_value)
self.assert_matching_span(test_agent, span.trace_id, span.span_id, name="matching-span", service="test")
@pytest.mark.parametrize(
"library_env", [tag_sampling_env("20"), tag_sampling_env("2*"), tag_sampling_env("2?"), tag_sampling_env("*")]
)
def test_metric_matching(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Tests that any patterns are equivalent to an existence check for metrics"""
with test_library, test_library.dd_start_span(name="matching-span", service="test") as span:
span.set_metric("tag", 20.0)
self.assert_matching_span(test_agent, span.trace_id, span.span_id, name="matching-span", service="test")
@pytest.mark.parametrize("library_env", [tag_sampling_env("20"), tag_sampling_env("2*"), tag_sampling_env("2?")])
def test_metric_mismatch_non_integer(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Tests that any non-integer metrics mismatch patterns -- other than any patterns"""
with test_library, test_library.dd_start_span(name="mismatching-span", service="test") as span:
span.set_metric("tag", 20.1)
self.assert_mismatching_span(test_agent, span.trace_id, span.span_id, name="mismatching-span", service="test")
@scenarios.parametric
@rfc("https://docs.google.com/document/d/1S9pufnJjrsxH6pRbpigdYFwA5JjSdZ6iLZ-9E7PoAic/")
@features.trace_sampling
@features.adaptive_sampling
class Test_Trace_Sampling_With_W3C:
@pytest.mark.parametrize(
"library_env",
[
{
"DD_TRACE_SAMPLE_RATE": 0,
"DD_TRACE_SAMPLING_RULES": json.dumps(
[
{"tags": {"tag2": "val2"}, "sample_rate": 0},
{"tags": {"tag1": "val1"}, "sample_rate": 1},
{"tags": {"tag0": "val*"}, "sample_rate": 0},
]
),
},
],
)
def test_trace_sampled_by_trace_sampling_rule_tags(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Test that a trace is sampled by the rule and the sampling decision is locked"""
with (
test_library,
test_library.dd_start_span(
name="web.request", service="webserver", resource="/bar", tags=[("tag0", "val0")]
) as main_span,
):
# based on the Tag("tag0", "val0") start span option, span sampling would be 'drop',
# setting new tags doesn't trigger re-sampling,
# but injecting headers does. In such case, headers will reflect the state
# after new pair of tags was set
# based on the Tag("tag1", "val1"), span sampling would be 'keep'
main_span.set_meta("tag1", "val1")
headers = {k.lower(): v for k, v in test_library.dd_inject_headers(main_span.span_id)}
# based on the Tag("tag2", "val2"), span sampling would be usually 'drop',
# but since headers were injected already, the sampling priority won't change
main_span.set_meta("tag2", "val2")
span = find_only_span(test_agent.wait_for_num_traces(1))
# sampling priority in headers reflects the state after new pair of tags was set
assert headers["x-datadog-sampling-priority"] == "2"
assert span["metrics"].get(SAMPLING_PRIORITY_KEY) == 2
assert span["metrics"].get(SAMPLING_RULE_PRIORITY_RATE) == 1
@pytest.mark.parametrize(
"library_env",
[
{
"DD_TRACE_SAMPLING_RULES": json.dumps([{"sample_rate": 0}]),
},
],
)
def test_distributed_headers_synthetics_sampling_decision(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Ensure that trace sampling rules does not override sampling priority from distributed headers
even when sampling priority is set via synthetics.
"""
with test_library:
test_library.dd_make_child_span_and_get_headers(
[
("x-datadog-trace-id", "123456789"),
("x-datadog-parent-id", "0"),
("x-datadog-sampling-priority", "1"),
("x-datadog-origin", "synthetics;=web,z"),
],
)
span = find_only_span(test_agent.wait_for_num_traces(1))
assert span.get("trace_id") == 123456789
assert span.get("parent_id") in (0, None)
assert "synthetics" in span["meta"].get(ORIGIN)
assert span["metrics"].get(SAMPLING_PRIORITY_KEY) == 1