Skip to content

Commit

Permalink
Fix test_dsc and twp baggage handling (#3789)
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Nov 18, 2024
1 parent 83442f5 commit 238492d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
4 changes: 2 additions & 2 deletions sentry_sdk/integrations/opentelemetry/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from datetime import datetime, timezone

from urllib3.util import parse_url as urlparse
from urllib.parse import quote
from urllib.parse import quote, unquote
from opentelemetry.trace import (
Span as AbstractSpan,
SpanKind,
Expand Down Expand Up @@ -354,7 +354,7 @@ def dsc_from_trace_state(trace_state):
for k, v in trace_state.items():
if Baggage.SENTRY_PREFIX_REGEX.match(k):
key = re.sub(Baggage.SENTRY_PREFIX_REGEX, "", k)
dsc[key] = v
dsc[unquote(key)] = unquote(v)
return dsc


Expand Down
9 changes: 3 additions & 6 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,10 @@ def generate_propagation_context(self, incoming_data=None):
def get_dynamic_sampling_context(self):
# type: () -> Optional[Dict[str, str]]
"""
Returns the Dynamic Sampling Context from the Propagation Context.
Returns the Dynamic Sampling Context from the baggage or populates one.
"""
return (
self._propagation_context.dynamic_sampling_context
if self._propagation_context
else None
)
baggage = self.get_baggage()
return baggage.dynamic_sampling_context() if baggage else None

def get_traceparent(self, *args, **kwargs):
# type: (Any, Any) -> Optional[str]
Expand Down
38 changes: 19 additions & 19 deletions tests/test_dsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def test_dsc_head_of_trace(sentry_init, capture_envelopes):
)
envelopes = capture_envelopes()

# We start a new transaction
with sentry_sdk.start_transaction(name="foo"):
# We start a new root_span
with sentry_sdk.start_span(name="foo"):
pass

assert len(envelopes) == 1
Expand Down Expand Up @@ -95,10 +95,10 @@ def test_dsc_continuation_of_trace(sentry_init, capture_envelopes):
"HTTP_BAGGAGE": baggage,
}

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

assert len(envelopes) == 1

Expand Down Expand Up @@ -145,7 +145,7 @@ def test_dsc_issue(sentry_init, capture_envelopes):
)
envelopes = capture_envelopes()

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

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

# We start a new transaction and an error occurs
with sentry_sdk.start_transaction(name="foo"):
# We start a new root span and an error occurs
with sentry_sdk.start_span(name="foo"):
try:
1 / 0
except ZeroDivisionError as exp:
Expand Down Expand Up @@ -239,7 +239,7 @@ def test_dsc_issue_with_tracing(sentry_init, capture_envelopes):
"traces_sample_rate",
[
0, # no traces will be started, but if incoming traces will be continued (by our instrumentations, not happening in this test)
None, # no tracing at all. This service will never create transactions.
None, # no tracing at all. This service will never create root spans.
],
)
def test_dsc_issue_twp(sentry_init, capture_envelopes, traces_sample_rate):
Expand Down Expand Up @@ -278,14 +278,14 @@ def test_dsc_issue_twp(sentry_init, capture_envelopes, traces_sample_rate):
}

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

# No transaction is started, just an error is captured
try:
1 / 0
except ZeroDivisionError as exp:
sentry_sdk.capture_exception(exp)
# No root span is started, just an error is captured
try:
1 / 0
except ZeroDivisionError as exp:
sentry_sdk.capture_exception(exp)

assert len(envelopes) == 1

Expand Down

0 comments on commit 238492d

Please sign in to comment.