diff --git a/sentry_sdk/integrations/opentelemetry/utils.py b/sentry_sdk/integrations/opentelemetry/utils.py index 07e60ddd0f..6127ceba5c 100644 --- a/sentry_sdk/integrations/opentelemetry/utils.py +++ b/sentry_sdk/integrations/opentelemetry/utils.py @@ -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, @@ -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 diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 48b8571b98..fbe258fb8a 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -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] diff --git a/tests/test_dsc.py b/tests/test_dsc.py index 3b8cff5baf..e1ceb80a05 100644 --- a/tests/test_dsc.py +++ b/tests/test_dsc.py @@ -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 @@ -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 @@ -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: @@ -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", @@ -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: @@ -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): @@ -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