forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_httpx.py
More file actions
66 lines (55 loc) · 2.36 KB
/
test_httpx.py
File metadata and controls
66 lines (55 loc) · 2.36 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
import asyncio
import httpx
from sentry_sdk import capture_message, start_transaction
from sentry_sdk.integrations.httpx import HttpxIntegration
def test_crumb_capture_and_hint(sentry_init, capture_events):
def before_breadcrumb(crumb, hint):
crumb["data"]["extra"] = "foo"
return crumb
sentry_init(integrations=[HttpxIntegration()], before_breadcrumb=before_breadcrumb)
clients = (httpx.Client(), httpx.AsyncClient())
for i, c in enumerate(clients):
with start_transaction():
events = capture_events()
url = "https://httpbin.org/status/200"
if not asyncio.iscoroutinefunction(c.get):
response = c.get(url)
else:
response = asyncio.get_event_loop().run_until_complete(c.get(url))
assert response.status_code == 200
capture_message("Testing!")
(event,) = events
# send request twice so we need get breadcrumb by index
crumb = event["breadcrumbs"]["values"][i]
assert crumb["type"] == "http"
assert crumb["category"] == "httplib"
assert crumb["data"] == {
"url": url,
"method": "GET",
"status_code": 200,
"reason": "OK",
"extra": "foo",
}
def test_outgoing_trace_headers(sentry_init):
sentry_init(traces_sample_rate=1.0, integrations=[HttpxIntegration()])
clients = (httpx.Client(), httpx.AsyncClient())
for i, c in enumerate(clients):
with start_transaction(
name="/interactions/other-dogs/new-dog",
op="greeting.sniff",
# make trace_id difference between transactions
trace_id=f"012345678901234567890123456789{i}",
) as transaction:
url = "https://httpbin.org/status/200"
if not asyncio.iscoroutinefunction(c.get):
response = c.get(url)
else:
response = asyncio.get_event_loop().run_until_complete(c.get(url))
request_span = transaction._span_recorder.spans[-1]
assert response.request.headers[
"sentry-trace"
] == "{trace_id}-{parent_span_id}-{sampled}".format(
trace_id=transaction.trace_id,
parent_span_id=request_span.span_id,
sampled=1,
)