|
1 | 1 | import asyncio |
2 | 2 |
|
| 3 | +import pytest |
3 | 4 | import httpx |
| 5 | +import responses |
4 | 6 |
|
5 | 7 | from sentry_sdk import capture_message, start_transaction |
6 | 8 | from sentry_sdk.integrations.httpx import HttpxIntegration |
7 | 9 |
|
8 | 10 |
|
9 | | -def test_crumb_capture_and_hint(sentry_init, capture_events): |
| 11 | +@pytest.mark.parametrize( |
| 12 | + "httpx_client", |
| 13 | + (httpx.Client(), httpx.AsyncClient()), |
| 14 | +) |
| 15 | +def test_crumb_capture_and_hint(sentry_init, capture_events, httpx_client): |
10 | 16 | def before_breadcrumb(crumb, hint): |
11 | 17 | crumb["data"]["extra"] = "foo" |
12 | 18 | return crumb |
13 | 19 |
|
14 | 20 | sentry_init(integrations=[HttpxIntegration()], before_breadcrumb=before_breadcrumb) |
15 | | - clients = (httpx.Client(), httpx.AsyncClient()) |
16 | | - for i, c in enumerate(clients): |
17 | | - with start_transaction(): |
18 | | - events = capture_events() |
19 | | - |
20 | | - url = "https://httpbin.org/status/200" |
21 | | - if not asyncio.iscoroutinefunction(c.get): |
22 | | - response = c.get(url) |
23 | | - else: |
24 | | - response = asyncio.get_event_loop().run_until_complete(c.get(url)) |
25 | | - |
26 | | - assert response.status_code == 200 |
27 | | - capture_message("Testing!") |
28 | | - |
29 | | - (event,) = events |
30 | | - # send request twice so we need get breadcrumb by index |
31 | | - crumb = event["breadcrumbs"]["values"][i] |
32 | | - assert crumb["type"] == "http" |
33 | | - assert crumb["category"] == "httplib" |
34 | | - assert crumb["data"] == { |
35 | | - "url": url, |
36 | | - "method": "GET", |
37 | | - "http.fragment": "", |
38 | | - "http.query": "", |
39 | | - "status_code": 200, |
40 | | - "reason": "OK", |
41 | | - "extra": "foo", |
42 | | - } |
43 | | - |
44 | | - |
45 | | -def test_outgoing_trace_headers(sentry_init): |
| 21 | + |
| 22 | + url = "http://example.com/" |
| 23 | + responses.add(responses.GET, url, status=200) |
| 24 | + |
| 25 | + with start_transaction(): |
| 26 | + events = capture_events() |
| 27 | + |
| 28 | + if asyncio.iscoroutinefunction(httpx_client.get): |
| 29 | + response = asyncio.get_event_loop().run_until_complete( |
| 30 | + httpx_client.get(url) |
| 31 | + ) |
| 32 | + else: |
| 33 | + response = httpx_client.get(url) |
| 34 | + |
| 35 | + assert response.status_code == 200 |
| 36 | + capture_message("Testing!") |
| 37 | + |
| 38 | + (event,) = events |
| 39 | + |
| 40 | + crumb = event["breadcrumbs"]["values"][0] |
| 41 | + assert crumb["type"] == "http" |
| 42 | + assert crumb["category"] == "httplib" |
| 43 | + assert crumb["data"] == { |
| 44 | + "url": url, |
| 45 | + "method": "GET", |
| 46 | + "http.fragment": "", |
| 47 | + "http.query": "", |
| 48 | + "status_code": 200, |
| 49 | + "reason": "OK", |
| 50 | + "extra": "foo", |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.parametrize( |
| 55 | + "httpx_client", |
| 56 | + (httpx.Client(), httpx.AsyncClient()), |
| 57 | +) |
| 58 | +def test_outgoing_trace_headers(sentry_init, httpx_client): |
46 | 59 | sentry_init(traces_sample_rate=1.0, integrations=[HttpxIntegration()]) |
47 | | - clients = (httpx.Client(), httpx.AsyncClient()) |
48 | | - for i, c in enumerate(clients): |
49 | | - with start_transaction( |
50 | | - name="/interactions/other-dogs/new-dog", |
51 | | - op="greeting.sniff", |
52 | | - # make trace_id difference between transactions |
53 | | - trace_id=f"012345678901234567890123456789{i}", |
54 | | - ) as transaction: |
55 | | - url = "https://httpbin.org/status/200" |
56 | | - if not asyncio.iscoroutinefunction(c.get): |
57 | | - response = c.get(url) |
58 | | - else: |
59 | | - response = asyncio.get_event_loop().run_until_complete(c.get(url)) |
60 | | - |
61 | | - request_span = transaction._span_recorder.spans[-1] |
62 | | - assert response.request.headers[ |
63 | | - "sentry-trace" |
64 | | - ] == "{trace_id}-{parent_span_id}-{sampled}".format( |
65 | | - trace_id=transaction.trace_id, |
66 | | - parent_span_id=request_span.span_id, |
67 | | - sampled=1, |
| 60 | + |
| 61 | + url = "http://example.com/" |
| 62 | + responses.add(responses.GET, url, status=200) |
| 63 | + |
| 64 | + with start_transaction( |
| 65 | + name="/interactions/other-dogs/new-dog", |
| 66 | + op="greeting.sniff", |
| 67 | + trace_id="01234567890123456789012345678901", |
| 68 | + ) as transaction: |
| 69 | + if asyncio.iscoroutinefunction(httpx_client.get): |
| 70 | + response = asyncio.get_event_loop().run_until_complete( |
| 71 | + httpx_client.get(url) |
68 | 72 | ) |
| 73 | + else: |
| 74 | + response = httpx_client.get(url) |
| 75 | + |
| 76 | + request_span = transaction._span_recorder.spans[-1] |
| 77 | + assert response.request.headers[ |
| 78 | + "sentry-trace" |
| 79 | + ] == "{trace_id}-{parent_span_id}-{sampled}".format( |
| 80 | + trace_id=transaction.trace_id, |
| 81 | + parent_span_id=request_span.span_id, |
| 82 | + sampled=1, |
| 83 | + ) |
0 commit comments