Skip to content

Commit

Permalink
Fix test_api (#3787)
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py authored Nov 15, 2024
1 parent c29382d commit aa9c5ca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,3 +642,18 @@ def __eq__(self, other):

def __ne__(self, other):
return not self.__eq__(other)


@pytest.fixture(name="SortedBaggage")
def sorted_baggage_matcher():
class SortedBaggage:
def __init__(self, baggage):
self.baggage = baggage

def __eq__(self, other):
return sorted(self.baggage.split(",")) == sorted(other.split(","))

def __ne__(self, other):
return not self.__eq__(other)

return SortedBaggage
54 changes: 27 additions & 27 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
get_current_span,
get_traceparent,
is_initialized,
start_transaction,
start_span,
set_tags,
get_global_scope,
get_current_scope,
Expand Down Expand Up @@ -43,23 +43,23 @@ def test_get_current_span_current_scope(sentry_init):


@pytest.mark.forked
def test_get_current_span_current_scope_with_transaction(sentry_init):
def test_get_current_span_current_scope_with_span(sentry_init):
sentry_init()

assert get_current_span() is None

with start_transaction() as new_transaction:
assert get_current_span() == new_transaction
with start_span() as new_span:
assert get_current_span() == new_span


@pytest.mark.forked
def test_traceparent_with_tracing_enabled(sentry_init):
sentry_init(traces_sample_rate=1.0)

with start_transaction() as transaction:
with start_span() as span:
expected_traceparent = "%s-%s-1" % (
transaction.trace_id,
transaction.span_id,
span.trace_id,
span.span_id,
)
assert get_traceparent() == expected_traceparent

Expand All @@ -77,51 +77,51 @@ def test_traceparent_with_tracing_disabled(sentry_init):


@pytest.mark.forked
def test_baggage_with_tracing_disabled(sentry_init):
def test_baggage_with_tracing_disabled(sentry_init, SortedBaggage):
sentry_init(release="1.0.0", environment="dev")
propagation_context = get_isolation_scope()._propagation_context
expected_baggage = (
"sentry-trace_id={},sentry-environment=dev,sentry-release=1.0.0".format(
propagation_context.trace_id
)
)
assert get_baggage() == expected_baggage
assert get_baggage() == SortedBaggage(expected_baggage)


@pytest.mark.forked
def test_baggage_with_tracing_enabled(sentry_init):
def test_baggage_with_tracing_enabled(sentry_init, SortedBaggage):
sentry_init(traces_sample_rate=1.0, release="1.0.0", environment="dev")
with start_transaction() as transaction:
with start_span() as span:
expected_baggage = "sentry-trace_id={},sentry-environment=dev,sentry-release=1.0.0,sentry-sample_rate=1.0,sentry-sampled={}".format(
transaction.trace_id, "true" if transaction.sampled else "false"
span.trace_id, "true" if span.sampled else "false"
)
assert get_baggage() == expected_baggage
assert get_baggage() == SortedBaggage(expected_baggage)


@pytest.mark.forked
def test_continue_trace(sentry_init):
sentry_init()
sentry_init(traces_sample_rate=1.0)

trace_id = "471a43a4192642f0b136d5159a501701"
parent_span_id = "6e8f22c393e68f19"
parent_sampled = 1
transaction = continue_trace(

with continue_trace(
{
"sentry-trace": "{}-{}-{}".format(trace_id, parent_span_id, parent_sampled),
"baggage": "sentry-trace_id=566e3688a61d4bc888951642d6f14a19",
},
name="some name",
)
with start_transaction(transaction):
assert transaction.name == "some name"

propagation_context = 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
assert propagation_context.dynamic_sampling_context == {
"trace_id": "566e3688a61d4bc888951642d6f14a19"
}
):
with start_span(name="some name") as span:
assert span.name == "some name"

propagation_context = get_isolation_scope()._propagation_context
assert propagation_context.trace_id == span.trace_id == trace_id
assert propagation_context.parent_span_id == parent_span_id
assert propagation_context.parent_sampled == parent_sampled
assert propagation_context.dynamic_sampling_context == {
"trace_id": "566e3688a61d4bc888951642d6f14a19"
}


@pytest.mark.forked
Expand Down

0 comments on commit aa9c5ca

Please sign in to comment.