-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtest_kafka_python.py
More file actions
858 lines (695 loc) · 29.9 KB
/
Copy pathtest_kafka_python.py
File metadata and controls
858 lines (695 loc) · 29.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
# (c) Copyright IBM Corp. 2025
import os
from typing import Generator
import pytest
from kafka import KafkaConsumer, KafkaProducer
from kafka.admin import KafkaAdminClient, NewTopic
from kafka.errors import TopicAlreadyExistsError
from mock import patch
from opentelemetry.trace import SpanKind
from opentelemetry.trace.span import format_span_id
from instana.configurator import config
from instana.instrumentation.kafka import kafka_python
from instana.instrumentation.kafka.kafka_python import (
clear_context,
close_consumer_span,
consumer_span,
save_consumer_span_into_context,
)
from instana.options import StandardOptions
from instana.singletons import agent, tracer
from instana.span.span import InstanaSpan
from instana.util.config import parse_ignored_endpoints_from_yaml
from tests.helpers import get_first_span_by_filter, testenv
class TestKafkaPython:
@pytest.fixture(autouse=True)
def _resource(self) -> Generator[None, None, None]:
"""SetUp and TearDown"""
# setup
# Clear all spans before a test run
self.recorder = tracer.span_processor
self.recorder.clear_spans()
# Kafka admin client
self.kafka_client = KafkaAdminClient(
bootstrap_servers=testenv["kafka_bootstrap_servers"],
client_id="test_kafka_python",
)
try:
self.kafka_client.create_topics(
[
NewTopic(
name=testenv["kafka_topic"],
num_partitions=1,
replication_factor=1,
),
NewTopic(
name=testenv["kafka_topic"] + "_1",
num_partitions=1,
replication_factor=1,
),
NewTopic(
name=testenv["kafka_topic"] + "_2",
num_partitions=1,
replication_factor=1,
),
NewTopic(
name=testenv["kafka_topic"] + "_3",
num_partitions=1,
replication_factor=1,
),
]
)
except TopicAlreadyExistsError:
pass
# Kafka producer
self.producer = KafkaProducer(
bootstrap_servers=testenv["kafka_bootstrap_servers"]
)
agent.options = StandardOptions()
yield
# teardown
# Ensure that allow_exit_as_root has the default value"""
agent.options.allow_exit_as_root = False
# Close connections
self.producer.close()
# Clear context
clear_context()
self.kafka_client.delete_topics(
[
testenv["kafka_topic"],
testenv["kafka_topic"] + "_1",
testenv["kafka_topic"] + "_2",
testenv["kafka_topic"] + "_3",
]
)
self.kafka_client.close()
def test_trace_kafka_python_send(self) -> None:
with tracer.start_as_current_span("test"):
future = self.producer.send(testenv["kafka_topic"], b"raw_bytes")
_ = future.get(timeout=10) # noqa: F841
spans = self.recorder.queued_spans()
assert len(spans) == 2
kafka_span = spans[0]
test_span = spans[1]
# Same traceId
assert test_span.t == kafka_span.t
# Parent relationships
assert kafka_span.p == test_span.s
# Error logging
assert not test_span.ec
assert not kafka_span.ec
assert kafka_span.n == "kafka"
assert kafka_span.k == SpanKind.CLIENT
assert kafka_span.data["kafka"]["service"] == testenv["kafka_topic"]
assert kafka_span.data["kafka"]["access"] == "send"
def test_trace_kafka_python_send_with_keyword_topic(self) -> None:
"""Test that tracing works when topic is passed as a keyword argument."""
with tracer.start_as_current_span("test"):
# Pass topic as a keyword argument
future = self.producer.send(
topic=testenv["kafka_topic"], value=b"raw_bytes"
)
_ = future.get(timeout=10) # noqa: F841
spans = self.recorder.queued_spans()
assert len(spans) == 2
kafka_span = spans[0]
test_span = spans[1]
# Same traceId
assert test_span.t == kafka_span.t
# Parent relationships
assert kafka_span.p == test_span.s
# Error logging
assert not test_span.ec
assert not kafka_span.ec
assert kafka_span.n == "kafka"
assert kafka_span.k == SpanKind.CLIENT
assert kafka_span.data["kafka"]["service"] == testenv["kafka_topic"]
assert kafka_span.data["kafka"]["access"] == "send"
def test_trace_kafka_python_send_with_keyword_args(self) -> None:
"""Test that tracing works when both topic and headers are passed as keyword arguments."""
with tracer.start_as_current_span("test"):
# Pass both topic and headers as keyword arguments
future = self.producer.send(
topic=testenv["kafka_topic"],
value=b"raw_bytes",
headers=[("custom-header", b"header-value")],
)
_ = future.get(timeout=10) # noqa: F841
spans = self.recorder.queued_spans()
assert len(spans) == 2
kafka_span = spans[0]
test_span = spans[1]
# Same traceId
assert test_span.t == kafka_span.t
# Parent relationships
assert kafka_span.p == test_span.s
# Error logging
assert not test_span.ec
assert not kafka_span.ec
assert kafka_span.n == "kafka"
assert kafka_span.k == SpanKind.CLIENT
assert kafka_span.data["kafka"]["service"] == testenv["kafka_topic"]
assert kafka_span.data["kafka"]["access"] == "send"
def test_trace_kafka_python_consume(self) -> None:
# Produce some events
self.producer.send(testenv["kafka_topic"], b"raw_bytes1")
self.producer.send(testenv["kafka_topic"], b"raw_bytes2")
self.producer.flush()
# Consume the events
consumer = KafkaConsumer(
testenv["kafka_topic"],
bootstrap_servers=testenv["kafka_bootstrap_servers"],
auto_offset_reset="earliest", # consume earliest available messages
enable_auto_commit=False, # do not auto-commit offsets
consumer_timeout_ms=1000,
)
with tracer.start_as_current_span("test"):
for msg in consumer:
if msg is None:
break
consumer.close()
spans = self.recorder.queued_spans()
assert len(spans) == 3
def filter(span):
return span.n == "kafka" and span.data["kafka"]["access"] == "consume"
kafka_span = get_first_span_by_filter(spans, filter)
def filter(span):
return span.n == "sdk" and span.data["sdk"]["name"] == "test"
test_span = get_first_span_by_filter(spans, filter)
# Same traceId
assert test_span.t == kafka_span.t
# Parent relationships
assert kafka_span.p == test_span.s
# Error logging
assert not test_span.ec
assert not kafka_span.ec
assert kafka_span.n == "kafka"
assert kafka_span.k == SpanKind.SERVER
assert kafka_span.data["kafka"]["service"] == testenv["kafka_topic"]
assert kafka_span.data["kafka"]["access"] == "consume"
def test_trace_kafka_python_poll(self) -> None:
# Produce some events
self.producer.send(testenv["kafka_topic"], b"raw_bytes1")
self.producer.send(testenv["kafka_topic"], b"raw_bytes2")
self.producer.flush()
# Consume the events
consumer = KafkaConsumer(
testenv["kafka_topic"],
bootstrap_servers=testenv["kafka_bootstrap_servers"],
auto_offset_reset="earliest", # consume earliest available messages
enable_auto_commit=False, # do not auto-commit offsets
consumer_timeout_ms=1000,
)
with tracer.start_as_current_span("test"):
msg = consumer.poll(timeout_ms=3000) # noqa: F841
consumer.close()
spans = self.recorder.queued_spans()
assert len(spans) == 3
def filter(span):
return span.n == "kafka" and span.data["kafka"]["access"] == "poll"
kafka_span = get_first_span_by_filter(spans, filter)
def filter(span):
return span.n == "sdk" and span.data["sdk"]["name"] == "test"
test_span = get_first_span_by_filter(spans, filter)
# Same traceId
assert test_span.t == kafka_span.t
# Parent relationships
assert kafka_span.p == test_span.s
# Error logging
assert not test_span.ec
assert not kafka_span.ec
assert kafka_span.n == "kafka"
assert kafka_span.k == SpanKind.SERVER
assert kafka_span.data["kafka"]["service"] == testenv["kafka_topic"]
assert kafka_span.data["kafka"]["access"] == "poll"
def test_trace_kafka_python_error(self) -> None:
consumer = KafkaConsumer(
"inexistent_kafka_topic",
bootstrap_servers=testenv["kafka_bootstrap_servers"],
auto_offset_reset="earliest",
enable_auto_commit=False,
consumer_timeout_ms=1000,
)
with tracer.start_as_current_span("test"):
consumer._client = None
try:
for msg in consumer:
if msg is None:
break
except Exception:
pass
spans = self.recorder.queued_spans()
assert len(spans) == 2
def filter(span):
return span.n == "kafka" and span.data["kafka"]["access"] == "consume"
kafka_span = get_first_span_by_filter(spans, filter)
def filter(span):
return span.n == "sdk" and span.data["sdk"]["name"] == "test"
test_span = get_first_span_by_filter(spans, filter)
# Same traceId
assert test_span.t == kafka_span.t
# Parent relationships
assert kafka_span.p == test_span.s
# Error logging
assert not test_span.ec
assert kafka_span.ec == 1
assert kafka_span.n == "kafka"
assert kafka_span.k == SpanKind.SERVER
assert kafka_span.data["kafka"]["service"] == "inexistent_kafka_topic"
assert kafka_span.data["kafka"]["access"] == "consume"
assert (
kafka_span.data["kafka"]["error"]
== "'NoneType' object has no attribute 'poll'"
)
def consume_from_topic(self, topic_name: str) -> None:
consumer = KafkaConsumer(
topic_name,
bootstrap_servers=testenv["kafka_bootstrap_servers"],
auto_offset_reset="earliest",
enable_auto_commit=False,
consumer_timeout_ms=1000,
)
with tracer.start_as_current_span("test"):
for msg in consumer:
if msg is None:
break
consumer.close()
@patch.dict(os.environ, {"INSTANA_IGNORE_ENDPOINTS": "kafka"})
def test_ignore_kafka(self) -> None:
agent.options.set_trace_configurations()
with tracer.start_as_current_span("test"):
self.producer.send(testenv["kafka_topic"], b"raw_bytes")
self.producer.flush()
spans = self.recorder.queued_spans()
assert len(spans) == 2
filtered_spans = agent.filter_spans(spans)
assert len(filtered_spans) == 1
@patch.dict(os.environ, {"INSTANA_IGNORE_ENDPOINTS": "kafka:send"})
def test_ignore_kafka_producer(self) -> None:
agent.options.set_trace_configurations()
with tracer.start_as_current_span("test-span"):
# Produce some events
self.producer.send(testenv["kafka_topic"], b"raw_bytes1")
self.producer.send(testenv["kafka_topic"], b"raw_bytes2")
self.producer.flush()
# Consume the events manually
# consume_from_topic not used due to to not create sdk span
consumer = KafkaConsumer(
testenv["kafka_topic"],
bootstrap_servers=testenv["kafka_bootstrap_servers"],
auto_offset_reset="earliest",
enable_auto_commit=False,
consumer_timeout_ms=1000,
)
for msg in consumer:
if msg is None:
break
consumer.close()
spans = self.recorder.queued_spans()
assert len(spans) == 3
filtered_spans = agent.filter_spans(spans)
assert len(filtered_spans) == 1
@patch.dict(os.environ, {"INSTANA_IGNORE_ENDPOINTS": "kafka:consume"})
def test_ignore_kafka_consumer(self) -> None:
agent.options.set_trace_configurations()
# Produce some events
self.producer.send(testenv["kafka_topic"], b"raw_bytes1")
self.producer.send(testenv["kafka_topic"], b"raw_bytes2")
self.producer.flush()
# Consume the events
self.consume_from_topic(testenv["kafka_topic"])
spans = self.recorder.queued_spans()
assert len(spans) == 1
@patch.dict(
os.environ,
{
"INSTANA_IGNORE_ENDPOINTS_PATH": "tests/util/test_configuration-1.yaml",
},
)
def test_ignore_specific_topic(self) -> None:
agent.options.set_trace_configurations()
with tracer.start_as_current_span("test-span"):
# Produce some events
self.producer.send(testenv["kafka_topic"], b"raw_bytes1")
self.producer.send(testenv["kafka_topic"] + "_1", b"raw_bytes1")
self.producer.flush()
# Consume the events
self.consume_from_topic(testenv["kafka_topic"])
self.consume_from_topic(testenv["kafka_topic"] + "_1")
spans = self.recorder.queued_spans()
assert len(spans) == 7
filtered_spans = agent.filter_spans(spans)
assert len(filtered_spans) == 6
span_to_be_filtered = get_first_span_by_filter(
spans,
lambda span: span.n == "kafka"
and span.data["kafka"]["service"] == "span-topic",
)
assert span_to_be_filtered not in filtered_spans
def test_ignore_specific_topic_with_config_file(self) -> None:
agent.options.ignore_endpoints = parse_ignored_endpoints_from_yaml(
"tests/util/test_configuration-1.yaml"
)
# Produce some events
self.producer.send(testenv["kafka_topic"], b"raw_bytes1")
self.producer.flush()
# Consume the events
self.consume_from_topic(testenv["kafka_topic"])
spans = self.recorder.queued_spans()
assert len(spans) == 1
def test_kafka_consumer_root_exit(self) -> None:
agent.options.allow_exit_as_root = True
self.producer.send(testenv["kafka_topic"], b"raw_bytes")
self.producer.flush()
# Consume the events
consumer = KafkaConsumer(
testenv["kafka_topic"],
bootstrap_servers=testenv["kafka_bootstrap_servers"],
auto_offset_reset="earliest", # consume earliest available messages
enable_auto_commit=False, # do not auto-commit offsets
consumer_timeout_ms=1000,
)
for msg in consumer:
if msg is None:
break
consumer.close()
spans = self.recorder.queued_spans()
assert len(spans) == 3
producer_span = spans[0]
consumer_span = spans[1]
assert producer_span.s
assert producer_span.n == "kafka"
assert producer_span.data["kafka"]["access"] == "send"
assert producer_span.data["kafka"]["service"] == "span-topic"
assert consumer_span.s
assert consumer_span.n == "kafka"
assert consumer_span.data["kafka"]["access"] == "consume"
assert consumer_span.data["kafka"]["service"] == "span-topic"
assert producer_span.t == consumer_span.t
def test_kafka_poll_root_exit_with_trace_correlation(self) -> None:
agent.options.allow_exit_as_root = True
self.producer.send(testenv["kafka_topic"] + "_1", b"raw_bytes1")
self.producer.send(testenv["kafka_topic"] + "_2", b"raw_bytes2")
self.producer.send(testenv["kafka_topic"] + "_3", b"raw_bytes3")
self.producer.flush()
# Consume the events
consumer = KafkaConsumer(
bootstrap_servers=testenv["kafka_bootstrap_servers"],
auto_offset_reset="earliest", # consume earliest available messages
enable_auto_commit=False, # do not auto-commit offsets
consumer_timeout_ms=1000,
)
topics = [
testenv["kafka_topic"] + "_1",
testenv["kafka_topic"] + "_2",
testenv["kafka_topic"] + "_3",
]
consumer.subscribe(topics)
messages = consumer.poll(timeout_ms=1000) # noqa: F841
consumer.close()
spans = self.recorder.queued_spans()
assert len(spans) == 6
producer_span_1 = get_first_span_by_filter(
spans,
lambda span: span.n == "kafka"
and span.data["kafka"]["access"] == "send"
and span.data["kafka"]["service"] == "span-topic_1",
)
producer_span_2 = get_first_span_by_filter(
spans,
lambda span: span.n == "kafka"
and span.data["kafka"]["access"] == "send"
and span.data["kafka"]["service"] == "span-topic_2",
)
producer_span_3 = get_first_span_by_filter(
spans,
lambda span: span.n == "kafka"
and span.data["kafka"]["access"] == "send"
and span.data["kafka"]["service"] == "span-topic_3",
)
poll_span_1 = get_first_span_by_filter(
spans,
lambda span: span.n == "kafka"
and span.data["kafka"]["access"] == "poll"
and span.data["kafka"]["service"] == "span-topic_1",
)
poll_span_2 = get_first_span_by_filter(
spans,
lambda span: span.n == "kafka"
and span.data["kafka"]["access"] == "poll"
and span.data["kafka"]["service"] == "span-topic_2",
)
poll_span_3 = get_first_span_by_filter(
spans,
lambda span: span.n == "kafka"
and span.data["kafka"]["access"] == "poll"
and span.data["kafka"]["service"] == "span-topic_3",
)
assert producer_span_1.n == "kafka"
assert producer_span_1.data["kafka"]["access"] == "send"
assert producer_span_1.data["kafka"]["service"] == "span-topic_1"
assert producer_span_2.n == "kafka"
assert producer_span_2.data["kafka"]["access"] == "send"
assert producer_span_2.data["kafka"]["service"] == "span-topic_2"
assert producer_span_3.n == "kafka"
assert producer_span_3.data["kafka"]["access"] == "send"
assert producer_span_3.data["kafka"]["service"] == "span-topic_3"
assert poll_span_1.n == "kafka"
assert poll_span_1.data["kafka"]["access"] == "poll"
assert poll_span_1.data["kafka"]["service"] == "span-topic_1"
assert poll_span_2.n == "kafka"
assert poll_span_2.data["kafka"]["access"] == "poll"
assert poll_span_2.data["kafka"]["service"] == "span-topic_2"
assert poll_span_3.n == "kafka"
assert poll_span_3.data["kafka"]["access"] == "poll"
assert poll_span_3.data["kafka"]["service"] == "span-topic_3"
# same trace id, different span ids
assert producer_span_1.t == poll_span_1.t
assert producer_span_1.s != poll_span_1.s
assert producer_span_2.t == poll_span_2.t
assert producer_span_2.s != poll_span_2.s
assert producer_span_3.t == poll_span_3.t
assert producer_span_3.s != poll_span_3.s
def test_kafka_poll_root_exit_without_trace_correlation(self) -> None:
agent.options.allow_exit_as_root = True
agent.options.kafka_trace_correlation = False
self.producer.send(testenv["kafka_topic"] + "_1", b"raw_bytes1")
self.producer.send(testenv["kafka_topic"] + "_2", b"raw_bytes2")
self.producer.send(testenv["kafka_topic"] + "_3", b"raw_bytes3")
self.producer.flush()
# Consume the events
consumer = KafkaConsumer(
bootstrap_servers=testenv["kafka_bootstrap_servers"],
auto_offset_reset="earliest", # consume earliest available messages
enable_auto_commit=False, # do not auto-commit offsets
consumer_timeout_ms=1000,
)
topics = [
testenv["kafka_topic"] + "_1",
testenv["kafka_topic"] + "_2",
testenv["kafka_topic"] + "_3",
]
consumer.subscribe(topics)
messages = consumer.poll(timeout_ms=1000) # noqa: F841
consumer.close()
spans = self.recorder.queued_spans()
assert len(spans) == 6
producer_span_1 = get_first_span_by_filter(
spans,
lambda span: span.n == "kafka"
and span.data["kafka"]["access"] == "send"
and span.data["kafka"]["service"] == "span-topic_1",
)
producer_span_2 = get_first_span_by_filter(
spans,
lambda span: span.n == "kafka"
and span.data["kafka"]["access"] == "send"
and span.data["kafka"]["service"] == "span-topic_2",
)
producer_span_3 = get_first_span_by_filter(
spans,
lambda span: span.n == "kafka"
and span.data["kafka"]["access"] == "send"
and span.data["kafka"]["service"] == "span-topic_3",
)
poll_span_1 = get_first_span_by_filter(
spans,
lambda span: span.n == "kafka"
and span.data["kafka"]["access"] == "poll"
and span.data["kafka"]["service"] == "span-topic_1",
)
poll_span_2 = get_first_span_by_filter(
spans,
lambda span: span.n == "kafka"
and span.data["kafka"]["access"] == "poll"
and span.data["kafka"]["service"] == "span-topic_2",
)
poll_span_3 = get_first_span_by_filter(
spans,
lambda span: span.n == "kafka"
and span.data["kafka"]["access"] == "poll"
and span.data["kafka"]["service"] == "span-topic_3",
)
assert producer_span_1.n == "kafka"
assert producer_span_1.data["kafka"]["access"] == "send"
assert producer_span_1.data["kafka"]["service"] == "span-topic_1"
assert producer_span_2.n == "kafka"
assert producer_span_2.data["kafka"]["access"] == "send"
assert producer_span_2.data["kafka"]["service"] == "span-topic_2"
assert producer_span_3.n == "kafka"
assert producer_span_3.data["kafka"]["access"] == "send"
assert producer_span_3.data["kafka"]["service"] == "span-topic_3"
assert poll_span_1.n == "kafka"
assert poll_span_1.data["kafka"]["access"] == "poll"
assert poll_span_1.data["kafka"]["service"] == "span-topic_1"
assert poll_span_2.n == "kafka"
assert poll_span_2.data["kafka"]["access"] == "poll"
assert poll_span_2.data["kafka"]["service"] == "span-topic_2"
assert poll_span_3.n == "kafka"
assert poll_span_3.data["kafka"]["access"] == "poll"
assert poll_span_3.data["kafka"]["service"] == "span-topic_3"
# different trace id and span ids
assert producer_span_1.t != poll_span_1.t
assert producer_span_1.s != poll_span_1.s
assert producer_span_2.t != poll_span_2.t
assert producer_span_2.s != poll_span_2.s
assert producer_span_3.t != poll_span_3.t
assert producer_span_3.s != poll_span_3.s
for topic_partition, partition_messages in messages.items():
for message in partition_messages:
assert not message.headers
@patch.dict(os.environ, {"INSTANA_ALLOW_ROOT_EXIT_SPAN": "1"})
def test_kafka_downstream_suppression(self) -> None:
config["tracing"]["ignore_endpoints"] = {
"kafka": [
{"methods": ["send"], "endpoints": [f"{testenv['kafka_topic']}_1"]},
{
"methods": ["consume"],
"endpoints": [f"{testenv['kafka_topic']}_2"],
},
]
}
agent.options.set_trace_configurations()
self.producer.send(testenv["kafka_topic"] + "_1", b"raw_bytes1")
self.producer.send(testenv["kafka_topic"] + "_2", b"raw_bytes2")
self.producer.send(testenv["kafka_topic"] + "_3", b"raw_bytes3")
self.producer.flush()
# Consume the events
consumer = KafkaConsumer(
bootstrap_servers=testenv["kafka_bootstrap_servers"],
auto_offset_reset="earliest", # consume earliest available messages
enable_auto_commit=False, # do not auto-commit offsets
consumer_timeout_ms=1000,
)
topics = [
testenv["kafka_topic"] + "_1",
testenv["kafka_topic"] + "_2",
testenv["kafka_topic"] + "_3",
]
consumer.subscribe(topics)
messages = consumer.poll(timeout_ms=1000) # noqa: F841
consumer.close()
spans = self.recorder.queued_spans()
assert len(spans) == 5
producer_span_1 = get_first_span_by_filter(
spans,
lambda span: span.n == "kafka"
and span.data["kafka"]["access"] == "send"
and span.data["kafka"]["service"] == "span-topic_1",
)
producer_span_2 = get_first_span_by_filter(
spans,
lambda span: span.n == "kafka"
and span.data["kafka"]["access"] == "send"
and span.data["kafka"]["service"] == "span-topic_2",
)
producer_span_3 = get_first_span_by_filter(
spans,
lambda span: span.n == "kafka"
and span.data["kafka"]["access"] == "send"
and span.data["kafka"]["service"] == "span-topic_3",
)
poll_span_2 = get_first_span_by_filter(
spans,
lambda span: span.n == "kafka"
and span.data["kafka"]["access"] == "poll"
and span.data["kafka"]["service"] == "span-topic_2",
)
poll_span_3 = get_first_span_by_filter(
spans,
lambda span: span.n == "kafka"
and span.data["kafka"]["access"] == "poll"
and span.data["kafka"]["service"] == "span-topic_3",
)
assert producer_span_1.n == "kafka"
assert producer_span_1.data["kafka"]["access"] == "send"
assert producer_span_1.data["kafka"]["service"] == "span-topic_1"
assert producer_span_2.n == "kafka"
assert producer_span_2.data["kafka"]["access"] == "send"
assert producer_span_2.data["kafka"]["service"] == "span-topic_2"
assert producer_span_3.n == "kafka"
assert producer_span_3.data["kafka"]["access"] == "send"
assert producer_span_3.data["kafka"]["service"] == "span-topic_3"
assert poll_span_2.n == "kafka"
assert poll_span_2.data["kafka"]["access"] == "poll"
assert poll_span_2.data["kafka"]["service"] == "span-topic_2"
assert poll_span_3.n == "kafka"
assert poll_span_3.data["kafka"]["access"] == "poll"
assert poll_span_3.data["kafka"]["service"] == "span-topic_3"
# same trace id, different span ids
assert producer_span_2.t == poll_span_2.t
assert producer_span_2.s != poll_span_2.s
assert producer_span_3.t == poll_span_3.t
assert producer_span_3.s != poll_span_3.s
for topic_partition, partition_messages in messages.items():
for message in partition_messages:
if message.topic == "span-topic_1":
assert message.headers == [("x_instana_l_s", b"0")]
elif message.topic == "span-topic_2":
assert message.headers == [
("x_instana_l_s", b"1"),
(
"x_instana_t",
format_span_id(producer_span_2.t).encode("utf-8"),
),
(
"x_instana_s",
format_span_id(producer_span_2.s).encode("utf-8"),
),
]
def test_save_consumer_span_into_context(self, span: "InstanaSpan") -> None:
"""Test save_consumer_span_into_context function."""
# Verify initial state
assert consumer_span.get(None) is None
assert kafka_python.consumer_token is None
# Save span into context
save_consumer_span_into_context(span)
# Verify span is saved in context variable
assert consumer_span.get(None) == span
# Verify token is stored
assert kafka_python.consumer_token is not None
def test_close_consumer_span_recording_span(self, span: "InstanaSpan") -> None:
"""Test close_consumer_span with a recording span."""
# Save span into context first
save_consumer_span_into_context(span)
assert kafka_python.consumer_token is not None
# Verify span is recording
assert span.is_recording()
# Close the span
close_consumer_span(span)
# Verify span was ended and context cleared
assert not span.is_recording()
assert consumer_span.get(None) is None
assert kafka_python.consumer_token is None
def test_clear_context(self, span: "InstanaSpan") -> None:
"""Test clear_context function."""
# Save span into context
save_consumer_span_into_context(span)
# Verify context has data
assert consumer_span.get(None) == span
assert kafka_python.consumer_token is not None
# Clear context
clear_context()
# Verify all context is cleared
assert consumer_span.get(None) is None
assert kafka_python.consumer_token is None