forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pyreqwest.py
More file actions
148 lines (115 loc) · 4.87 KB
/
test_pyreqwest.py
File metadata and controls
148 lines (115 loc) · 4.87 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
from http.server import BaseHTTPRequestHandler, HTTPServer
from threading import Thread
import pytest
from pyreqwest.client import ClientBuilder, SyncClientBuilder
from pyreqwest.simple.request import pyreqwest_get as async_pyreqwest_get
from pyreqwest.simple.sync_request import pyreqwest_get as sync_pyreqwest_get
from sentry_sdk import start_transaction
from sentry_sdk.consts import SPANDATA
from sentry_sdk.integrations.pyreqwest import PyreqwestIntegration
from tests.conftest import get_free_port
class PyreqwestMockHandler(BaseHTTPRequestHandler):
captured_requests = []
def do_GET(self) -> None:
self.captured_requests.append(
{
"path": self.path,
"headers": {k.lower(): v for k, v in self.headers.items()},
}
)
code = 200
if "/status/" in self.path:
try:
code = int(self.path.split("/")[-1])
except (ValueError, IndexError):
code = 200
self.send_response(code)
self.end_headers()
self.wfile.write(b"OK")
def log_message(self, format: str, *args: object) -> None:
pass
@pytest.fixture(scope="module")
def server_port():
port = get_free_port()
server = HTTPServer(("localhost", port), PyreqwestMockHandler)
thread = Thread(target=server.serve_forever)
thread.daemon = True
thread.start()
yield port
server.shutdown()
@pytest.fixture(autouse=True)
def clear_captured_requests():
PyreqwestMockHandler.captured_requests.clear()
def test_sync_client_spans(sentry_init, capture_events, server_port):
sentry_init(integrations=[PyreqwestIntegration()], traces_sample_rate=1.0)
events = capture_events()
url = f"http://localhost:{server_port}/hello"
with start_transaction(name="test_transaction"):
client = SyncClientBuilder().build()
response = client.get(url).build().send()
assert response.status == 200
(event,) = events
span = event["spans"][0]
assert span["op"] == "http.client"
assert span["description"] == f"GET {url}"
assert span["data"]["url"] == url
assert span["data"][SPANDATA.HTTP_STATUS_CODE] == 200
assert span["origin"] == "auto.http.pyreqwest"
@pytest.mark.asyncio
async def test_async_client_spans(sentry_init, capture_events, server_port):
sentry_init(integrations=[PyreqwestIntegration()], traces_sample_rate=1.0)
events = capture_events()
url = f"http://localhost:{server_port}/hello"
async with ClientBuilder().build() as client:
with start_transaction(name="test_transaction"):
response = await client.get(url).build().send()
assert response.status == 200
(event,) = events
span = event["spans"][0]
assert span["op"] == "http.client"
assert span["description"] == f"GET {url}"
assert span["data"]["url"] == url
assert span["data"][SPANDATA.HTTP_STATUS_CODE] == 200
assert span["origin"] == "auto.http.pyreqwest"
def test_sync_simple_request_spans(sentry_init, capture_events, server_port):
sentry_init(integrations=[PyreqwestIntegration()], traces_sample_rate=1.0)
events = capture_events()
url = f"http://localhost:{server_port}/hello-simple"
with start_transaction(name="test_transaction"):
response = sync_pyreqwest_get(url).send()
assert response.status == 200
(event,) = events
span = event["spans"][0]
assert span["op"] == "http.client"
assert span["description"] == f"GET {url}"
@pytest.mark.asyncio
async def test_async_simple_request_spans(sentry_init, capture_events, server_port):
sentry_init(integrations=[PyreqwestIntegration()], traces_sample_rate=1.0)
events = capture_events()
url = f"http://localhost:{server_port}/hello-simple-async"
with start_transaction(name="test_transaction"):
response = await async_pyreqwest_get(url).send()
assert response.status == 200
(event,) = events
span = event["spans"][0]
assert span["op"] == "http.client"
assert span["description"] == f"GET {url}"
def test_outgoing_trace_headers(sentry_init, server_port):
sentry_init(
integrations=[PyreqwestIntegration()],
traces_sample_rate=1.0,
trace_propagation_targets=["localhost"],
)
url = f"http://localhost:{server_port}/trace"
with start_transaction(
name="test_transaction", trace_id="01234567890123456789012345678901"
):
client = SyncClientBuilder().build()
response = client.get(url).build().send()
assert response.status == 200
assert len(PyreqwestMockHandler.captured_requests) == 1
headers = PyreqwestMockHandler.captured_requests[0]["headers"]
assert "sentry-trace" in headers
assert headers["sentry-trace"].startswith("01234567890123456789012345678901")
assert "baggage" in headers
assert "sentry-trace_id=01234567890123456789012345678901" in headers["baggage"]