Skip to content

Commit 9686f5f

Browse files
committed
Better tests
1 parent abfe9a2 commit 9686f5f

File tree

1 file changed

+192
-22
lines changed

1 file changed

+192
-22
lines changed
Lines changed: 192 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import pytest
22
import requests
3-
import responses
43
import sentry_sdk
4+
from http.client import HTTPConnection
55

6+
USE_DEFAULT_TRACES_SAMPLE_RATE = -1
67

78
INCOMING_TRACE_ID = "771a43a4192642f0b136d5159a501700"
89
INCOMING_HEADERS = {
@@ -20,16 +21,36 @@
2021

2122

2223
#
23-
# I want to have propper testing of trace propagation.
24+
# I want to have proper testing of trace propagation.
2425
# Testing the matrix of test cases described here:
2526
# https://docs.google.com/spreadsheets/d/1IyOTYIC2bwu6HeHrxbLHAm6Lq44atVzf2TDJoPCMDZA/edit?gid=0#gid=0
2627
#
2728

2829

29-
@responses.activate
30+
@pytest.fixture
31+
def _mock_putheader(monkeypatch):
32+
"""
33+
Mock HTTPConnection.putheader to capture calls to it.
34+
"""
35+
putheader_calls = []
36+
original_putheader = HTTPConnection.putheader
37+
38+
def mock_putheader_fn(self, header, value):
39+
putheader_calls.append((header, value))
40+
return original_putheader(self, header, value)
41+
42+
monkeypatch.setattr(HTTPConnection, "putheader", mock_putheader_fn)
43+
return putheader_calls
44+
45+
3046
@pytest.mark.parametrize(
3147
"traces_sample_rate",
32-
[1, 1, 1, 1], # [-1, None, 0, 1.0],
48+
[
49+
USE_DEFAULT_TRACES_SAMPLE_RATE,
50+
None,
51+
0,
52+
1.0,
53+
],
3354
ids=[
3455
"default traces_sample_rate",
3556
"traces_sample_rate=None",
@@ -38,39 +59,188 @@
3859
],
3960
)
4061
def test_trace_propagation_no_incoming_trace(
41-
sentry_init, capture_events, traces_sample_rate
62+
sentry_init, capture_events, _mock_putheader, traces_sample_rate
4263
):
43-
# TODO: responses mocks away our stdlib putrequest patch, so I need to find a way to mock the request in another way
44-
responses.add(responses.GET, "http://example.com")
64+
init_kwargs = {}
65+
if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE:
66+
init_kwargs["traces_sample_rate"] = traces_sample_rate
67+
sentry_init(**init_kwargs)
68+
69+
events = capture_events()
70+
71+
with sentry_sdk.continue_trace({}):
72+
with sentry_sdk.start_span(op="test", name="test"):
73+
requests.get(
74+
"http://example.com", headers={"custom-header": "custom-value"}
75+
)
76+
77+
# CHECK if performance data (a transaction/span) is sent to Sentry
78+
if traces_sample_rate == 1:
79+
assert len(events) == 1
80+
else:
81+
assert len(events) == 0
82+
83+
outgoing_request_headers = {key: value for key, value in _mock_putheader}
84+
assert "custom-header" in outgoing_request_headers
85+
86+
# CHECK if trace information is added to the outgoing request
87+
assert "sentry-trace" in outgoing_request_headers
88+
assert "baggage" in outgoing_request_headers
89+
90+
# CHECK if incoming trace is continued
91+
# as no incoming data is given to continue_trace() the incoming trace is never continued
92+
assert INCOMING_TRACE_ID not in outgoing_request_headers["sentry-trace"]
93+
assert INCOMING_TRACE_ID not in outgoing_request_headers["baggage"]
94+
95+
96+
@pytest.mark.parametrize(
97+
"traces_sample_rate",
98+
[
99+
USE_DEFAULT_TRACES_SAMPLE_RATE,
100+
None,
101+
0,
102+
1.0,
103+
],
104+
ids=[
105+
"default traces_sample_rate",
106+
"traces_sample_rate=None",
107+
"traces_sample_rate=0",
108+
"traces_sample_rate=1",
109+
],
110+
)
111+
def test_trace_propagation_with_incoming_trace(
112+
sentry_init, capture_events, _mock_putheader, traces_sample_rate
113+
):
114+
init_kwargs = {}
115+
if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE:
116+
init_kwargs["traces_sample_rate"] = traces_sample_rate
117+
sentry_init(**init_kwargs)
118+
119+
events = capture_events()
120+
121+
with sentry_sdk.continue_trace(INCOMING_HEADERS):
122+
with sentry_sdk.start_span(op="test", name="test"):
123+
requests.get(
124+
"http://example.com", headers={"custom-header": "custom-value"}
125+
)
126+
127+
# CHECK if performance data (a transaction/span) is sent to Sentry
128+
if traces_sample_rate == 1:
129+
assert len(events) == 1
130+
else:
131+
assert len(events) == 0
45132

133+
outgoing_request_headers = {key: value for key, value in _mock_putheader}
134+
assert "custom-header" in outgoing_request_headers
135+
136+
# CHECK if trace information is added to the outgoing request
137+
assert "sentry-trace" in outgoing_request_headers
138+
assert "baggage" in outgoing_request_headers
139+
140+
# CHECK if incoming trace is continued
141+
if traces_sample_rate in (0, 1, USE_DEFAULT_TRACES_SAMPLE_RATE):
142+
# continue the incoming trace
143+
assert INCOMING_TRACE_ID in outgoing_request_headers["sentry-trace"]
144+
assert INCOMING_TRACE_ID in outgoing_request_headers["baggage"]
145+
elif traces_sample_rate is None:
146+
# do NOT continue the incoming trace
147+
assert INCOMING_TRACE_ID not in outgoing_request_headers["sentry-trace"]
148+
assert INCOMING_TRACE_ID not in outgoing_request_headers["baggage"]
149+
150+
151+
@pytest.mark.parametrize(
152+
"traces_sample_rate",
153+
[
154+
USE_DEFAULT_TRACES_SAMPLE_RATE,
155+
None,
156+
0,
157+
1.0,
158+
],
159+
ids=[
160+
"default traces_sample_rate",
161+
"traces_sample_rate=None",
162+
"traces_sample_rate=0",
163+
"traces_sample_rate=1",
164+
],
165+
)
166+
def test_trace_propagation_no_incoming_trace_and_targets_not_matching(
167+
sentry_init, capture_events, _mock_putheader, traces_sample_rate
168+
):
46169
init_kwargs = {
47-
"debug": True,
170+
"trace_propagation_targets": [
171+
"http://someothersite.com",
172+
],
48173
}
49-
if traces_sample_rate != -1:
174+
if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE:
50175
init_kwargs["traces_sample_rate"] = traces_sample_rate
51-
52176
sentry_init(**init_kwargs)
53177

54178
events = capture_events()
55179

56-
with sentry_sdk.start_span(op="test", name="test"):
57-
requests.get("http://example.com")
180+
with sentry_sdk.continue_trace({}):
181+
with sentry_sdk.start_span(op="test", name="test"):
182+
requests.get(
183+
"http://example.com", headers={"custom-header": "custom-value"}
184+
)
58185

59-
# CHECK: performance data (a transaction/span) is sent to sentry
186+
# CHECK if performance data (a transaction/span) is sent to Sentry
60187
if traces_sample_rate == 1:
61188
assert len(events) == 1
62189
else:
63190
assert len(events) == 0
64191

65-
# CHECK: trace information is added to the outgoing request
66-
request = responses.calls[0].request
192+
outgoing_request_headers = {key: value for key, value in _mock_putheader}
193+
assert "custom-header" in outgoing_request_headers
194+
195+
# CHECK if trace information is added to the outgoing request
196+
assert "sentry-trace" not in outgoing_request_headers
197+
assert "baggage" not in outgoing_request_headers
198+
199+
200+
@pytest.mark.parametrize(
201+
"traces_sample_rate",
202+
[
203+
USE_DEFAULT_TRACES_SAMPLE_RATE,
204+
None,
205+
0,
206+
1.0,
207+
],
208+
ids=[
209+
"default traces_sample_rate",
210+
"traces_sample_rate=None",
211+
"traces_sample_rate=0",
212+
"traces_sample_rate=1",
213+
],
214+
)
215+
def test_trace_propagation_with_incoming_trace_and_targets_not_matching(
216+
sentry_init, capture_events, _mock_putheader, traces_sample_rate
217+
):
218+
init_kwargs = {
219+
"trace_propagation_targets": [
220+
"http://someothersite.com",
221+
],
222+
}
223+
if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE:
224+
init_kwargs["traces_sample_rate"] = traces_sample_rate
225+
sentry_init(**init_kwargs)
226+
227+
events = capture_events()
228+
229+
with sentry_sdk.continue_trace(INCOMING_HEADERS):
230+
with sentry_sdk.start_span(op="test", name="test"):
231+
requests.get(
232+
"http://example.com", headers={"custom-header": "custom-value"}
233+
)
234+
235+
# CHECK if performance data (a transaction/span) is sent to Sentry
67236
if traces_sample_rate == 1:
68-
assert "sentry-trace" in request.headers
69-
assert "baggage" in request.headers
70-
assert INCOMING_TRACE_ID not in request.headers["sentry-trace"]
71-
assert INCOMING_TRACE_ID not in request.headers["baggage"]
237+
assert len(events) == 1
72238
else:
73-
assert "sentry-trace" not in request.headers
74-
assert "baggage" not in request.headers
239+
assert len(events) == 0
240+
241+
outgoing_request_headers = {key: value for key, value in _mock_putheader}
242+
assert "custom-header" in outgoing_request_headers
75243

76-
# CHECK: the incoming trace_id and the current trace id do match (or not match)
244+
# CHECK if trace information is added to the outgoing request
245+
assert "sentry-trace" not in outgoing_request_headers
246+
assert "baggage" not in outgoing_request_headers

0 commit comments

Comments
 (0)