Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(tests): Split up tracing tests #857

Merged
merged 3 commits into from Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions tests/tracing/test_deprecated.py
@@ -0,0 +1,20 @@
from sentry_sdk import start_span

from sentry_sdk.tracing import Span


def test_start_span_to_start_transaction(sentry_init, capture_events):
# XXX: this only exists for backwards compatibility with code before
# Transaction / start_transaction were introduced.
sentry_init(traces_sample_rate=1.0)
events = capture_events()

with start_span(transaction="/1/"):
pass

with start_span(Span(transaction="/2/")):
pass

assert len(events) == 2
assert events[0]["transaction"] == "/1/"
assert events[1]["transaction"] == "/2/"
107 changes: 1 addition & 106 deletions tests/test_tracing.py → tests/tracing/test_integration_tests.py
Expand Up @@ -10,7 +10,7 @@
start_span,
start_transaction,
)
from sentry_sdk.tracing import Span, Transaction
from sentry_sdk.tracing import Transaction


@pytest.mark.parametrize("sample_rate", [0.0, 1.0])
Expand Down Expand Up @@ -46,23 +46,6 @@ def test_basic(sentry_init, capture_events, sample_rate):
assert not events


def test_start_span_to_start_transaction(sentry_init, capture_events):
# XXX: this only exists for backwards compatibility with code before
# Transaction / start_transaction were introduced.
sentry_init(traces_sample_rate=1.0)
events = capture_events()

with start_span(transaction="/1/"):
pass

with start_span(Span(transaction="/2/")):
pass

assert len(events) == 2
assert events[0]["transaction"] == "/1/"
assert events[1]["transaction"] == "/2/"


@pytest.mark.parametrize("sampled", [True, False, None])
def test_continue_from_headers(sentry_init, capture_events, sampled):
sentry_init(traces_sample_rate=1.0)
Expand Down Expand Up @@ -114,19 +97,6 @@ def test_continue_from_headers(sentry_init, capture_events, sampled):
assert message["message"] == "hello"


def test_sampling_decided_only_for_transactions(sentry_init, capture_events):
sentry_init(traces_sample_rate=0.5)

with start_transaction(name="hi") as transaction:
assert transaction.sampled is not None

with start_span() as span:
assert span.sampled == transaction.sampled

with start_span() as span:
assert span.sampled is None


@pytest.mark.parametrize(
"args,expected_refcount",
[({"traces_sample_rate": 1.0}, 100), ({"traces_sample_rate": 0.0}, 0)],
Expand Down Expand Up @@ -156,67 +126,6 @@ def foo():
assert len(references) == expected_refcount


def test_span_trimming(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0, _experiments={"max_spans": 3})
events = capture_events()

with start_transaction(name="hi"):
for i in range(10):
with start_span(op="foo{}".format(i)):
pass

(event,) = events
span1, span2 = event["spans"]
assert span1["op"] == "foo0"
assert span2["op"] == "foo1"


def test_nested_transaction_sampling_override():
with start_transaction(name="outer", sampled=True) as outer_transaction:
assert outer_transaction.sampled is True
with start_transaction(name="inner", sampled=False) as inner_transaction:
assert inner_transaction.sampled is False
assert outer_transaction.sampled is True


def test_transaction_method_signature(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0)
events = capture_events()

with pytest.raises(TypeError):
start_span(name="foo")
assert len(events) == 0

with start_transaction() as transaction:
pass
assert transaction.name == "<unlabeled transaction>"
assert len(events) == 1

with start_transaction() as transaction:
transaction.name = "name-known-after-transaction-started"
assert len(events) == 2

with start_transaction(name="a"):
pass
assert len(events) == 3

with start_transaction(Transaction(name="c")):
pass
assert len(events) == 4


def test_no_double_sampling(sentry_init, capture_events):
# Transactions should not be subject to the global/error sample rate.
# Only the traces_sample_rate should apply.
sentry_init(traces_sample_rate=1.0, sample_rate=0.0)
events = capture_events()

with start_transaction(name="/"):
pass

assert len(events) == 1


def test_transactions_do_not_go_through_before_send(sentry_init, capture_events):
def before_send(event, hint):
raise RuntimeError("should not be called")
Expand All @@ -228,17 +137,3 @@ def before_send(event, hint):
pass

assert len(events) == 1


def test_get_transaction_from_scope(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0)
events = capture_events()

with start_transaction(name="/"):
with start_span(op="child-span"):
with start_span(op="child-child-span"):
scope = Hub.current.scope
assert scope.span.op == "child-child-span"
assert scope.transaction.name == "/"

assert len(events) == 1
45 changes: 45 additions & 0 deletions tests/tracing/test_misc.py
@@ -0,0 +1,45 @@
import pytest

from sentry_sdk import start_span, start_transaction
from sentry_sdk.tracing import Transaction


def test_span_trimming(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0, _experiments={"max_spans": 3})
events = capture_events()

with start_transaction(name="hi"):
for i in range(10):
with start_span(op="foo{}".format(i)):
pass

(event,) = events
span1, span2 = event["spans"]
assert span1["op"] == "foo0"
assert span2["op"] == "foo1"


def test_transaction_method_signature(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0)
events = capture_events()

with pytest.raises(TypeError):
start_span(name="foo")
assert len(events) == 0

with start_transaction() as transaction:
pass
assert transaction.name == "<unlabeled transaction>"
assert len(events) == 1

with start_transaction() as transaction:
transaction.name = "name-known-after-transaction-started"
assert len(events) == 2

with start_transaction(name="a"):
pass
assert len(events) == 3

with start_transaction(Transaction(name="c")):
pass
assert len(events) == 4
34 changes: 34 additions & 0 deletions tests/tracing/test_sampling.py
@@ -0,0 +1,34 @@
from sentry_sdk import start_span, start_transaction


def test_sampling_decided_only_for_transactions(sentry_init, capture_events):
sentry_init(traces_sample_rate=0.5)

with start_transaction(name="hi") as transaction:
assert transaction.sampled is not None

with start_span() as span:
assert span.sampled == transaction.sampled

with start_span() as span:
assert span.sampled is None


def test_nested_transaction_sampling_override():
with start_transaction(name="outer", sampled=True) as outer_transaction:
assert outer_transaction.sampled is True
with start_transaction(name="inner", sampled=False) as inner_transaction:
assert inner_transaction.sampled is False
assert outer_transaction.sampled is True


def test_no_double_sampling(sentry_init, capture_events):
# Transactions should not be subject to the global/error sample rate.
# Only the traces_sample_rate should apply.
sentry_init(traces_sample_rate=1.0, sample_rate=0.0)
events = capture_events()

with start_transaction(name="/"):
pass

assert len(events) == 1