Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
antonpirker committed Feb 22, 2024
1 parent 8c229f7 commit 4501cba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 37 deletions.
17 changes: 2 additions & 15 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,28 +318,15 @@ def get_traceparent():
"""
Returns the traceparent either from the active span or from the scope.
"""
current_scope = Scope.get_current_scope()
traceparent = current_scope.get_traceparent()

if traceparent is None:
isolation_scope = Scope.get_isolation_scope()
traceparent = isolation_scope.get_traceparent()

return traceparent
return Scope.get_current_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.
"""
current_scope = Scope.get_current_scope()
baggage = current_scope.get_baggage()

if baggage is None:
isolation_scope = Scope.get_isolation_scope()
baggage = isolation_scope.get_baggage()

baggage = Scope.get_current_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 331 in sentry_sdk/api.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/api.py#L331

Added line #L331 was not covered by tests

Expand Down
36 changes: 20 additions & 16 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,14 +501,16 @@ def get_traceparent(self, *args, **kwargs):
if has_tracing_enabled(client.options) and self.span is not None:
return self.span.to_traceparent()

if self._propagation_context is None:
return None
# If this scope has a propagation context, return traceparent from there
if self._propagation_context is not None:
traceparent = "%s-%s" % (
self._propagation_context["trace_id"],
self._propagation_context["span_id"],
)
return traceparent

traceparent = "%s-%s" % (
self._propagation_context["trace_id"],
self._propagation_context["span_id"],
)
return traceparent
# Fall back to isolation scope's traceparent. It always has one
return Scope.get_isolation_scope().get_traceparent()

def get_baggage(self, *args, **kwargs):
# type: (Any, Any) -> Optional[Baggage]
Expand All @@ -522,16 +524,18 @@ def get_baggage(self, *args, **kwargs):
if has_tracing_enabled(client.options) and self.span is not None:
return self.span.to_baggage()

if self._propagation_context is None:
return None
# If this scope has a propagation context, return baggage from there
if self._propagation_context is not None:
dynamic_sampling_context = self._propagation_context.get(
"dynamic_sampling_context"
)
if dynamic_sampling_context is None:
return Baggage.from_options(self)
else:
return Baggage(dynamic_sampling_context)

dynamic_sampling_context = self._propagation_context.get(
"dynamic_sampling_context"
)
if dynamic_sampling_context is None:
return Baggage.from_options(self)
else:
return Baggage(dynamic_sampling_context)
# Fall back to isolation scope's baggage. It always has one
return Scope.get_isolation_scope().get_baggage()

def get_trace_context(self):
# type: () -> Any
Expand Down
9 changes: 3 additions & 6 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
)

from sentry_sdk.client import Client, NonRecordingClient
from sentry_sdk.hub import Hub
from sentry_sdk.scope import Scope


Expand Down Expand Up @@ -66,7 +65,7 @@ def test_traceparent_with_tracing_enabled(sentry_init):
def test_traceparent_with_tracing_disabled(sentry_init):
sentry_init()

propagation_context = Hub.current.scope._propagation_context
propagation_context = Scope.get_isolation_scope()._propagation_context
expected_traceparent = "%s-%s" % (
propagation_context["trace_id"],
propagation_context["span_id"],
Expand All @@ -77,7 +76,7 @@ def test_traceparent_with_tracing_disabled(sentry_init):
@pytest.mark.forked
def test_baggage_with_tracing_disabled(sentry_init):
sentry_init(release="1.0.0", environment="dev")
propagation_context = Hub.current.scope._propagation_context
propagation_context = Scope.get_isolation_scope()._propagation_context
expected_baggage = (
"sentry-trace_id={},sentry-environment=dev,sentry-release=1.0.0".format(
propagation_context["trace_id"]
Expand Down Expand Up @@ -113,9 +112,7 @@ def test_continue_trace(sentry_init):
with start_transaction(transaction):
assert transaction.name == "some name"

propagation_context = (
Hub.current.scope._propagation_context
) # TODO: because this is the current scope and continue_trace was done on isolation scope.
propagation_context = Scope.get_isolation_scope()._propagation_context
assert propagation_context["trace_id"] == transaction.trace_id == trace_id
assert propagation_context["parent_span_id"] == parent_span_id
assert propagation_context["parent_sampled"] == parent_sampled
Expand Down

0 comments on commit 4501cba

Please sign in to comment.