Skip to content

Commit 665b4a5

Browse files
authored
Fix rq tests (#3762)
* Fix transaction name setting and forked some tests to make them work in potel * Transactions in transactins is undefined behavior, so remove this.
1 parent fbbf5e7 commit 665b4a5

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

sentry_sdk/integrations/arq.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ async def _sentry_run_job(self, job_id, score):
104104
with sentry_sdk.isolation_scope() as scope:
105105
scope._name = "arq"
106106
scope.set_transaction_name(
107-
DEFAULT_TRANSACTION_NAME, source=TRANSACTION_SOURCE_TASK,
107+
DEFAULT_TRANSACTION_NAME,
108+
source=TRANSACTION_SOURCE_TASK,
108109
)
109110
scope.clear_breadcrumbs()
110111

sentry_sdk/integrations/rq.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
from rq.job import Job
3434

35+
DEFAULT_TRANSACTION_NAME = "unknown RQ task"
36+
3537

3638
class RqIntegration(Integration):
3739
identifier = "rq"
@@ -55,22 +57,27 @@ def setup_once():
5557
def sentry_patched_perform_job(self, job, *args, **kwargs):
5658
# type: (Any, Job, *Queue, **Any) -> bool
5759
with sentry_sdk.new_scope() as scope:
60+
try:
61+
transaction_name = job.func_name or DEFAULT_TRANSACTION_NAME
62+
except AttributeError:
63+
transaction_name = DEFAULT_TRANSACTION_NAME
64+
65+
scope.set_transaction_name(
66+
transaction_name, source=TRANSACTION_SOURCE_TASK
67+
)
5868
scope.clear_breadcrumbs()
5969
scope.add_event_processor(_make_event_processor(weakref.ref(job)))
6070

6171
with sentry_sdk.continue_trace(
6272
job.meta.get("_sentry_trace_headers") or {}
6373
):
64-
with sentry_sdk.start_transaction(
74+
with sentry_sdk.start_span(
6575
op=OP.QUEUE_TASK_RQ,
66-
name="unknown RQ task",
76+
name=transaction_name,
6777
source=TRANSACTION_SOURCE_TASK,
6878
origin=RqIntegration.origin,
6979
custom_sampling_context={"rq_job": job},
70-
) as transaction:
71-
with capture_internal_exceptions():
72-
transaction.name = job.func_name
73-
80+
):
7481
rv = old_perform_job(self, job, *args, **kwargs)
7582

7683
if self.is_horse:

tests/integrations/opentelemetry/test_utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,7 @@ def test_span_data_for_db_query():
334334
),
335335
(
336336
SpanKind.SERVER,
337-
Status(
338-
StatusCode.ERROR, "I'm a teapot"
339-
),
337+
Status(StatusCode.ERROR, "I'm a teapot"),
340338
{
341339
"http.method": "POST",
342340
"http.route": "/some/route",

tests/integrations/rq/test_rq.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from fakeredis import FakeStrictRedis
66

77
import sentry_sdk
8-
from sentry_sdk import start_transaction
98
from sentry_sdk.integrations.rq import RqIntegration
109
from sentry_sdk.utils import parse_version
1110

@@ -46,6 +45,7 @@ def do_trick(dog, trick):
4645
return "{}, can you {}? Good dog!".format(dog, trick)
4746

4847

48+
@pytest.mark.forked
4949
def test_basic(sentry_init, capture_events):
5050
sentry_init(integrations=[RqIntegration()])
5151
events = capture_events()
@@ -78,6 +78,7 @@ def test_basic(sentry_init, capture_events):
7878
assert "started_at" in extra
7979

8080

81+
@pytest.mark.forked
8182
def test_transport_shutdown(sentry_init, capture_events_forksafe):
8283
sentry_init(integrations=[RqIntegration()])
8384

@@ -96,6 +97,7 @@ def test_transport_shutdown(sentry_init, capture_events_forksafe):
9697
assert exception["type"] == "ZeroDivisionError"
9798

9899

100+
@pytest.mark.forked
99101
def test_transaction_with_error(
100102
sentry_init, capture_events, DictionaryContaining # noqa:N803
101103
):
@@ -131,6 +133,7 @@ def test_transaction_with_error(
131133
)
132134

133135

136+
@pytest.mark.forked
134137
def test_error_has_trace_context_if_tracing_disabled(
135138
sentry_init,
136139
capture_events,
@@ -149,6 +152,7 @@ def test_error_has_trace_context_if_tracing_disabled(
149152
assert error_event["contexts"]["trace"]
150153

151154

155+
@pytest.mark.forked
152156
def test_tracing_enabled(
153157
sentry_init,
154158
capture_events,
@@ -159,18 +163,17 @@ def test_tracing_enabled(
159163
queue = rq.Queue(connection=FakeStrictRedis())
160164
worker = rq.SimpleWorker([queue], connection=queue.connection)
161165

162-
with start_transaction(op="rq transaction") as transaction:
163-
queue.enqueue(crashing_job, foo=None)
164-
worker.work(burst=True)
166+
queue.enqueue(crashing_job, foo=None)
167+
worker.work(burst=True)
165168

166-
error_event, envelope, _ = events
169+
error_event, transaction = events
167170

168171
assert error_event["transaction"] == "tests.integrations.rq.test_rq.crashing_job"
169-
assert error_event["contexts"]["trace"]["trace_id"] == transaction.trace_id
170-
171-
assert envelope["contexts"]["trace"] == error_event["contexts"]["trace"]
172+
assert transaction["transaction"] == "tests.integrations.rq.test_rq.crashing_job"
173+
assert transaction["contexts"]["trace"] == error_event["contexts"]["trace"]
172174

173175

176+
@pytest.mark.forked
174177
def test_tracing_disabled(
175178
sentry_init,
176179
capture_events,
@@ -251,6 +254,7 @@ def test_traces_sampler_gets_correct_values_in_sampling_context(
251254
)
252255

253256

257+
@pytest.mark.forked
254258
@pytest.mark.skipif(
255259
parse_version(rq.__version__) < (1, 5), reason="At least rq-1.5 required"
256260
)

0 commit comments

Comments
 (0)