Skip to content

Commit e9a2f05

Browse files
committed
Better tests
1 parent abfe9a2 commit e9a2f05

File tree

1 file changed

+97
-25
lines changed

1 file changed

+97
-25
lines changed
Lines changed: 97 additions & 25 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,90 @@
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")
45-
46-
init_kwargs = {
47-
"debug": True,
48-
}
49-
if traces_sample_rate != -1:
64+
init_kwargs = {}
65+
if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE:
5066
init_kwargs["traces_sample_rate"] = traces_sample_rate
51-
5267
sentry_init(**init_kwargs)
5368

5469
events = capture_events()
5570

56-
with sentry_sdk.start_span(op="test", name="test"):
57-
requests.get("http://example.com")
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+
)
5876

59-
# CHECK: performance data (a transaction/span) is sent to sentry
77+
# CHECK if performance data (a transaction/span) is sent to Sentry
6078
if traces_sample_rate == 1:
6179
assert len(events) == 1
6280
else:
6381
assert len(events) == 0
6482

65-
# CHECK: trace information is added to the outgoing request
66-
request = responses.calls[0].request
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
67128
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"]
129+
assert len(events) == 1
72130
else:
73-
assert "sentry-trace" not in request.headers
74-
assert "baggage" not in request.headers
131+
assert len(events) == 0
132+
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
75139

76-
# CHECK: the incoming trace_id and the current trace id do match (or not match)
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"]

0 commit comments

Comments
 (0)