forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_redis.py
More file actions
529 lines (445 loc) · 18.4 KB
/
Copy pathtest_redis.py
File metadata and controls
529 lines (445 loc) · 18.4 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
from unittest import mock
import pytest
from fakeredis import FakeStrictRedis
import sentry_sdk
from sentry_sdk import capture_message, start_transaction
from sentry_sdk.consts import SPANDATA
from sentry_sdk.integrations.redis import RedisIntegration
MOCK_CONNECTION_POOL = mock.MagicMock()
MOCK_CONNECTION_POOL.connection_kwargs = {
"host": "localhost",
"port": 63791,
"db": 1,
}
def test_basic(sentry_init, capture_events):
sentry_init(integrations=[RedisIntegration()])
events = capture_events()
connection = FakeStrictRedis()
connection.get("foobar")
capture_message("hi")
(event,) = events
(crumb,) = event["breadcrumbs"]["values"]
assert crumb == {
"category": "redis",
"message": "GET 'foobar'",
"data": {
"redis.key": "foobar",
"redis.command": "GET",
"redis.is_cluster": False,
"db.operation": "GET",
},
"timestamp": crumb["timestamp"],
"type": "redis",
}
@pytest.mark.parametrize("span_streaming", [True, False])
@pytest.mark.parametrize(
"is_transaction, send_default_pii, expected_first_ten",
[
(False, False, ["GET 'foo'", "SET 'bar' [Filtered]", "SET 'baz' [Filtered]"]),
(True, True, ["GET 'foo'", "SET 'bar' 1", "SET 'baz' 2"]),
],
)
def test_redis_pipeline(
sentry_init,
capture_events,
capture_items,
is_transaction,
send_default_pii,
expected_first_ten,
span_streaming,
):
sentry_init(
integrations=[RedisIntegration()],
traces_sample_rate=1.0,
send_default_pii=send_default_pii,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
connection = FakeStrictRedis()
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="custom parent"):
pipeline = connection.pipeline(transaction=is_transaction)
pipeline.get("foo")
pipeline.set("bar", 1)
pipeline.set("baz", 2)
pipeline.execute()
sentry_sdk.flush()
assert len(items) == 2
pipeline_span, parent_span = items[0].payload, items[1].payload
assert parent_span["name"] == "custom parent"
assert parent_span["is_segment"] is True
assert pipeline_span["name"] == "redis.pipeline.execute"
assert pipeline_span["attributes"]["sentry.op"] == "db.redis"
assert pipeline_span["attributes"]["sentry.origin"] == "auto.db.redis"
assert pipeline_span["attributes"][SPANDATA.DB_SYSTEM_NAME] == "redis"
else:
events = capture_events()
with start_transaction():
pipeline = connection.pipeline(transaction=is_transaction)
pipeline.get("foo")
pipeline.set("bar", 1)
pipeline.set("baz", 2)
pipeline.execute()
(event,) = events
(span,) = event["spans"]
assert span["op"] == "db.redis"
assert span["description"] == "redis.pipeline.execute"
assert span["data"][SPANDATA.DB_SYSTEM] == "redis"
assert span["data"]["redis.commands"] == {
"count": 3,
"first_ten": expected_first_ten,
}
assert span["tags"] == {
"redis.transaction": is_transaction,
"redis.is_cluster": False,
}
@pytest.mark.parametrize("span_streaming", [True, False])
def test_sensitive_data(sentry_init, capture_events, capture_items, span_streaming):
# fakeredis does not support the AUTH command, so we need to mock it
with mock.patch(
"sentry_sdk.integrations.redis.utils._COMMANDS_INCLUDING_SENSITIVE_DATA",
["get"],
):
sentry_init(
integrations=[RedisIntegration()],
traces_sample_rate=1.0,
send_default_pii=True,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
connection = FakeStrictRedis()
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="custom parent"):
connection.get("this is super secret")
sentry_sdk.flush()
assert len(items) == 2
redis_span, parent_span = items[0].payload, items[1].payload
assert parent_span["name"] == "custom parent"
assert redis_span["name"] == "GET [Filtered]"
assert redis_span["attributes"]["sentry.op"] == "db.redis"
else:
events = capture_events()
with start_transaction():
connection.get(
"this is super secret"
) # because fakeredis does not support AUTH we use GET instead
(event,) = events
spans = event["spans"]
assert spans[0]["op"] == "db.redis"
assert spans[0]["description"] == "GET [Filtered]"
@pytest.mark.parametrize("span_streaming", [True, False])
def test_pii_data_redacted(sentry_init, capture_events, capture_items, span_streaming):
sentry_init(
integrations=[RedisIntegration()],
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
connection = FakeStrictRedis()
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="custom parent"):
connection.set("somekey1", "my secret string1")
connection.set("somekey2", "my secret string2")
connection.get("somekey2")
connection.delete("somekey1", "somekey2")
sentry_sdk.flush()
assert len(items) == 5
set1, set2, get, delete, parent = [item.payload for item in items]
assert parent["name"] == "custom parent"
assert set1["name"] == "SET 'somekey1' [Filtered]"
assert set1["attributes"]["sentry.op"] == "db.redis"
assert set2["name"] == "SET 'somekey2' [Filtered]"
assert get["name"] == "GET 'somekey2'"
assert delete["name"] == "DEL 'somekey1' [Filtered]"
else:
events = capture_events()
with start_transaction():
connection.set("somekey1", "my secret string1")
connection.set("somekey2", "my secret string2")
connection.get("somekey2")
connection.delete("somekey1", "somekey2")
(event,) = events
spans = event["spans"]
assert spans[0]["op"] == "db.redis"
assert spans[0]["description"] == "SET 'somekey1' [Filtered]"
assert spans[1]["description"] == "SET 'somekey2' [Filtered]"
assert spans[2]["description"] == "GET 'somekey2'"
assert spans[3]["description"] == "DEL 'somekey1' [Filtered]"
@pytest.mark.parametrize("span_streaming", [True, False])
def test_pii_data_sent(sentry_init, capture_events, capture_items, span_streaming):
sentry_init(
integrations=[RedisIntegration()],
traces_sample_rate=1.0,
send_default_pii=True,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
connection = FakeStrictRedis()
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="custom parent"):
connection.set("somekey1", "my secret string1")
connection.set("somekey2", "my secret string2")
connection.get("somekey2")
connection.delete("somekey1", "somekey2")
sentry_sdk.flush()
assert len(items) == 5
set1, set2, get, delete, parent = [item.payload for item in items]
assert parent["name"] == "custom parent"
assert set1["name"] == "SET 'somekey1' 'my secret string1'"
assert set1["attributes"]["sentry.op"] == "db.redis"
assert set2["name"] == "SET 'somekey2' 'my secret string2'"
assert get["name"] == "GET 'somekey2'"
assert delete["name"] == "DEL 'somekey1' 'somekey2'"
else:
events = capture_events()
with start_transaction():
connection.set("somekey1", "my secret string1")
connection.set("somekey2", "my secret string2")
connection.get("somekey2")
connection.delete("somekey1", "somekey2")
(event,) = events
spans = event["spans"]
assert spans[0]["op"] == "db.redis"
assert spans[0]["description"] == "SET 'somekey1' 'my secret string1'"
assert spans[1]["description"] == "SET 'somekey2' 'my secret string2'"
assert spans[2]["description"] == "GET 'somekey2'"
assert spans[3]["description"] == "DEL 'somekey1' 'somekey2'"
@pytest.mark.parametrize("span_streaming", [True, False])
def test_no_data_truncation_by_default(
sentry_init, capture_events, capture_items, span_streaming
):
sentry_init(
integrations=[RedisIntegration()],
traces_sample_rate=1.0,
send_default_pii=True,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
connection = FakeStrictRedis()
long_string = "a" * 100000
short_string = "b" * 10
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="custom parent"):
connection.set("somekey1", long_string)
connection.set("somekey2", short_string)
sentry_sdk.flush()
assert len(items) == 3
set1, set2, parent = [item.payload for item in items]
assert parent["name"] == "custom parent"
assert set1["name"] == f"SET 'somekey1' '{long_string}'"
assert set1["attributes"]["sentry.op"] == "db.redis"
assert set2["name"] == f"SET 'somekey2' '{short_string}'"
else:
events = capture_events()
with start_transaction():
connection.set("somekey1", long_string)
connection.set("somekey2", short_string)
(event,) = events
spans = event["spans"]
assert spans[0]["op"] == "db.redis"
assert spans[0]["description"] == f"SET 'somekey1' '{long_string}'"
assert spans[1]["description"] == f"SET 'somekey2' '{short_string}'"
@pytest.mark.parametrize("span_streaming", [True, False])
def test_data_truncation_custom(
sentry_init, capture_events, capture_items, span_streaming
):
sentry_init(
integrations=[RedisIntegration(max_data_size=30)],
traces_sample_rate=1.0,
send_default_pii=True,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
connection = FakeStrictRedis()
long_string = "a" * 100000
short_string = "b" * 10
expected_long = "SET 'somekey1' '%s..." % (
long_string[: 30 - len("...") - len("SET 'somekey1' '")],
)
expected_short = "SET 'somekey2' '%s'" % (short_string,)
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="custom parent"):
connection.set("somekey1", long_string)
connection.set("somekey2", short_string)
sentry_sdk.flush()
assert len(items) == 3
set1, set2, parent = [item.payload for item in items]
assert parent["name"] == "custom parent"
assert set1["name"] == expected_long
assert set1["attributes"]["sentry.op"] == "db.redis"
assert set2["name"] == expected_short
else:
events = capture_events()
with start_transaction():
connection.set("somekey1", long_string)
connection.set("somekey2", short_string)
(event,) = events
spans = event["spans"]
assert spans[0]["op"] == "db.redis"
assert spans[0]["description"] == expected_long
assert spans[1]["description"] == expected_short
def test_breadcrumbs(sentry_init, capture_events):
sentry_init(
integrations=[RedisIntegration(max_data_size=30)],
send_default_pii=True,
)
events = capture_events()
connection = FakeStrictRedis()
long_string = "a" * 100000
connection.set("somekey1", long_string)
short_string = "b" * 10
connection.set("somekey2", short_string)
capture_message("hi")
(event,) = events
crumbs = event["breadcrumbs"]["values"]
assert crumbs[0] == {
"message": "SET 'somekey1' 'aaaaaaaaaaa...",
"type": "redis",
"category": "redis",
"data": {
"db.operation": "SET",
"redis.is_cluster": False,
"redis.command": "SET",
"redis.key": "somekey1",
},
"timestamp": crumbs[0]["timestamp"],
}
assert crumbs[1] == {
"message": "SET 'somekey2' 'bbbbbbbbbb'",
"type": "redis",
"category": "redis",
"data": {
"db.operation": "SET",
"redis.is_cluster": False,
"redis.command": "SET",
"redis.key": "somekey2",
},
"timestamp": crumbs[1]["timestamp"],
}
@pytest.mark.parametrize("span_streaming", [True, False])
def test_db_connection_attributes_client(
sentry_init, capture_events, capture_items, span_streaming
):
sentry_init(
traces_sample_rate=1.0,
integrations=[RedisIntegration()],
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="custom parent"):
connection = FakeStrictRedis(connection_pool=MOCK_CONNECTION_POOL)
connection.get("foobar")
sentry_sdk.flush()
assert len(items) == 2
redis_span, parent_span = items[0].payload, items[1].payload
assert parent_span["name"] == "custom parent"
assert redis_span["name"] == "GET 'foobar'"
attrs = redis_span["attributes"]
assert attrs["sentry.op"] == "db.redis"
assert attrs[SPANDATA.DB_SYSTEM_NAME] == "redis"
assert attrs[SPANDATA.DB_DRIVER_NAME] == "redis-py"
assert attrs[SPANDATA.DB_NAMESPACE] == "1"
assert attrs[SPANDATA.SERVER_ADDRESS] == "localhost"
assert attrs[SPANDATA.SERVER_PORT] == 63791
else:
events = capture_events()
with start_transaction():
connection = FakeStrictRedis(connection_pool=MOCK_CONNECTION_POOL)
connection.get("foobar")
(event,) = events
(span,) = event["spans"]
assert span["op"] == "db.redis"
assert span["description"] == "GET 'foobar'"
assert span["data"][SPANDATA.DB_SYSTEM] == "redis"
assert span["data"][SPANDATA.DB_DRIVER_NAME] == "redis-py"
assert span["data"][SPANDATA.DB_NAME] == "1"
assert span["data"][SPANDATA.SERVER_ADDRESS] == "localhost"
assert span["data"][SPANDATA.SERVER_PORT] == 63791
@pytest.mark.parametrize("span_streaming", [True, False])
def test_db_connection_attributes_pipeline(
sentry_init, capture_events, capture_items, span_streaming
):
sentry_init(
traces_sample_rate=1.0,
integrations=[RedisIntegration()],
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="custom parent"):
connection = FakeStrictRedis(connection_pool=MOCK_CONNECTION_POOL)
pipeline = connection.pipeline(transaction=False)
pipeline.get("foo")
pipeline.set("bar", 1)
pipeline.set("baz", 2)
pipeline.execute()
sentry_sdk.flush()
assert len(items) == 2
pipeline_span, parent_span = items[0].payload, items[1].payload
assert parent_span["name"] == "custom parent"
assert pipeline_span["name"] == "redis.pipeline.execute"
attrs = pipeline_span["attributes"]
assert attrs["sentry.op"] == "db.redis"
assert attrs[SPANDATA.DB_SYSTEM_NAME] == "redis"
assert attrs[SPANDATA.DB_DRIVER_NAME] == "redis-py"
assert attrs[SPANDATA.DB_NAMESPACE] == "1"
assert attrs[SPANDATA.SERVER_ADDRESS] == "localhost"
assert attrs[SPANDATA.SERVER_PORT] == 63791
else:
events = capture_events()
with start_transaction():
connection = FakeStrictRedis(connection_pool=MOCK_CONNECTION_POOL)
pipeline = connection.pipeline(transaction=False)
pipeline.get("foo")
pipeline.set("bar", 1)
pipeline.set("baz", 2)
pipeline.execute()
(event,) = events
(span,) = event["spans"]
assert span["op"] == "db.redis"
assert span["description"] == "redis.pipeline.execute"
assert span["data"][SPANDATA.DB_SYSTEM] == "redis"
assert span["data"][SPANDATA.DB_DRIVER_NAME] == "redis-py"
assert span["data"][SPANDATA.DB_NAME] == "1"
assert span["data"][SPANDATA.SERVER_ADDRESS] == "localhost"
assert span["data"][SPANDATA.SERVER_PORT] == 63791
@pytest.mark.parametrize("span_streaming", [True, False])
def test_span_origin(sentry_init, capture_events, capture_items, span_streaming):
sentry_init(
integrations=[RedisIntegration()],
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
connection = FakeStrictRedis()
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="custom parent"):
# default case
connection.set("somekey", "somevalue")
# pipeline
pipeline = connection.pipeline(transaction=False)
pipeline.get("somekey")
pipeline.set("anotherkey", 1)
pipeline.execute()
sentry_sdk.flush()
assert len(items) == 3
set_span, pipeline_span, parent_span = [item.payload for item in items]
assert parent_span["name"] == "custom parent"
assert parent_span["attributes"]["sentry.origin"] == "manual"
assert set_span["attributes"]["sentry.origin"] == "auto.db.redis"
assert pipeline_span["attributes"]["sentry.origin"] == "auto.db.redis"
else:
events = capture_events()
with start_transaction(name="custom_transaction"):
# default case
connection.set("somekey", "somevalue")
# pipeline
pipeline = connection.pipeline(transaction=False)
pipeline.get("somekey")
pipeline.set("anotherkey", 1)
pipeline.execute()
(event,) = events
assert event["contexts"]["trace"]["origin"] == "manual"
for span in event["spans"]:
assert span["origin"] == "auto.db.redis"