8000 Fix test_dsc and twp baggage handling (#3789) · getsentry/sentry-python@5dc59d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5dc59d9

Browse files
authored
Fix test_dsc and twp baggage handling (#3789)
1 parent bf84550 commit 5dc59d9

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

sentry_sdk/integrations/opentelemetry/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from datetime import datetime, timezone
44

55
from urllib3.util import parse_url as urlparse
6-
from urllib.parse import quote
6+
from urllib.parse import quote, unquote
77
from opentelemetry.trace import (
88
Span as AbstractSpan,
99
SpanKind,
@@ -354,7 +354,7 @@ def dsc_from_trace_state(trace_state):
354354
for k, v in trace_state.items():
355355
if Baggage.SENTRY_PREFIX_REGEX.match(k):
356356
key = re.sub(Baggage.SENTRY_PREFIX_REGEX, "", k)
357-
dsc[key] = v
357+
dsc[unquote(key)] = unquote(v)
358358
return dsc
359359

360360

sentry_sdk/scope.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -480,13 +480,10 @@ def generate_propagation_context(self, incoming_data=None):
480480
def get_dynamic_sampling_context(self):
481481
# type: () -> Optional[Dict[str, str]]
482482
"""
483-
Returns the Dynamic Sampling Context from the Propagation Context.
483+
Returns the Dynamic Sampling Context from the baggage or populates one.
484484
"""
485-
return (
486-
self._propagation_context.dynamic_sampling_context
487-
if self._propagation_context
488-
else None
489-
)
485+
baggage = self.get_baggage()
486+
return baggage.dynamic_sampling_context() if baggage else None
490487

491488
def get_traceparent(self, *args, **kwargs):
492489
# type: (Any, Any) -> Optional[str]

tests/test_dsc.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def test_dsc_head_of_trace(sentry_init, capture_envelopes):
2727
)
2828
envelopes = capture_envelopes()
2929

30-
# We start a new transaction
31-
with sentry_sdk.start_transaction(name="foo"):
30+
# We start a new root_span
31+
with sentry_sdk.start_span(name="foo"):
3232
pass
3333

3434
assert len(envelopes) == 1
@@ -95,10 +95,10 @@ def test_dsc_continuation_of_trace(sentry_init, capture_envelopes):
9595
"HTTP_BAGGAGE": baggage,
9696
}
9797

98-
# We continue the incoming trace and start a new transaction
99-
transaction = sentry_sdk.continue_trace(incoming_http_headers)
100-
with sentry_sdk.start_transaction(transaction, name="foo"):
101-
pass
98+
# We continue the incoming trace and start a new root span
99+
with sentry_sdk.continue_trace(incoming_http_headers):
100+
with sentry_sdk.start_span(name="foo"):
101+
pass
102102

103103
assert len(envelopes) == 1
104104

@@ -145,7 +145,7 @@ def test_dsc_issue(sentry_init, capture_envelopes):
145145
)
146146
envelopes = capture_envelopes()
147147

148-
# No transaction is started, just an error is captured
148+
# No root span is started, just an error is captured
149149
try:
150150
1 / 0
151151
except ZeroDivisionError as exp:
@@ -181,8 +181,8 @@ def test_dsc_issue(sentry_init, capture_envelopes):
181181

182182
def test_dsc_issue_with_tracing(sentry_init, capture_envelopes):
183183
"""
184-
Our service has tracing enabled and an error occurs in an transaction.
185-
Envelopes containing errors also have the same DSC than the transaction envelopes.
184+
Our service has tracing enabled and an error occurs in an root span.
185+
Envelopes containing errors also have the same DSC than the root span envelopes.
186186
"""
187187
sentry_init(
188188
dsn="https://mysecret@bla.ingest.sentry.io/12312012",
@@ -192,8 +192,8 @@ def test_dsc_issue_with_tracing(sentry_init, capture_envelopes):
192192
)
193193
envelopes = capture_envelopes()
194194

195-
# We start a new transaction and an error occurs
196-
with sentry_sdk.start_transaction(name="foo"):
195+
# We start a new root span and an error occurs
196+
with sentry_sdk.start_span(name="foo"):
197197
try:
198198
1 / 0
199199
except ZeroDivisionErro 6D4E r as exp:
@@ -239,7 +239,7 @@ def test_dsc_issue_with_tracing(sentry_init, capture_envelopes):
239239
"traces_sample_rate",
240240
[
241241
0, # no traces will be started, but if incoming traces will be continued (by our instrumentations, not happening in this test)
242-
None, # no tracing at all. This service will never create transactions.
242+
None, # no tracing at all. This service will never create root spans.
243243
],
244244
)
245245
def test_dsc_issue_twp(sentry_init, capture_envelopes, traces_sample_rate):
@@ -278,14 +278,14 @@ def test_dsc_issue_twp(sentry_init, capture_envelopes, traces_sample_rate):
278278
}
279279

280280
# We continue the trace (meaning: saving the incoming trace information on the scope)
281-
# but in this test, we do not start a transaction.
282-
sentry_sdk.continue_trace(incoming_http_headers)
281+
# but in this test, we do not start a root span.
282+
with sentry_sdk.continue_trace(incoming_http_headers):
283283

284-
# No transaction is started, just an error is captured
285-
try:
286-
1 / 0
287-
except ZeroDivisionError as exp:
288-
sentry_sdk.capture_exception(exp)
284+
# No root span is started, just an error is captured
285+
try:
286+
1 / 0
287+
except ZeroDivisionError as exp:
288+
sentry_sdk.capture_exception(exp)
289289

290290
assert len(envelopes) == 1
291291

0 commit comments

Comments
 (0)
0