forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tornado.py
More file actions
577 lines (451 loc) · 17.9 KB
/
Copy pathtest_tornado.py
File metadata and controls
577 lines (451 loc) · 17.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
import json
import pytest
from tornado.testing import AsyncHTTPTestCase
from tornado.web import Application, HTTPError, RequestHandler
import sentry_sdk
from sentry_sdk import capture_message, start_transaction
from sentry_sdk.integrations.tornado import TornadoIntegration
@pytest.fixture
def tornado_testcase(request):
# Take the unittest class provided by tornado and manually call its setUp
# and tearDown.
#
# The pytest plugins for tornado seem too complicated to use, as they for
# some reason assume I want to write my tests in async code.
def inner(app):
class TestBogus(AsyncHTTPTestCase):
def get_app(self):
return app
def bogustest(self):
# We need to pass a valid test method name to the ctor, so this
# is the method. It does nothing.
pass
self = TestBogus("bogustest")
self.setUp()
request.addfinalizer(self.tearDown)
return self
return inner
class CrashingHandler(RequestHandler):
def get(self):
sentry_sdk.get_isolation_scope().set_tag("foo", "42")
1 / 0
def post(self):
sentry_sdk.get_isolation_scope().set_tag("foo", "43")
1 / 0
class CrashingWithMessageHandler(RequestHandler):
def get(self):
capture_message("hi")
1 / 0
class HelloHandler(RequestHandler):
async def get(self):
sentry_sdk.get_isolation_scope().set_tag("foo", "42")
return b"hello"
async def post(self):
sentry_sdk.get_isolation_scope().set_tag("foo", "43")
return b"hello"
class ChildSpanHandler(RequestHandler):
def get(self):
with sentry_sdk.traces.start_span(name="child-span"):
pass
self.write("ok")
def test_basic(tornado_testcase, sentry_init, capture_events):
sentry_init(integrations=[TornadoIntegration()], send_default_pii=True)
events = capture_events()
client = tornado_testcase(Application([(r"/hi", CrashingHandler)]))
response = client.fetch(
"/hi?foo=bar", headers={"Cookie": "name=value; name2=value2; name3=value3"}
)
assert response.code == 500
(event,) = events
(exception,) = event["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"
assert exception["mechanism"]["type"] == "tornado"
request = event["request"]
host = request["headers"]["Host"]
assert event["request"] == {
"env": {"REMOTE_ADDR": "127.0.0.1"},
"headers": {
"Accept-Encoding": "gzip",
"Connection": "close",
"Cookie": "name=value; name2=value2; name3=value3",
**request["headers"],
},
"cookies": {"name": "value", "name2": "value2", "name3": "value3"},
"method": "GET",
"query_string": "foo=bar",
"url": "http://{host}/hi".format(host=host),
}
assert event["tags"] == {"foo": "42"}
assert (
event["transaction"]
== "tests.integrations.tornado.test_tornado.CrashingHandler.get"
)
assert event["transaction_info"] == {"source": "component"}
assert not sentry_sdk.get_isolation_scope()._tags
@pytest.mark.parametrize("send_pii", [True, False])
@pytest.mark.parametrize("span_streaming", [True, False])
@pytest.mark.parametrize(
"handler,code",
[
(CrashingHandler, 500),
(HelloHandler, 200),
],
)
def test_transactions(
tornado_testcase,
sentry_init,
capture_events,
capture_items,
handler,
code,
span_streaming,
send_pii,
):
sentry_init(
integrations=[TornadoIntegration()],
traces_sample_rate=1.0,
send_default_pii=send_pii,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
if span_streaming:
items = capture_items("event", "span")
else:
events = capture_events()
client = tornado_testcase(Application([(r"/hi", handler)]))
if span_streaming:
with sentry_sdk.traces.start_span(name="client") as span:
request_headers = dict(span._iter_headers())
else:
with start_transaction(name="client") as span:
pass
request_headers = dict(span.iter_headers())
response = client.fetch(
"/hi?foo=bar", method="POST", body=b"heyoo", headers=request_headers
)
assert response.code == code
sentry_sdk.flush()
if span_streaming:
spans = [i.payload for i in items if i.type == "span"]
errors = [i.payload for i in items if i.type == "event"]
client_segment, server_segment = spans
if code == 500:
assert len(errors) == 1
server_error = errors[0]
assert server_error["exception"]["values"][0]["type"] == "ZeroDivisionError"
assert (
server_error["transaction"]
== "tests.integrations.tornado.test_tornado.CrashingHandler.post"
)
assert server_error["transaction_info"] == {"source": "component"}
assert (
server_error["contexts"]["trace"]["trace_id"]
== server_segment["trace_id"]
)
expected_handler = (
"tests.integrations.tornado.test_tornado.HelloHandler.post"
if code == 200
else "tests.integrations.tornado.test_tornado.CrashingHandler.post"
)
assert server_segment["name"] == expected_handler
assert server_segment["attributes"]["sentry.span.source"] == "component"
assert server_segment["attributes"]["http.request.method"] == "POST"
assert server_segment["attributes"]["http.request.body.data"] == "heyoo"
assert server_segment["attributes"]["http.response.status_code"] == code
assert server_segment["status"] == ("ok" if code == 200 else "error")
assert client_segment["trace_id"] == server_segment["trace_id"]
if send_pii:
assert server_segment["attributes"]["url.query"] == "foo=bar"
assert server_segment["attributes"]["url.full"].endswith("/hi?foo=bar")
assert server_segment["attributes"]["url.full"].startswith("http://")
assert server_segment["attributes"]["url.path"] == "/hi"
else:
assert "url.query" not in server_segment["attributes"]
assert "url.full" not in server_segment["attributes"]
assert "url.path" not in server_segment["attributes"]
else:
if code == 200:
client_tx, server_tx = events
server_error = None
else:
client_tx, server_error, server_tx = events
assert client_tx["type"] == "transaction"
assert client_tx["transaction"] == "client"
assert client_tx["transaction_info"] == {
"source": "custom"
} # because this is just the start_transaction() above.
if server_error is not None:
assert server_error["exception"]["values"][0]["type"] == "ZeroDivisionError"
assert (
server_error["transaction"]
== "tests.integrations.tornado.test_tornado.CrashingHandler.post"
)
assert server_error["transaction_info"] == {"source": "component"}
if code == 200:
assert (
server_tx["transaction"]
== "tests.integrations.tornado.test_tornado.HelloHandler.post"
)
else:
assert (
server_tx["transaction"]
== "tests.integrations.tornado.test_tornado.CrashingHandler.post"
)
assert server_tx["transaction_info"] == {"source": "component"}
assert server_tx["type"] == "transaction"
request = server_tx["request"]
host = request["headers"]["Host"]
expected_request = {
"env": {"REMOTE_ADDR": "127.0.0.1"},
"headers": {
"Accept-Encoding": "gzip",
"Connection": "close",
**request["headers"],
},
"method": "POST",
"query_string": "foo=bar",
"data": {"heyoo": [""]},
"url": "http://{host}/hi".format(host=host),
}
if send_pii:
expected_request["cookies"] = {}
assert server_tx["request"] == expected_request
assert (
client_tx["contexts"]["trace"]["trace_id"]
== server_tx["contexts"]["trace"]["trace_id"]
)
if server_error is not None:
assert (
server_error["contexts"]["trace"]["trace_id"]
== server_tx["contexts"]["trace"]["trace_id"]
)
def test_400_not_logged(tornado_testcase, sentry_init, capture_events):
sentry_init(integrations=[TornadoIntegration()])
events = capture_events()
class CrashingHandler(RequestHandler):
def get(self):
raise HTTPError(400, "Oops")
client = tornado_testcase(Application([(r"/", CrashingHandler)]))
response = client.fetch("/")
assert response.code == 400
assert not events
def test_user_auth(tornado_testcase, sentry_init, capture_events):
sentry_init(integrations=[TornadoIntegration()], send_default_pii=True)
events = capture_events()
class UserHandler(RequestHandler):
def get(self):
1 / 0
def get_current_user(self):
return 42
class NoUserHandler(RequestHandler):
def get(self):
1 / 0
client = tornado_testcase(
Application([(r"/auth", UserHandler), (r"/noauth", NoUserHandler)])
)
# has user
response = client.fetch("/auth")
assert response.code == 500
(event,) = events
(exception,) = event["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"
assert event["user"] == {"is_authenticated": True}
events.clear()
# has no user
response = client.fetch("/noauth")
assert response.code == 500
(event,) = events
(exception,) = event["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"
assert "user" not in event
def test_formdata(tornado_testcase, sentry_init, capture_events):
sentry_init(integrations=[TornadoIntegration()], send_default_pii=True)
events = capture_events()
class FormdataHandler(RequestHandler):
def post(self):
raise ValueError(json.dumps(sorted(self.request.body_arguments)))
client = tornado_testcase(Application([(r"/form", FormdataHandler)]))
response = client.fetch(
"/form?queryarg=1",
method="POST",
headers={"Content-Type": "application/x-www-form-urlencoded"},
body=b"field1=value1&field2=value2",
)
assert response.code == 500
(event,) = events
(exception,) = event["exception"]["values"]
assert exception["value"] == '["field1", "field2"]'
assert event["request"]["data"] == {"field1": ["value1"], "field2": ["value2"]}
def test_json(tornado_testcase, sentry_init, capture_events):
sentry_init(integrations=[TornadoIntegration()], send_default_pii=True)
events = capture_events()
class FormdataHandler(RequestHandler):
def post(self):
raise ValueError(json.dumps(sorted(self.request.body_arguments)))
client = tornado_testcase(Application([(r"/form", FormdataHandler)]))
response = client.fetch(
"/form?queryarg=1",
method="POST",
headers={"Content-Type": "application/json"},
body=b"""
{"foo": {"bar": 42}}
""",
)
assert response.code == 500
(event,) = events
(exception,) = event["exception"]["values"]
assert exception["value"] == "[]"
assert event
assert event["request"]["data"] == {"foo": {"bar": 42}}
def test_error_has_new_trace_context_performance_enabled(
tornado_testcase, sentry_init, capture_events
):
"""
Check if an 'trace' context is added to errros and transactions when performance monitoring is enabled.
"""
sentry_init(
integrations=[TornadoIntegration()],
traces_sample_rate=1.0,
)
events = capture_events()
client = tornado_testcase(Application([(r"/hi", CrashingWithMessageHandler)]))
client.fetch("/hi")
(msg_event, error_event, transaction_event) = events
assert "trace" in msg_event["contexts"]
assert "trace_id" in msg_event["contexts"]["trace"]
assert "trace" in error_event["contexts"]
assert "trace_id" in error_event["contexts"]["trace"]
assert "trace" in transaction_event["contexts"]
assert "trace_id" in transaction_event["contexts"]["trace"]
assert (
msg_event["contexts"]["trace"]["trace_id"]
== error_event["contexts"]["trace"]["trace_id"]
== transaction_event["contexts"]["trace"]["trace_id"]
)
def test_error_has_new_trace_context_performance_disabled(
tornado_testcase, sentry_init, capture_events
):
"""
Check if an 'trace' context is added to errros and transactions when performance monitoring is disabled.
"""
sentry_init(
integrations=[TornadoIntegration()],
traces_sample_rate=None, # this is the default, just added for clarity
)
events = capture_events()
client = tornado_testcase(Application([(r"/hi", CrashingWithMessageHandler)]))
client.fetch("/hi")
(msg_event, error_event) = events
assert "trace" in msg_event["contexts"]
assert "trace_id" in msg_event["contexts"]["trace"]
assert "trace" in error_event["contexts"]
assert "trace_id" in error_event["contexts"]["trace"]
assert (
msg_event["contexts"]["trace"]["trace_id"]
== error_event["contexts"]["trace"]["trace_id"]
)
def test_error_has_existing_trace_context_performance_enabled(
tornado_testcase, sentry_init, capture_events
):
"""
Check if an 'trace' context is added to errros and transactions
from the incoming 'sentry-trace' header when performance monitoring is enabled.
"""
sentry_init(
integrations=[TornadoIntegration()],
traces_sample_rate=1.0,
)
events = capture_events()
trace_id = "471a43a4192642f0b136d5159a501701"
parent_span_id = "6e8f22c393e68f19"
parent_sampled = 1
sentry_trace_header = "{}-{}-{}".format(trace_id, parent_span_id, parent_sampled)
headers = {"sentry-trace": sentry_trace_header}
client = tornado_testcase(Application([(r"/hi", CrashingWithMessageHandler)]))
client.fetch("/hi", headers=headers)
(msg_event, error_event, transaction_event) = events
assert "trace" in msg_event["contexts"]
assert "trace_id" in msg_event["contexts"]["trace"]
assert "trace" in error_event["contexts"]
assert "trace_id" in error_event["contexts"]["trace"]
assert "trace" in transaction_event["contexts"]
assert "trace_id" in transaction_event["contexts"]["trace"]
assert (
msg_event["contexts"]["trace"]["trace_id"]
== error_event["contexts"]["trace"]["trace_id"]
== transaction_event["contexts"]["trace"]["trace_id"]
== "471a43a4192642f0b136d5159a501701"
)
def test_error_has_existing_trace_context_performance_disabled(
tornado_testcase, sentry_init, capture_events
):
"""
Check if an 'trace' context is added to errros and transactions
from the incoming 'sentry-trace' header when performance monitoring is disabled.
"""
sentry_init(
integrations=[TornadoIntegration()],
traces_sample_rate=None, # this is the default, just added for clarity
)
events = capture_events()
trace_id = "471a43a4192642f0b136d5159a501701"
parent_span_id = "6e8f22c393e68f19"
parent_sampled = 1
sentry_trace_header = "{}-{}-{}".format(trace_id, parent_span_id, parent_sampled)
headers = {"sentry-trace": sentry_trace_header}
client = tornado_testcase(Application([(r"/hi", CrashingWithMessageHandler)]))
client.fetch("/hi", headers=headers)
(msg_event, error_event) = events
assert "trace" in msg_event["contexts"]
assert "trace_id" in msg_event["contexts"]["trace"]
assert "trace" in error_event["contexts"]
assert "trace_id" in error_event["contexts"]["trace"]
assert (
msg_event["contexts"]["trace"]["trace_id"]
== error_event["contexts"]["trace"]["trace_id"]
== "471a43a4192642f0b136d5159a501701"
)
@pytest.mark.parametrize("span_streaming", [True, False])
def test_span_origin(
tornado_testcase, sentry_init, capture_events, capture_items, span_streaming
):
sentry_init(
integrations=[TornadoIntegration()],
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
if span_streaming:
items = capture_items("span")
else:
events = capture_events()
client = tornado_testcase(Application([(r"/hi", CrashingHandler)]))
client.fetch(
"/hi?foo=bar", headers={"Cookie": "name=value; name2=value2; name3=value3"}
)
sentry_sdk.flush()
if span_streaming:
(segment,) = [i.payload for i in items]
assert segment["attributes"]["sentry.origin"] == "auto.http.tornado"
else:
(_, event) = events
assert event["contexts"]["trace"]["origin"] == "auto.http.tornado"
@pytest.mark.parametrize("send_default_pii", [True, False])
def test_user_ip_address_on_all_spans(
tornado_testcase, sentry_init, capture_items, send_default_pii
):
sentry_init(
integrations=[TornadoIntegration()],
traces_sample_rate=1.0,
send_default_pii=send_default_pii,
_experiments={"trace_lifecycle": "stream"},
)
items = capture_items("span")
client = tornado_testcase(Application([(r"/hi", ChildSpanHandler)]))
client.fetch("/hi")
sentry_sdk.flush()
child_span, server_span = [item.payload for item in items]
if send_default_pii:
assert server_span["attributes"]["user.ip_address"] == "127.0.0.1"
assert child_span["attributes"]["user.ip_address"] == "127.0.0.1"
else:
assert "user.ip_address" not in server_span["attributes"]
assert "user.ip_address" not in child_span["attributes"]