-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathtest_options.py
More file actions
1510 lines (1370 loc) · 55.5 KB
/
test_options.py
File metadata and controls
1510 lines (1370 loc) · 55.5 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
# (c) Copyright IBM Corp. 2025
import logging
import os
from typing import Generator
import pytest
from mock import patch
from instana.configurator import config
from instana.options import (
AWSFargateOptions,
AWSLambdaOptions,
BaseOptions,
EKSFargateOptions,
GCROptions,
ServerlessOptions,
StandardOptions,
)
INTERNAL_SPAN_FILTERS = [
{
"name": "filter-internal-spans-by-url",
"attributes": [
{
"key": "http.url",
"values": ["com.instana"],
"match_type": "contains",
}
],
},
{
"name": "filter-internal-spans-by-host",
"attributes": [
{
"key": "http.host",
"values": ["com.instana"],
"match_type": "contains",
}
],
},
{
"name": "filter-internal-sdk-spans-by-url",
"attributes": [
{
"key": "sdk.custom.tags.http.url",
"values": ["com.instana"],
"match_type": "contains",
}
],
},
]
class TestBaseOptions:
@pytest.fixture(autouse=True)
def _resource(self) -> Generator[None, None, None]:
self.base_options = None
yield
if "tracing" in config.keys():
del config["tracing"]
def test_base_options(self) -> None:
if "INSTANA_DEBUG" in os.environ:
del os.environ["INSTANA_DEBUG"]
for key in list(os.environ.keys()):
if key.startswith("INSTANA_TRACING_FILTER_"):
del os.environ[key]
self.base_options = BaseOptions()
assert not self.base_options.debug
assert self.base_options.log_level == logging.WARN
assert not self.base_options.extra_http_headers
assert not self.base_options.allow_exit_as_root
assert self.base_options.span_filters == {"exclude": INTERNAL_SPAN_FILTERS}
assert self.base_options.kafka_trace_correlation
assert self.base_options.secrets_matcher == "contains-ignore-case"
assert self.base_options.secrets_list == ["key", "pass", "secret"]
assert not self.base_options.secrets
assert self.base_options.disabled_spans == []
assert self.base_options.enabled_spans == []
def test_base_options_with_config(self) -> None:
config["tracing"] = {
"filter": {
"exclude": [
{
"name": "service1",
"attributes": [
{
"key": "service",
"values": ["service1"],
"match_type": "strict",
}
],
},
{
"name": "service3",
"attributes": [
{
"key": "method",
"values": ["method1", "method2"],
"match_type": "strict",
}
],
},
]
},
"kafka": {"trace_correlation": True},
}
self.base_options = BaseOptions()
assert self.base_options.span_filters == {
"exclude": [
{
"name": "service1",
"attributes": [
{
"key": "service",
"values": ["service1"],
"match_type": "strict",
}
],
"suppression": True,
},
{
"name": "service3",
"attributes": [
{
"key": "method",
"values": ["method1", "method2"],
"match_type": "strict",
}
],
"suppression": True,
},
*INTERNAL_SPAN_FILTERS,
],
"include": [],
}
assert self.base_options.kafka_trace_correlation
@patch.dict(
os.environ,
{
"INSTANA_DEBUG": "true",
"INSTANA_EXTRA_HTTP_HEADERS": "SOMETHING;HERE",
"INSTANA_TRACING_FILTER_EXCLUDE_SERVICE1_ATTRIBUTES": "type;service1;strict",
"INSTANA_TRACING_FILTER_EXCLUDE_SERVICE2_ATTRIBUTES": "type;service2;strict",
"INSTANA_SECRETS": "secret1:username,password",
"INSTANA_TRACING_DISABLE": "logging, redis,kafka",
},
)
def test_base_options_with_env_vars(self) -> None:
self.base_options = BaseOptions()
assert self.base_options.log_level == logging.DEBUG
assert self.base_options.debug
assert self.base_options.extra_http_headers == ["something", "here"]
assert self.base_options.span_filters == {
"include": [],
"exclude": [
{
"name": "SERVICE1",
"attributes": [
{"key": "type", "values": ["service1"], "match_type": "strict"}
],
"suppression": True,
},
{
"name": "SERVICE2",
"attributes": [
{"key": "type", "values": ["service2"], "match_type": "strict"}
],
"suppression": True,
},
{
"name": "filter-internal-spans-by-url",
"attributes": [
{
"key": "http.url",
"values": ["com.instana"],
"match_type": "contains",
}
],
},
{
"name": "filter-internal-spans-by-host",
"attributes": [
{
"key": "http.host",
"values": ["com.instana"],
"match_type": "contains",
}
],
},
{
"name": "filter-internal-sdk-spans-by-url",
"attributes": [
{
"key": "sdk.custom.tags.http.url",
"values": ["com.instana"],
"match_type": "contains",
}
],
},
],
}
assert self.base_options.secrets_matcher == "secret1"
assert self.base_options.secrets_list == ["username", "password"]
assert "logging" in self.base_options.disabled_spans
assert "redis" in self.base_options.disabled_spans
assert "kafka" in self.base_options.disabled_spans
assert len(self.base_options.enabled_spans) == 0
@patch.dict(
os.environ,
{"INSTANA_CONFIG_PATH": "tests/util/test_configuration-1.yaml"},
)
def test_base_options_with_endpoint_file(self) -> None:
self.base_options = BaseOptions()
assert self.base_options.span_filters == {
"include": [
{
"name": "Kafka Producer",
"attributes": [
{"key": "type", "values": ["kafka"], "match_type": "strict"},
{"key": "kind", "values": ["exit"], "match_type": "strict"},
{
"key": "kafka.service",
"values": ["topic"],
"match_type": "contains",
},
],
"suppression": None,
}
],
"exclude": [
{
"name": "Redis",
"attributes": [
{"key": "command", "values": ["get"], "match_type": "strict"},
{"key": "get", "values": ["type"], "match_type": "strict"},
],
"suppression": True,
},
{
"name": "DynamoDB",
"attributes": [
{"key": "op", "values": ["query"], "match_type": "strict"}
],
"suppression": True,
},
{
"name": "Kafka",
"attributes": [
{
"key": "kafka.access",
"values": ["consume", "send", "produce"],
"match_type": "contains",
},
{
"key": "kafka.service",
"values": ["span-topic", "topic1", "topic2"],
"match_type": "strict",
},
{
"key": "kafka.access",
"values": ["*"],
"match_type": "strict",
},
],
"suppression": True,
},
{
"name": "Protocols Category",
"suppression": True,
"attributes": [
{
"key": "category",
"values": ["protocols"],
"match_type": "strict",
}
],
},
{
"name": "Entry Span Kind",
"suppression": True,
"attributes": [
{
"key": "kind",
"values": ["intermediate"],
"match_type": "strict",
}
],
},
{
"name": "filter-internal-spans-by-url",
"attributes": [
{
"key": "http.url",
"values": ["com.instana"],
"match_type": "contains",
}
],
},
{
"name": "filter-internal-spans-by-host",
"attributes": [
{
"key": "http.host",
"values": ["com.instana"],
"match_type": "contains",
}
],
},
{
"name": "filter-internal-sdk-spans-by-url",
"attributes": [
{
"key": "sdk.custom.tags.http.url",
"values": ["com.instana"],
"match_type": "contains",
}
],
},
],
}
del self.base_options
@patch.dict(
os.environ,
{
"INSTANA_TRACING_FILTER_EXCLUDE_SERVICE1_ATTRIBUTES": "type;env_service1;strict",
"INSTANA_TRACING_FILTER_EXCLUDE_SERVICE2_ATTRIBUTES": "type;env_service2.method1,env_service2.method2;strict",
"INSTANA_KAFKA_TRACE_CORRELATION": "false",
"INSTANA_TRACING_DISABLE": "logging,redis, kafka",
},
)
def test_set_trace_configurations_by_env_variable(self) -> None:
# The priority is as follows:
# environment variables > in-code configuration >
# > agent config (configuration.yaml) > default value
# in-code configuration
config["tracing"] = {}
config["tracing"]["filter"] = "config_service1;config_service2:method1,method2"
config["tracing"]["kafka"] = {"trace_correlation": True}
config["tracing"]["disable"] = [{"databases": True}]
# agent config (configuration.yaml)
test_tracing = {
"filter": "service1;service2:method1,method2",
"disable": [
{"messaging": True},
],
}
# Setting by env variable
self.base_options = StandardOptions()
self.base_options.set_tracing(test_tracing)
assert self.base_options.span_filters == {
"include": [],
"exclude": [
{
"name": "SERVICE1",
"attributes": [
{
"key": "type",
"values": ["env_service1"],
"match_type": "strict",
}
],
"suppression": True,
},
{
"name": "SERVICE2",
"attributes": [
{
"key": "type",
"values": ["env_service2.method1", "env_service2.method2"],
"match_type": "strict",
}
],
"suppression": True,
},
{
"name": "filter-internal-spans-by-url",
"attributes": [
{
"key": "http.url",
"values": ["com.instana"],
"match_type": "contains",
}
],
},
{
"name": "filter-internal-spans-by-host",
"attributes": [
{
"key": "http.host",
"values": ["com.instana"],
"match_type": "contains",
}
],
},
{
"name": "filter-internal-sdk-spans-by-url",
"attributes": [
{
"key": "sdk.custom.tags.http.url",
"values": ["com.instana"],
"match_type": "contains",
}
],
},
],
}
assert not self.base_options.kafka_trace_correlation
# Check disabled_spans list
assert "logging" in self.base_options.disabled_spans
assert "redis" in self.base_options.disabled_spans
assert "kafka" in self.base_options.disabled_spans
assert "databases" not in self.base_options.disabled_spans
assert "messaging" not in self.base_options.disabled_spans
assert len(self.base_options.enabled_spans) == 0
@patch.dict(
os.environ,
{
"INSTANA_KAFKA_TRACE_CORRELATION": "false",
"INSTANA_CONFIG_PATH": "tests/util/test_configuration-1.yaml",
},
)
def test_set_trace_configurations_by_in_code_configuration(self) -> None:
# The priority is as follows:
# environment variables (INSTANA_CONFIG_PATH) > in-code configuration > agent config (configuration.yaml) > default value
# in-code configuration
config["tracing"] = {}
config["tracing"]["filter"] = "config_service1;config_service2:method1,method2"
config["tracing"]["kafka"] = {"trace_correlation": True}
config["tracing"]["disable"] = [{"databases": True}]
# agent config (configuration.yaml)
test_tracing = {
"filter": "service1;service2:method1,method2",
"disable": [
{"messaging": True},
],
}
self.base_options = StandardOptions()
self.base_options.set_tracing(test_tracing)
assert self.base_options.span_filters == {
"include": [
{
"name": "Kafka Producer",
"attributes": [
{"key": "type", "values": ["kafka"], "match_type": "strict"},
{"key": "kind", "values": ["exit"], "match_type": "strict"},
{
"key": "kafka.service",
"values": ["topic"],
"match_type": "contains",
},
],
"suppression": None,
}
],
"exclude": [
{
"name": "Redis",
"attributes": [
{"key": "command", "values": ["get"], "match_type": "strict"},
{"key": "get", "values": ["type"], "match_type": "strict"},
],
"suppression": True,
},
{
"name": "DynamoDB",
"attributes": [
{"key": "op", "values": ["query"], "match_type": "strict"}
],
"suppression": True,
},
{
"name": "Kafka",
"attributes": [
{
"key": "kafka.access",
"values": ["consume", "send", "produce"],
"match_type": "contains",
},
{
"key": "kafka.service",
"values": ["span-topic", "topic1", "topic2"],
"match_type": "strict",
},
{
"key": "kafka.access",
"values": ["*"],
"match_type": "strict",
},
],
"suppression": True,
},
{
"name": "Protocols Category",
"suppression": True,
"attributes": [
{
"key": "category",
"values": ["protocols"],
"match_type": "strict",
}
],
},
{
"name": "Entry Span Kind",
"suppression": True,
"attributes": [
{
"key": "kind",
"values": ["intermediate"],
"match_type": "strict",
}
],
},
{
"name": "filter-internal-spans-by-url",
"attributes": [
{
"key": "http.url",
"values": ["com.instana"],
"match_type": "contains",
}
],
},
{
"name": "filter-internal-spans-by-host",
"attributes": [
{
"key": "http.host",
"values": ["com.instana"],
"match_type": "contains",
}
],
},
{
"name": "filter-internal-sdk-spans-by-url",
"attributes": [
{
"key": "sdk.custom.tags.http.url",
"values": ["com.instana"],
"match_type": "contains",
}
],
},
],
}
# Check disabled_spans list
assert "databases" in self.base_options.disabled_spans
assert "logging" in self.base_options.disabled_spans
assert "redis" not in self.base_options.disabled_spans
assert "kafka" not in self.base_options.disabled_spans
assert "messaging" not in self.base_options.disabled_spans
assert "redis" in self.base_options.enabled_spans
def test_set_trace_configurations_by_in_code_variable(self) -> None:
config["tracing"] = {}
config["tracing"]["filter"] = {
"exclude": [
{
"name": "config_service1",
"attributes": [
{
"key": "service",
"values": ["config_service1"],
"match_type": "strict",
}
],
},
{
"name": "config_service2",
"attributes": [
{
"key": "method",
"values": ["method1", "method2"],
"match_type": "strict",
}
],
},
]
}
config["tracing"]["kafka"] = {"trace_correlation": True}
test_tracing = {
"filter": {
"exclude": [
{
"name": "service1",
"attributes": [
{
"key": "service",
"values": ["service1"],
"match_type": "strict",
}
],
},
]
}
}
self.base_options = StandardOptions()
self.base_options.set_tracing(test_tracing)
assert self.base_options.span_filters == {
"exclude": [
{
"name": "config_service1",
"attributes": [
{
"key": "service",
"values": ["config_service1"],
"match_type": "strict",
}
],
"suppression": True,
},
{
"name": "config_service2",
"attributes": [
{
"key": "method",
"values": ["method1", "method2"],
"match_type": "strict",
}
],
"suppression": True,
},
*INTERNAL_SPAN_FILTERS,
],
"include": [],
}
assert self.base_options.kafka_trace_correlation
def test_set_trace_configurations_by_agent_configuration(self) -> None:
test_tracing = {
"filter": {
"exclude": [
{
"name": "service1",
"attributes": [
{
"key": "service",
"values": ["service1"],
"match_type": "strict",
}
],
},
{
"name": "service2",
"attributes": [
{
"key": "method",
"values": ["method1", "method2"],
"match_type": "strict",
}
],
},
]
},
"trace-correlation": True,
"disable": [
{
"messaging": True,
"logging": True,
"kafka": False,
},
],
}
self.base_options = StandardOptions()
self.base_options.set_tracing(test_tracing)
# set_tracing does not override span_filters when already set (has internal filters)
assert self.base_options.span_filters == {"exclude": INTERNAL_SPAN_FILTERS}
assert self.base_options.kafka_trace_correlation
# Check disabled_spans list
assert "databases" not in self.base_options.disabled_spans
assert "logging" in self.base_options.disabled_spans
assert "messaging" in self.base_options.disabled_spans
assert "kafka" in self.base_options.enabled_spans
def test_set_trace_configurations_by_default(self) -> None:
self.base_options = StandardOptions()
self.base_options.set_tracing({})
assert self.base_options.span_filters == {"exclude": INTERNAL_SPAN_FILTERS}
assert self.base_options.kafka_trace_correlation
assert len(self.base_options.disabled_spans) == 0
assert len(self.base_options.enabled_spans) == 0
@patch.dict(
os.environ,
{"INSTANA_TRACING_DISABLE": "true"},
)
def test_set_trace_configurations_disable_all_tracing(self) -> None:
self.base_options = BaseOptions()
# All categories should be disabled
assert "logging" in self.base_options.disabled_spans
assert "databases" in self.base_options.disabled_spans
assert "messaging" in self.base_options.disabled_spans
assert "protocols" in self.base_options.disabled_spans
# Check is_span_disabled method
assert self.base_options.is_span_disabled(category="logging")
assert self.base_options.is_span_disabled(category="databases")
assert self.base_options.is_span_disabled(span_type="redis")
@patch.dict(
os.environ,
{
"INSTANA_CONFIG_PATH": "tests/util/test_configuration-1.yaml",
},
)
def test_set_trace_configurations_disable_local_yaml(self) -> None:
self.base_options = BaseOptions()
# All categories should be disabled
assert "logging" in self.base_options.disabled_spans
assert "databases" in self.base_options.disabled_spans
assert "redis" not in self.base_options.disabled_spans
assert "redis" in self.base_options.enabled_spans
# Check is_span_disabled method
assert self.base_options.is_span_disabled(category="logging")
assert self.base_options.is_span_disabled(category="databases")
assert not self.base_options.is_span_disabled(span_type="redis")
def test_is_span_disabled_method(self) -> None:
self.base_options = BaseOptions()
# Default behavior - nothing disabled
assert not self.base_options.is_span_disabled(category="logging")
assert not self.base_options.is_span_disabled(span_type="redis")
# Disable a category
self.base_options.disabled_spans = ["databases"]
assert not self.base_options.is_span_disabled(category="logging")
assert self.base_options.is_span_disabled(category="databases")
assert self.base_options.is_span_disabled(span_type="redis")
assert self.base_options.is_span_disabled(span_type="mysql")
# Test precedence rules
self.base_options.enabled_spans = ["redis"]
assert self.base_options.is_span_disabled(category="databases")
assert self.base_options.is_span_disabled(span_type="mysql")
assert not self.base_options.is_span_disabled(span_type="redis")
@patch.dict(
os.environ,
{
"INSTANA_TRACING_FILTER_EXCLUDE_KAFKA_ATTRIBUTES": "kafka.service;kafka;strict",
"INSTANA_TRACING_FILTER_EXCLUDE_REDIS_ATTRIBUTES": "redis.command;SET,GET;contains",
"INSTANA_TRACING_FILTER_INCLUDE_FOO_ATTRIBUTES": "http.url;foo;contains",
},
)
def test_tracing_filter_environment_variables(self) -> None:
self.base_options = StandardOptions()
assert self.base_options.span_filters == {
"include": [
{
"name": "FOO",
"attributes": [
{"key": "http.url", "values": ["foo"], "match_type": "contains"}
],
"suppression": None,
}
],
"exclude": [
{
"name": "KAFKA",
"attributes": [
{
"key": "kafka.service",
"values": ["kafka"],
"match_type": "strict",
}
],
"suppression": True,
},
{
"name": "REDIS",
"attributes": [
{
"key": "redis.command",
"values": ["SET", "GET"],
"match_type": "contains",
}
],
"suppression": True,
},
{
"name": "filter-internal-spans-by-url",
"attributes": [
{
"key": "http.url",
"values": ["com.instana"],
"match_type": "contains",
}
],
},
{
"name": "filter-internal-spans-by-host",
"attributes": [
{
"key": "http.host",
"values": ["com.instana"],
"match_type": "contains",
}
],
},
{
"name": "filter-internal-sdk-spans-by-url",
"attributes": [
{
"key": "sdk.custom.tags.http.url",
"values": ["com.instana"],
"match_type": "contains",
}
],
},
],
}
class TestStandardOptions:
@pytest.fixture(autouse=True)
def _resource(self) -> Generator[None, None, None]:
self.standart_options = None
yield
if "tracing" in config.keys():
del config["tracing"]
def test_standard_options(self) -> None:
self.standart_options = StandardOptions()
assert self.standart_options.AGENT_DEFAULT_HOST == "localhost"
assert self.standart_options.AGENT_DEFAULT_PORT == 42699
def test_set_secrets(self) -> None:
self.standart_options = StandardOptions()
test_secrets = {"matcher": "sample-match", "list": ["sample", "list"]}
self.standart_options.set_secrets(test_secrets)
assert self.standart_options.secrets_matcher == "sample-match"
assert self.standart_options.secrets_list == ["sample", "list"]
def test_set_extra_headers(self) -> None:
self.standart_options = StandardOptions()
test_headers = {"header1": "sample-match", "header2": ["sample", "list"]}
self.standart_options.set_extra_headers(test_headers)
assert self.standart_options.extra_http_headers == test_headers
def test_set_tracing(
self,
caplog: pytest.LogCaptureFixture,
) -> None:
caplog.set_level(logging.DEBUG, logger="instana")
self.standart_options = StandardOptions()
test_tracing = {
"filter": {
"exclude": [
{
"name": "service1",
"attributes": [
{
"key": "service",
"values": ["service1"],
"match_type": "strict",
}
],
},
{
"name": "service2",
"attributes": [
{
"key": "method",
"values": ["method1", "method2"],
"match_type": "strict",
}
],
},
]
},
"kafka": {"trace-correlation": "false", "header-format": "binary"},
}
self.standart_options.set_tracing(test_tracing)
assert self.standart_options.span_filters == {"exclude": INTERNAL_SPAN_FILTERS}
assert not self.standart_options.kafka_trace_correlation
assert (
"Binary header format for Kafka is deprecated. Please use string header format."
in caplog.messages
)
assert not self.standart_options.extra_http_headers
def test_set_tracing_with_span_disabling(self) -> None:
self.standart_options = StandardOptions()
test_tracing = {
"disable": [{"logging": True}, {"redis": False}, {"databases": True}]
}
self.standart_options.set_tracing(test_tracing)
# Check disabled_spans and enabled_spans lists
assert "logging" in self.standart_options.disabled_spans
assert "databases" in self.standart_options.disabled_spans
assert "redis" in self.standart_options.enabled_spans
# Check is_span_disabled method
assert self.standart_options.is_span_disabled(category="logging")
assert self.standart_options.is_span_disabled(category="databases")
assert self.standart_options.is_span_disabled(span_type="mysql")
assert not self.standart_options.is_span_disabled(span_type="redis")
def test_set_from(self) -> None:
self.standart_options = StandardOptions()
test_res_data = {
"secrets": {"matcher": "sample-match", "list": ["sample", "list"]},
"tracing": {
"filter": {
"exclude": [
{
"name": "service1",
"attributes": [
{
"key": "service",
"values": ["service1"],
"match_type": "strict",
}
],
},
{
"name": "service2",
"attributes": [
{
"key": "method",
"values": ["method1", "method2"],
"match_type": "strict",
}
],
},
]
}
},
}
self.standart_options.set_from(test_res_data)
assert (
self.standart_options.secrets_matcher == test_res_data["secrets"]["matcher"]
)
assert self.standart_options.secrets_list == test_res_data["secrets"]["list"]
assert self.standart_options.span_filters == {"exclude": INTERNAL_SPAN_FILTERS}
test_res_data2 = {
"extraHeaders": {"header1": "sample-match", "header2": ["sample", "list"]},
}
self.standart_options.set_from(test_res_data2)
assert (
self.standart_options.extra_http_headers == test_res_data2["extraHeaders"]
)
def test_set_from_bool(
self,
caplog: pytest.LogCaptureFixture,
) -> None:
caplog.set_level(logging.DEBUG, logger="instana")
caplog.clear()
self.standart_options = StandardOptions()
self.standart_options.set_from(True) # type: ignore[arg-type]
assert len(caplog.messages) == 1
assert len(caplog.records) == 1
assert (
"options.set_from: Wrong data type - <class 'bool'>" in caplog.messages[0]
)