diff --git a/sentry_sdk/api.py b/sentry_sdk/api.py index 166dddd199..639d0fe5dc 100644 --- a/sentry_sdk/api.py +++ b/sentry_sdk/api.py @@ -295,7 +295,7 @@ def start_transaction( **kwargs, # type: Any ): # type: (...) -> Union[Transaction, NoOpSpan] - return Scope.get_current_scope().start_transaction(transaction, **kwargs) + return Scope.get_isolation_scope().start_transaction(transaction, **kwargs) def set_measurement(name, value, unit=""): @@ -318,7 +318,7 @@ def get_traceparent(): """ Returns the traceparent either from the active span or from the scope. """ - return Scope.get_current_scope().get_traceparent() + return Scope.get_isolation_scope().get_traceparent() def get_baggage(): @@ -326,7 +326,7 @@ def get_baggage(): """ Returns Baggage either from the active span or from the scope. """ - baggage = Scope.get_current_scope().get_baggage() + baggage = Scope.get_isolation_scope().get_baggage() if baggage is not None: return baggage.serialize() @@ -339,6 +339,6 @@ def continue_trace(environ_or_headers, op=None, name=None, source=None): """ Sets the propagation context from environment or headers and returns a transaction. """ - return Scope.get_current_scope().continue_trace( + return Scope.get_isolation_scope().continue_trace( environ_or_headers, op, name, source ) diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 2121dcfe8d..efc816b6ba 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -469,7 +469,7 @@ def generate_propagation_context(self, incoming_data=None): self._propagation_context, ) - if self._propagation_context is None and self._type == ScopeType.CURRENT: + if self._propagation_context is None and self._type != ScopeType.CURRENT: self.set_new_propagation_context() def get_dynamic_sampling_context(self): @@ -616,6 +616,21 @@ def iter_trace_propagation_headers(self, *args, **kwargs): for header in self.iter_headers(): yield header + def get_active_propagation_context(self): + # type: () -> Dict[str, Any] + if self._propagation_context is not None: + return self._propagation_context + + current_scope = Scope.get_current_scope() + if current_scope._propagation_context is not None: + return current_scope._propagation_context + + isolation_scope = Scope.get_isolation_scope() + if isolation_scope._propagation_context is not None: + return isolation_scope._propagation_context + + return {} + def clear(self): # type: () -> None """Clears the entire scope.""" @@ -964,20 +979,20 @@ def start_span(self, instrumenter=INSTRUMENTER.SENTRY, **kwargs): if instrumenter != configuration_instrumenter: return NoOpSpan() - scope = Scope.get_current_scope() - span = scope.span + # get current span or transaction + span = self.span or Scope.get_isolation_scope().span if span is None: # New spans get the `trace_id`` from the scope if "trace_id" not in kwargs: - traceparent = self.get_traceparent() - trace_id = traceparent.split("-")[0] if traceparent else None + + trace_id = self.get_active_propagation_context().get("trace_id") if trace_id is not None: kwargs["trace_id"] = trace_id span = Span(**kwargs) else: - # Children take `trace_id`` from the parnent. + # Children take `trace_id`` from the parent span. span = span.start_child(**kwargs) return span