Skip to content

Commit

Permalink
transaction is on isolation scope, span on corrent scope.
Browse files Browse the repository at this point in the history
  • Loading branch information
antonpirker committed Feb 22, 2024
1 parent 5622978 commit 62d792d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
8 changes: 4 additions & 4 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=""):
Expand All @@ -318,15 +318,15 @@ 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()

Check warning on line 321 in sentry_sdk/api.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/api.py#L321

Added line #L321 was not covered by tests


def get_baggage():
# type: () -> Optional[str]
"""
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()

Check warning on line 329 in sentry_sdk/api.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/api.py#L329

Added line #L329 was not covered by tests

if baggage is not None:
return baggage.serialize()

Check warning on line 332 in sentry_sdk/api.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/api.py#L332

Added line #L332 was not covered by tests
Expand All @@ -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
)
27 changes: 21 additions & 6 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

Check warning on line 622 in sentry_sdk/scope.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/scope.py#L622

Added line #L622 was not covered by tests

current_scope = Scope.get_current_scope()
if current_scope._propagation_context is not None:
return current_scope._propagation_context

Check warning on line 626 in sentry_sdk/scope.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/scope.py#L626

Added line #L626 was not covered by tests

isolation_scope = Scope.get_isolation_scope()
if isolation_scope._propagation_context is not None:
return isolation_scope._propagation_context

return {}

Check warning on line 632 in sentry_sdk/scope.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/scope.py#L632

Added line #L632 was not covered by tests

def clear(self):
# type: () -> None
"""Clears the entire scope."""
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 62d792d

Please sign in to comment.