forked from elastic/apm-agent-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkafka_tests.py
262 lines (218 loc) · 10.2 KB
/
kafka_tests.py
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
# BSD 3-Clause License
#
# Copyright (c) 2022, Elasticsearch BV
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import datetime
import os
import threading
import time
import pytest
import elasticapm
from elasticapm.conf.constants import SPAN, TRANSACTION
kafka = pytest.importorskip("kafka")
from kafka import KafkaConsumer, KafkaProducer
from kafka.admin import KafkaAdminClient, NewTopic
pytestmark = [pytest.mark.kafka]
KAFKA_HOST = os.environ.get("KAFKA_HOST")
if not KAFKA_HOST:
pytestmark.append(pytest.mark.skip("Skipping kafka tests, no KAFKA_HOST environment variable set"))
@pytest.fixture(scope="function")
def topics():
topics = ["test", "foo", "bar"]
admin_client = KafkaAdminClient(bootstrap_servers=[f"{KAFKA_HOST}:9092"])
# since kafka-python 2.1.0 we started to get failures in create_topics because topics were already there despite
# calls to delete_topics. In the meantime we found a proper fix use a big hammer and catch topics handling failures
# https://github.com/dpkp/kafka-python/issues/2557
try:
admin_client.create_topics([NewTopic(name, num_partitions=1, replication_factor=1) for name in topics])
except Exception:
pass
yield topics
try:
admin_client.delete_topics(topics)
except Exception:
pass
@pytest.fixture()
def producer():
producer = KafkaProducer(bootstrap_servers=f"{KAFKA_HOST}:9092")
yield producer
@pytest.fixture()
def consumer():
consumer = KafkaConsumer(bootstrap_servers=f"{KAFKA_HOST}:9092", consumer_timeout_ms=500)
consumer.subscribe(topics=["foo", "bar", "test"])
yield consumer
def test_kafka_produce(instrument, elasticapm_client, producer, topics):
elasticapm_client.begin_transaction("test")
producer.send("test", key=b"foo", value=b"bar")
elasticapm_client.end_transaction("test", "success")
transactions = elasticapm_client.events[TRANSACTION]
span = elasticapm_client.events[SPAN][0]
assert span["name"] == "Kafka SEND to test"
assert span["context"]["message"]["queue"]["name"] == "test"
assert span["context"]["destination"]["port"] == 9092
assert span["context"]["destination"]["service"]["name"] == "kafka"
assert span["context"]["destination"]["service"]["resource"] == "kafka/test"
assert span["context"]["destination"]["service"]["type"] == "messaging"
def test_kafka_produce_ignore_topic(instrument, elasticapm_client, producer, topics):
elasticapm_client.config.update("1", ignore_message_queues="foo*,*bar")
elasticapm_client.begin_transaction("test")
producer.send(topic="foo", key=b"foo", value=b"bar")
producer.send("bar", key=b"foo", value=b"bar")
producer.send("test", key=b"foo", value=b"bar")
elasticapm_client.end_transaction("test", "success")
spans = elasticapm_client.events[SPAN]
assert len(spans) == 1
assert spans[0]["name"] == "Kafka SEND to test"
def test_kafka_consume(instrument, elasticapm_client, producer, consumer, topics):
def delayed_send():
time.sleep(0.2)
elasticapm_client.begin_transaction("foo")
producer.send("test", key=b"foo", value=b"bar")
producer.send("test", key=b"baz", value=b"bazzinga")
elasticapm_client.end_transaction("foo")
thread = threading.Thread(target=delayed_send)
thread.start()
for item in consumer:
with elasticapm.capture_span("foo"):
pass
thread.join()
transactions = elasticapm_client.events[TRANSACTION]
spans = elasticapm_client.events[SPAN]
# the consumer transactions should have the same trace id as the transaction that triggered the messages
assert transactions[0]["trace_id"] == transactions[1]["trace_id"] == transactions[2]["trace_id"]
assert transactions[1]["name"] == "Kafka RECEIVE from test"
assert transactions[1]["type"] == "messaging"
assert transactions[1]["context"]["message"]["queue"]["name"] == "test"
assert spans[2]["transaction_id"] == transactions[1]["id"]
assert spans[3]["transaction_id"] == transactions[2]["id"]
def test_kafka_consume_ongoing_transaction(instrument, elasticapm_client, producer, consumer, topics):
def delayed_send():
time.sleep(0.2)
elasticapm_client.begin_transaction("foo")
producer.send("test", key=b"foo", value=b"bar")
producer.send("test", key=b"baz", value=b"bazzinga")
elasticapm_client.end_transaction("foo")
thread = threading.Thread(target=delayed_send)
thread.start()
transaction = elasticapm_client.begin_transaction("foo")
for item in consumer:
pass
thread.join()
elasticapm_client.end_transaction("foo")
transactions = elasticapm_client.events[TRANSACTION]
assert len(transactions) == 2
external_spans = elasticapm_client.spans_for_transaction(transactions[0])
spans = elasticapm_client.spans_for_transaction(transactions[1])
assert len(external_spans) == 2
assert len(spans) == 2
assert spans[0]["links"][0]["trace_id"] == external_spans[0]["trace_id"]
assert spans[1]["links"][0]["trace_id"] == external_spans[1]["trace_id"]
def test_kafka_consumer_ignore_topic(instrument, elasticapm_client, producer, consumer, topics):
elasticapm_client.config.update("1", ignore_message_queues="foo*,*bar")
def delayed_send():
time.sleep(0.2)
producer.send(topic="foo", key=b"foo", value=b"bar")
producer.send("bar", key=b"foo", value=b"bar")
producer.send("test", key=b"foo", value=b"bar")
thread = threading.Thread(target=delayed_send)
thread.start()
for item in consumer:
with elasticapm.capture_span("test"):
assert item
thread.join()
transactions = elasticapm_client.events[TRANSACTION]
assert len(transactions) == 1
assert transactions[0]["name"] == "Kafka RECEIVE from test"
def test_kafka_consumer_ignore_topic_ongoing_transaction(instrument, elasticapm_client, producer, consumer, topics):
elasticapm_client.config.update("1", ignore_message_queues="foo*,*bar")
def delayed_send():
time.sleep(0.2)
producer.send(topic="foo", key=b"foo", value=b"bar")
producer.send("bar", key=b"foo", value=b"bar")
producer.send("test", key=b"foo", value=b"bar")
thread = threading.Thread(target=delayed_send)
thread.start()
transaction = elasticapm_client.begin_transaction("foo")
for item in consumer:
pass
thread.join()
elasticapm_client.end_transaction("foo")
transactions = elasticapm_client.events[TRANSACTION]
spans = elasticapm_client.spans_for_transaction(transactions[0])
assert len(spans) == 1
assert spans[0]["name"] == "Kafka RECEIVE from test"
def test_kafka_poll_ongoing_transaction(instrument, elasticapm_client, producer, consumer, topics):
def delayed_send():
time.sleep(0.2)
producer.send("test", key=b"foo", value=b"bar")
producer.send("test", key=b"baz", value=b"bazzinga")
thread = threading.Thread(target=delayed_send)
thread.start()
transaction = elasticapm_client.begin_transaction("foo")
results = consumer.poll(timeout_ms=1000)
elasticapm_client.end_transaction("foo")
transactions = elasticapm_client.events[TRANSACTION]
spans = elasticapm_client.events[SPAN]
assert len(spans) == 1
assert spans[0]["name"] == "Kafka POLL from bar, foo, test"
def test_kafka_no_client(instrument, producer, consumer, topics):
assert elasticapm.get_client() is None
# the following code shouldn't trigger any errors
producer.send("test", key=b"foo", value=b"bar")
for item in consumer:
pass
def test_kafka_send_unsampled_transaction(instrument, elasticapm_client, producer, topics):
transaction_object = elasticapm_client.begin_transaction("transaction")
transaction_object.is_sampled = False
producer.send("test", key=b"foo", value=b"bar")
elasticapm_client.end_transaction("foo")
spans = elasticapm_client.events[SPAN]
assert len(spans) == 0
def test_kafka_poll_unsampled_transaction(instrument, elasticapm_client, consumer, topics):
transaction_object = elasticapm_client.begin_transaction("transaction")
transaction_object.is_sampled = False
consumer.poll(timeout_ms=50)
elasticapm_client.end_transaction("foo")
spans = elasticapm_client.events[SPAN]
assert len(spans) == 0
def test_kafka_consumer_unsampled_transaction_handles_stop_iteration(
instrument, elasticapm_client, producer, consumer, topics
):
def delayed_send():
time.sleep(0.2)
producer.send("test", key=b"foo", value=b"bar")
thread = threading.Thread(target=delayed_send)
thread.start()
transaction = elasticapm_client.begin_transaction("foo")
transaction.is_sampled = False
for item in consumer:
pass
thread.join()
elasticapm_client.end_transaction("foo")
spans = elasticapm_client.events[SPAN]
assert len(spans) == 0