Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 121 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
start_transaction,
)
from sentry_sdk.client import Client, NonRecordingClient
from sentry_sdk.traces import StreamedSpan
from sentry_sdk.tracing import Span
from tests.conftest import TestTransportWithOptions

Expand All @@ -34,7 +35,16 @@ def test_get_current_span():
assert get_current_span(fake_scope) is None


def test_get_current_span_default_hub(sentry_init):
def test_get_current_span_span_streaming():
fake_scope = mock.MagicMock()
fake_scope.streamed_span = mock.MagicMock()
assert sentry_sdk.traces.get_current_span(fake_scope) == fake_scope.streamed_span

fake_scope.streamed_span = None
assert sentry_sdk.traces.get_current_span(fake_scope) is None


def test_get_current_span_current_scope(sentry_init):
sentry_init()

assert get_current_span() is None
Expand All @@ -46,7 +56,19 @@ def test_get_current_span_default_hub(sentry_init):
assert get_current_span() == fake_span


def test_get_current_span_default_hub_with_transaction(sentry_init):
def test_get_current_span_current_scope_span_streaming(sentry_init):
sentry_init(trace_lifecycle="stream")

assert sentry_sdk.traces.get_current_span() is None

scope = get_current_scope()
fake_span = StreamedSpan(name="abc", scope=scope)

assert scope.streamed_span == fake_span
assert sentry_sdk.traces.get_current_span() == fake_span


def test_get_current_span_with_transaction(sentry_init):
sentry_init()

assert get_current_span() is None
Expand All @@ -55,6 +77,15 @@ def test_get_current_span_default_hub_with_transaction(sentry_init):
assert get_current_span() == new_transaction


def test_get_current_span_with_segment(sentry_init):
sentry_init(trace_lifecycle="stream")

assert sentry_sdk.traces.get_current_span() is None

with sentry_sdk.traces.start_span(name="segment") as new_segment:
assert sentry_sdk.traces.get_current_span() == new_segment
Comment thread
sentrivana marked this conversation as resolved.


def test_traceparent_with_tracing_enabled(sentry_init):
sentry_init(traces_sample_rate=1.0)

Expand All @@ -66,6 +97,17 @@ def test_traceparent_with_tracing_enabled(sentry_init):
assert get_traceparent() == expected_traceparent


def test_traceparent_with_tracing_enabled_span_streaming(sentry_init):
sentry_init(traces_sample_rate=1.0, trace_lifecycle="stream")

with sentry_sdk.traces.start_span(name="span") as segment:
expected_traceparent = "%s-%s-1" % (
segment.trace_id,
segment.span_id,
)
assert get_traceparent() == expected_traceparent


def test_traceparent_with_tracing_disabled(sentry_init):
sentry_init()

Expand All @@ -77,6 +119,17 @@ def test_traceparent_with_tracing_disabled(sentry_init):
assert get_traceparent() == expected_traceparent


def test_traceparent_with_tracing_disabled_span_streaming(sentry_init):
sentry_init(trace_lifecycle="stream")

propagation_context = get_isolation_scope()._propagation_context
expected_traceparent = "%s-%s" % (
propagation_context.trace_id,
propagation_context.span_id,
)
assert get_traceparent() == expected_traceparent


def test_baggage_with_tracing_disabled(sentry_init):
sentry_init(release="1.0.0", environment="dev")
propagation_context = get_isolation_scope()._propagation_context
Expand All @@ -88,6 +141,17 @@ def test_baggage_with_tracing_disabled(sentry_init):
assert get_baggage() == expected_baggage


def test_baggage_with_tracing_disabled_span_streaming(sentry_init):
sentry_init(release="1.0.0", environment="dev", trace_lifecycle="stream")
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


def test_baggage_with_tracing_enabled(sentry_init):
sentry_init(traces_sample_rate=1.0, release="1.0.0", environment="dev")
with start_transaction() as transaction:
Expand All @@ -97,6 +161,20 @@ def test_baggage_with_tracing_enabled(sentry_init):
assert re.match(expected_baggage_re, get_baggage())


def test_baggage_with_tracing_enabled_span_streaming(sentry_init):
sentry_init(
traces_sample_rate=1.0,
trace_lifecycle="stream",
release="1.0.0",
environment="dev",
)
with sentry_sdk.traces.start_span(name="segment") as segment:
expected_baggage_re = r"^sentry-trace_id={},sentry-sample_rand=0\.\d{{6}},sentry-environment=dev,sentry-release=1\.0\.0,sentry-transaction=segment,sentry-sample_rate=1\.0,sentry-sampled={}$".format(
segment.trace_id, "true" if segment.sampled else "false"
)
assert re.match(expected_baggage_re, get_baggage())


def test_baggage_with_dsn(sentry_init):
sentry_init(
dsn="http://97333d956c9e40989a0139756c121c34@sentry-x.sentry-y.s.c.local/976543210",
Expand All @@ -112,6 +190,22 @@ def test_baggage_with_dsn(sentry_init):
assert re.match(expected_baggage_re, get_baggage())


def test_baggage_with_dsn_span_streaming(sentry_init):
sentry_init(
dsn="http://97333d956c9e40989a0139756c121c34@sentry-x.sentry-y.s.c.local/976543210",
traces_sample_rate=1.0,
trace_lifecycle="stream",
release="2.0.0",
environment="dev",
transport=TestTransportWithOptions,
)
with sentry_sdk.traces.start_span(name="segment") as segment:
expected_baggage_re = r"^sentry-trace_id={},sentry-sample_rand=0\.\d{{6}},sentry-environment=dev,sentry-release=2\.0\.0,sentry-public_key=97333d956c9e40989a0139756c121c34,sentry-transaction=segment,sentry-sample_rate=1\.0,sentry-sampled={}$".format(
segment.trace_id, "true" if segment.sampled else "false"
)
assert re.match(expected_baggage_re, get_baggage())


def test_continue_trace(sentry_init):
sentry_init()

Expand All @@ -138,6 +232,31 @@ def test_continue_trace(sentry_init):
}


def test_continue_trace_span_streaming(sentry_init):
sentry_init(trace_lifecycle="stream")

trace_id = "471a43a4192642f0b136d5159a501701"
parent_span_id = "6e8f22c393e68f19"
parent_sampled = 1
sentry_sdk.traces.continue_trace(
{
"sentry-trace": "{}-{}-{}".format(trace_id, parent_span_id, parent_sampled),
"baggage": "sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-sample_rand=0.123456",
}
)
with sentry_sdk.traces.start_span(name="some name") as segment:
assert segment.name == "some name"

propagation_context = get_isolation_scope()._propagation_context
assert propagation_context.trace_id == segment.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",
"sample_rand": "0.123456",
}


def test_is_initialized():
assert not is_initialized()

Expand Down
Loading