@@ -52,13 +52,13 @@ def mock_putheader_fn(self, header, value):
52
52
1.0 ,
53
53
],
54
54
ids = [
55
- "default traces_sample_rate" ,
55
+ "traces_sample_rate=DEFAULT " ,
56
56
"traces_sample_rate=None" ,
57
57
"traces_sample_rate=0" ,
58
58
"traces_sample_rate=1" ,
59
59
],
60
60
)
61
- def test_trace_propagation_no_incoming_trace (
61
+ def test_no_incoming_trace_and_trace_propagation_targets_matching (
62
62
sentry_init , capture_events , _mock_putheader , traces_sample_rate
63
63
):
64
64
init_kwargs = {}
@@ -68,11 +68,11 @@ def test_trace_propagation_no_incoming_trace(
68
68
69
69
events = capture_events ()
70
70
71
- with sentry_sdk .continue_trace ({}):
71
+ NO_INCOMING_HEADERS = {} # noqa: N806
72
+
73
+ with sentry_sdk .continue_trace (NO_INCOMING_HEADERS ):
72
74
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" )
76
76
77
77
# CHECK if performance data (a transaction/span) is sent to Sentry
78
78
if traces_sample_rate == 1 :
@@ -81,7 +81,6 @@ def test_trace_propagation_no_incoming_trace(
81
81
assert len (events ) == 0
82
82
83
83
outgoing_request_headers = {key : value for key , value in _mock_putheader }
84
- assert "custom-header" in outgoing_request_headers
85
84
86
85
# CHECK if trace information is added to the outgoing request
87
86
assert "sentry-trace" in outgoing_request_headers
@@ -102,27 +101,31 @@ def test_trace_propagation_no_incoming_trace(
102
101
1.0 ,
103
102
],
104
103
ids = [
105
- "default traces_sample_rate" ,
104
+ "traces_sample_rate=DEFAULT " ,
106
105
"traces_sample_rate=None" ,
107
106
"traces_sample_rate=0" ,
108
107
"traces_sample_rate=1" ,
109
108
],
110
109
)
111
- def test_trace_propagation_with_incoming_trace (
110
+ def test_no_incoming_trace_and_trace_propagation_targets_not_matching (
112
111
sentry_init , capture_events , _mock_putheader , traces_sample_rate
113
112
):
114
- init_kwargs = {}
113
+ init_kwargs = {
114
+ "trace_propagation_targets" : [
115
+ "http://someothersite.com" ,
116
+ ],
117
+ }
115
118
if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE :
116
119
init_kwargs ["traces_sample_rate" ] = traces_sample_rate
117
120
sentry_init (** init_kwargs )
118
121
119
122
events = capture_events ()
120
123
121
- with sentry_sdk .continue_trace (INCOMING_HEADERS ):
124
+ NO_INCOMING_HEADERS = {} # noqa: N806
125
+
126
+ with sentry_sdk .continue_trace (NO_INCOMING_HEADERS ):
122
127
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" )
126
129
127
130
# CHECK if performance data (a transaction/span) is sent to Sentry
128
131
if traces_sample_rate == 1 :
@@ -131,21 +134,13 @@ def test_trace_propagation_with_incoming_trace(
131
134
assert len (events ) == 0
132
135
133
136
outgoing_request_headers = {key : value for key , value in _mock_putheader }
134
- assert "custom-header" in outgoing_request_headers
135
137
136
138
# 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
139
141
140
142
# 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))
149
144
150
145
151
146
@pytest .mark .parametrize (
@@ -157,31 +152,38 @@ def test_trace_propagation_with_incoming_trace(
157
152
1.0 ,
158
153
],
159
154
ids = [
160
- "default traces_sample_rate" ,
155
+ "traces_sample_rate=DEFAULT " ,
161
156
"traces_sample_rate=None" ,
162
157
"traces_sample_rate=0" ,
163
158
"traces_sample_rate=1" ,
164
159
],
165
160
)
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 ,
168
176
):
169
- init_kwargs = {
170
- "trace_propagation_targets" : [
171
- "http://someothersite.com" ,
172
- ],
173
- }
177
+ init_kwargs = {}
174
178
if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE :
175
179
init_kwargs ["traces_sample_rate" ] = traces_sample_rate
176
180
sentry_init (** init_kwargs )
177
181
178
182
events = capture_events ()
179
183
180
- with sentry_sdk .continue_trace ({} ):
184
+ with sentry_sdk .continue_trace (INCOMING_HEADERS ):
181
185
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" )
185
187
186
188
# CHECK if performance data (a transaction/span) is sent to Sentry
187
189
if traces_sample_rate == 1 :
@@ -190,11 +192,20 @@ def test_trace_propagation_no_incoming_trace_and_targets_not_matching(
190
192
assert len (events ) == 0
191
193
192
194
outgoing_request_headers = {key : value for key , value in _mock_putheader }
193
- assert "custom-header" in outgoing_request_headers
194
195
195
196
# 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" ]
198
209
199
210
200
211
@pytest .mark .parametrize (
@@ -206,14 +217,27 @@ def test_trace_propagation_no_incoming_trace_and_targets_not_matching(
206
217
1.0 ,
207
218
],
208
219
ids = [
209
- "default traces_sample_rate" ,
220
+ "traces_sample_rate=DEFAULT " ,
210
221
"traces_sample_rate=None" ,
211
222
"traces_sample_rate=0" ,
212
223
"traces_sample_rate=1" ,
213
224
],
214
225
)
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 ,
217
241
):
218
242
init_kwargs = {
219
243
"trace_propagation_targets" : [
@@ -228,9 +252,7 @@ def test_trace_propagation_with_incoming_trace_and_targets_not_matching(
228
252
229
253
with sentry_sdk .continue_trace (INCOMING_HEADERS ):
230
254
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" )
234
256
235
257
# CHECK if performance data (a transaction/span) is sent to Sentry
236
258
if traces_sample_rate == 1 :
@@ -239,8 +261,10 @@ def test_trace_propagation_with_incoming_trace_and_targets_not_matching(
239
261
assert len (events ) == 0
240
262
241
263
outgoing_request_headers = {key : value for key , value in _mock_putheader }
242
- assert "custom-header" in outgoing_request_headers
243
264
244
265
# CHECK if trace information is added to the outgoing request
245
266
assert "sentry-trace" not in outgoing_request_headers
246
267
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