|
1 | 1 | import pytest
|
2 | 2 |
|
3 |
| -from sentry_sdk import start_span, start_transaction |
4 |
| -from sentry_sdk.tracing import Transaction |
| 3 | +from sentry_sdk import Hub, start_span, start_transaction |
| 4 | +from sentry_sdk.tracing import Span, Transaction |
5 | 5 |
|
6 | 6 |
|
7 | 7 | def test_span_trimming(sentry_init, capture_events):
|
@@ -49,3 +49,82 @@ def test_transaction_method_signature(sentry_init, capture_events):
|
49 | 49 | with start_transaction(Transaction(name="c")):
|
50 | 50 | pass
|
51 | 51 | assert len(events) == 4
|
| 52 | + |
| 53 | + |
| 54 | +def test_finds_transaction_on_scope(sentry_init): |
| 55 | + sentry_init(traces_sample_rate=1.0) |
| 56 | + |
| 57 | + transaction = start_transaction(name="dogpark") |
| 58 | + |
| 59 | + scope = Hub.current.scope |
| 60 | + |
| 61 | + # See note in Scope class re: getters and setters of the `transaction` |
| 62 | + # property. For the moment, assigning to scope.transaction merely sets the |
| 63 | + # transaction name, rather than putting the transaction on the scope, so we |
| 64 | + # have to assign to _span directly. |
| 65 | + scope._span = transaction |
| 66 | + |
| 67 | + # Reading scope.property, however, does what you'd expect, and returns the |
| 68 | + # transaction on the scope. |
| 69 | + assert scope.transaction is not None |
| 70 | + assert isinstance(scope.transaction, Transaction) |
| 71 | + assert scope.transaction.name == "dogpark" |
| 72 | + |
| 73 | + # If the transaction is also set as the span on the scope, it can be found |
| 74 | + # by accessing _span, too. |
| 75 | + assert scope._span is not None |
| 76 | + assert isinstance(scope._span, Transaction) |
| 77 | + assert scope._span.name == "dogpark" |
| 78 | + |
| 79 | + |
| 80 | +def test_finds_transaction_when_decedent_span_is_on_scope( |
| 81 | + sentry_init, |
| 82 | +): |
| 83 | + sentry_init(traces_sample_rate=1.0) |
| 84 | + |
| 85 | + transaction = start_transaction(name="dogpark") |
| 86 | + child_span = transaction.start_child(op="sniffing") |
| 87 | + |
| 88 | + scope = Hub.current.scope |
| 89 | + scope._span = child_span |
| 90 | + |
| 91 | + # this is the same whether it's the transaction itself or one of its |
| 92 | + # decedents directly attached to the scope |
| 93 | + assert scope.transaction is not None |
| 94 | + assert isinstance(scope.transaction, Transaction) |
| 95 | + assert scope.transaction.name == "dogpark" |
| 96 | + |
| 97 | + # here we see that it is in fact the span on the scope, rather than the |
| 98 | + # transaction itself |
| 99 | + assert scope._span is not None |
| 100 | + assert isinstance(scope._span, Span) |
| 101 | + assert scope._span.op == "sniffing" |
| 102 | + |
| 103 | + |
| 104 | +def test_finds_orphan_span_on_scope(sentry_init): |
| 105 | + # this is deprecated behavior which may be removed at some point (along with |
| 106 | + # the start_span function) |
| 107 | + sentry_init(traces_sample_rate=1.0) |
| 108 | + |
| 109 | + span = start_span(op="sniffing") |
| 110 | + |
| 111 | + scope = Hub.current.scope |
| 112 | + scope._span = span |
| 113 | + |
| 114 | + assert scope._span is not None |
| 115 | + assert isinstance(scope._span, Span) |
| 116 | + assert scope._span.op == "sniffing" |
| 117 | + |
| 118 | + |
| 119 | +def test_finds_non_orphan_span_on_scope(sentry_init): |
| 120 | + sentry_init(traces_sample_rate=1.0) |
| 121 | + |
| 122 | + transaction = start_transaction(name="dogpark") |
| 123 | + child_span = transaction.start_child(op="sniffing") |
| 124 | + |
| 125 | + scope = Hub.current.scope |
| 126 | + scope._span = child_span |
| 127 | + |
| 128 | + assert scope._span is not None |
| 129 | + assert isinstance(scope._span, Span) |
| 130 | + assert scope._span.op == "sniffing" |
0 commit comments