Skip to content

Commit 3153c60

Browse files
committed
cleanup
1 parent 9686f5f commit 3153c60

File tree

1 file changed

+71
-47
lines changed

1 file changed

+71
-47
lines changed

tests/tracing/test_trace_propagation.py

+71-47
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ def mock_putheader_fn(self, header, value):
5252
1.0,
5353
],
5454
ids=[
55-
"default traces_sample_rate",
55+
"traces_sample_rate=DEFAULT",
5656
"traces_sample_rate=None",
5757
"traces_sample_rate=0",
5858
"traces_sample_rate=1",
5959
],
6060
)
61-
def test_trace_propagation_no_incoming_trace(
61+
def test_no_incoming_trace_and_trace_propagation_targets_matching(
6262
sentry_init, capture_events, _mock_putheader, traces_sample_rate
6363
):
6464
init_kwargs = {}
@@ -68,11 +68,11 @@ def test_trace_propagation_no_incoming_trace(
6868

6969
events = capture_events()
7070

71-
with sentry_sdk.continue_trace({}):
71+
NO_INCOMING_HEADERS = {} # noqa: N806
72+
73+
with sentry_sdk.continue_trace(NO_INCOMING_HEADERS):
7274
with sentry_sdk.start_span(op="test", name="test"):
73-
requests.get(
74-
"http://example.com", headers={"custom-header": "custom-value"}
75-
)
75+
requests.get("http://example.com")
7676

7777
# CHECK if performance data (a transaction/span) is sent to Sentry
7878
if traces_sample_rate == 1:
@@ -81,7 +81,6 @@ def test_trace_propagation_no_incoming_trace(
8181
assert len(events) == 0
8282

8383
outgoing_request_headers = {key: value for key, value in _mock_putheader}
84-
assert "custom-header" in outgoing_request_headers
8584

8685
# CHECK if trace information is added to the outgoing request
8786
assert "sentry-trace" in outgoing_request_headers
@@ -102,27 +101,31 @@ def test_trace_propagation_no_incoming_trace(
102101
1.0,
103102
],
104103
ids=[
105-
"default traces_sample_rate",
104+
"traces_sample_rate=DEFAULT",
106105
"traces_sample_rate=None",
107106
"traces_sample_rate=0",
108107
"traces_sample_rate=1",
109108
],
110109
)
111-
def test_trace_propagation_with_incoming_trace(
110+
def test_no_incoming_trace_and_trace_propagation_targets_not_matching(
112111
sentry_init, capture_events, _mock_putheader, traces_sample_rate
113112
):
114-
init_kwargs = {}
113+
init_kwargs = {
114+
"trace_propagation_targets": [
115+
"http://someothersite.com",
116+
],
117+
}
115118
if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE:
116119
init_kwargs["traces_sample_rate"] = traces_sample_rate
117120
sentry_init(**init_kwargs)
118121

119122
events = capture_events()
120123

121-
with sentry_sdk.continue_trace(INCOMING_HEADERS):
124+
NO_INCOMING_HEADERS = {} # noqa: N806
125+
126+
with sentry_sdk.continue_trace(NO_INCOMING_HEADERS):
122127
with sentry_sdk.start_span(op="test", name="test"):
123-
requests.get(
124-
"http://example.com", headers={"custom-header": "custom-value"}
125-
)
128+
requests.get("http://example.com")
126129

127130
# CHECK if performance data (a transaction/span) is sent to Sentry
128131
if traces_sample_rate == 1:
@@ -131,21 +134,13 @@ def test_trace_propagation_with_incoming_trace(
131134
assert len(events) == 0
132135

133136
outgoing_request_headers = {key: value for key, value in _mock_putheader}
134-
assert "custom-header" in outgoing_request_headers
135137

136138
# 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+
assert "sentry-trace" not in outgoing_request_headers
140+
assert "baggage" not in outgoing_request_headers
139141

140142
# 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"]
143+
# (no assert necessary, because the trace information is not added to the outgoing request (see previous asserts))
149144

150145

151146
@pytest.mark.parametrize(
@@ -157,31 +152,38 @@ def test_trace_propagation_with_incoming_trace(
157152
1.0,
158153
],
159154
ids=[
160-
"default traces_sample_rate",
155+
"traces_sample_rate=DEFAULT",
161156
"traces_sample_rate=None",
162157
"traces_sample_rate=0",
163158
"traces_sample_rate=1",
164159
],
165160
)
166-
def test_trace_propagation_no_incoming_trace_and_targets_not_matching(
167-
sentry_init, capture_events, _mock_putheader, traces_sample_rate
161+
@pytest.mark.parametrize(
162+
"incoming_parent_sampled",
163+
["deferred", "1", "0"],
164+
ids=[
165+
"incoming_parent_sampled=DEFERRED",
166+
"incoming_parent_sampled=1",
167+
"incoming_parent_sampled=0",
168+
],
169+
)
170+
def test_with_incoming_trace_and_trace_propagation_targets_matching(
171+
sentry_init,
172+
capture_events,
173+
_mock_putheader,
174+
incoming_parent_sampled,
175+
traces_sample_rate,
168176
):
169-
init_kwargs = {
170-
"trace_propagation_targets": [
171-
"http://someothersite.com",
172-
],
173-
}
177+
init_kwargs = {}
174178
if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE:
175179
init_kwargs["traces_sample_rate"] = traces_sample_rate
176180
sentry_init(**init_kwargs)
177181

178182
events = capture_events()
179183

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

186188
# CHECK if performance data (a transaction/span) is sent to Sentry
187189
if traces_sample_rate == 1:
@@ -190,11 +192,20 @@ def test_trace_propagation_no_incoming_trace_and_targets_not_matching(
190192
assert len(events) == 0
191193

192194
outgoing_request_headers = {key: value for key, value in _mock_putheader}
193-
assert "custom-header" in outgoing_request_headers
194195

195196
# 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
197+
assert "sentry-trace" in outgoing_request_headers
198+
assert "baggage" in outgoing_request_headers
199+
200+
# CHECK if incoming trace is continued
201+
if traces_sample_rate in (0, 1, USE_DEFAULT_TRACES_SAMPLE_RATE):
202+
# continue the incoming trace
203+
assert INCOMING_TRACE_ID in outgoing_request_headers["sentry-trace"]
204+
assert INCOMING_TRACE_ID in outgoing_request_headers["baggage"]
205+
elif traces_sample_rate is None:
206+
# do NOT continue the incoming trace
207+
assert INCOMING_TRACE_ID not in outgoing_request_headers["sentry-trace"]
208+
assert INCOMING_TRACE_ID not in outgoing_request_headers["baggage"]
198209

199210

200211
@pytest.mark.parametrize(
@@ -206,14 +217,27 @@ def test_trace_propagation_no_incoming_trace_and_targets_not_matching(
206217
1.0,
207218
],
208219
ids=[
209-
"default traces_sample_rate",
220+
"traces_sample_rate=DEFAULT",
210221
"traces_sample_rate=None",
211222
"traces_sample_rate=0",
212223
"traces_sample_rate=1",
213224
],
214225
)
215-
def test_trace_propagation_with_incoming_trace_and_targets_not_matching(
216-
sentry_init, capture_events, _mock_putheader, traces_sample_rate
226+
@pytest.mark.parametrize(
227+
"incoming_parent_sampled",
228+
["deferred", "1", "0"],
229+
ids=[
230+
"incoming_parent_sampled=DEFERRED",
231+
"incoming_parent_sampled=1",
232+
"incoming_parent_sampled=0",
233+
],
234+
)
235+
def test_with_incoming_trace_and_trace_propagation_targets_not_matching(
236+
sentry_init,
237+
capture_events,
238+
_mock_putheader,
239+
incoming_parent_sampled,
240+
traces_sample_rate,
217241
):
218242
init_kwargs = {
219243
"trace_propagation_targets": [
@@ -228,9 +252,7 @@ def test_trace_propagation_with_incoming_trace_and_targets_not_matching(
228252

229253
with sentry_sdk.continue_trace(INCOMING_HEADERS):
230254
with sentry_sdk.start_span(op="test", name="test"):
231-
requests.get(
232-
"http://example.com", headers={"custom-header": "custom-value"}
233-
)
255+
requests.get("http://example.com")
234256

235257
# CHECK if performance data (a transaction/span) is sent to Sentry
236258
if traces_sample_rate == 1:
@@ -239,8 +261,10 @@ def test_trace_propagation_with_incoming_trace_and_targets_not_matching(
239261
assert len(events) == 0
240262

241263
outgoing_request_headers = {key: value for key, value in _mock_putheader}
242-
assert "custom-header" in outgoing_request_headers
243264

244265
# CHECK if trace information is added to the outgoing request
245266
assert "sentry-trace" not in outgoing_request_headers
246267
assert "baggage" not in outgoing_request_headers
268+
269+
# CHECK if incoming trace is continued
270+
# (no assert necessary, because the trace information is not added to the outgoing request (see previous asserts))

0 commit comments

Comments
 (0)