From 9a1fbb41599f78af8e52140bc0e4ff8c26ba0f17 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Mon, 11 Nov 2024 15:48:43 +0100 Subject: [PATCH 1/2] Fix transaction name setting and forked some tests to make them work in potel --- sentry_sdk/integrations/rq.py | 19 +++++++++++++------ tests/integrations/rq/test_rq.py | 7 +++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/sentry_sdk/integrations/rq.py b/sentry_sdk/integrations/rq.py index 7e84b15681..7e016bfa9a 100644 --- a/sentry_sdk/integrations/rq.py +++ b/sentry_sdk/integrations/rq.py @@ -32,6 +32,8 @@ from rq.job import Job +DEFAULT_TRANSACTION_NAME = "unknown RQ task" + class RqIntegration(Integration): identifier = "rq" @@ -55,22 +57,27 @@ def setup_once(): def sentry_patched_perform_job(self, job, *args, **kwargs): # type: (Any, Job, *Queue, **Any) -> bool with sentry_sdk.new_scope() as scope: + try: + transaction_name = job.func_name or DEFAULT_TRANSACTION_NAME + except AttributeError: + transaction_name = DEFAULT_TRANSACTION_NAME + + scope.set_transaction_name( + transaction_name, source=TRANSACTION_SOURCE_TASK + ) scope.clear_breadcrumbs() scope.add_event_processor(_make_event_processor(weakref.ref(job))) with sentry_sdk.continue_trace( job.meta.get("_sentry_trace_headers") or {} ): - with sentry_sdk.start_transaction( + with sentry_sdk.start_span( op=OP.QUEUE_TASK_RQ, - name="unknown RQ task", + name=transaction_name, source=TRANSACTION_SOURCE_TASK, origin=RqIntegration.origin, custom_sampling_context={"rq_job": job}, - ) as transaction: - with capture_internal_exceptions(): - transaction.name = job.func_name - + ): rv = old_perform_job(self, job, *args, **kwargs) if self.is_horse: diff --git a/tests/integrations/rq/test_rq.py b/tests/integrations/rq/test_rq.py index ffd6f458e1..3ce6197554 100644 --- a/tests/integrations/rq/test_rq.py +++ b/tests/integrations/rq/test_rq.py @@ -46,6 +46,7 @@ def do_trick(dog, trick): return "{}, can you {}? Good dog!".format(dog, trick) +@pytest.mark.forked def test_basic(sentry_init, capture_events): sentry_init(integrations=[RqIntegration()]) events = capture_events() @@ -78,6 +79,7 @@ def test_basic(sentry_init, capture_events): assert "started_at" in extra +@pytest.mark.forked def test_transport_shutdown(sentry_init, capture_events_forksafe): sentry_init(integrations=[RqIntegration()]) @@ -96,6 +98,7 @@ def test_transport_shutdown(sentry_init, capture_events_forksafe): assert exception["type"] == "ZeroDivisionError" +@pytest.mark.forked def test_transaction_with_error( sentry_init, capture_events, DictionaryContaining # noqa:N803 ): @@ -131,6 +134,7 @@ def test_transaction_with_error( ) +@pytest.mark.forked def test_error_has_trace_context_if_tracing_disabled( sentry_init, capture_events, @@ -149,6 +153,7 @@ def test_error_has_trace_context_if_tracing_disabled( assert error_event["contexts"]["trace"] +@pytest.mark.forked def test_tracing_enabled( sentry_init, capture_events, @@ -171,6 +176,7 @@ def test_tracing_enabled( assert envelope["contexts"]["trace"] == error_event["contexts"]["trace"] +@pytest.mark.forked def test_tracing_disabled( sentry_init, capture_events, @@ -251,6 +257,7 @@ def test_traces_sampler_gets_correct_values_in_sampling_context( ) +@pytest.mark.forked @pytest.mark.skipif( parse_version(rq.__version__) < (1, 5), reason="At least rq-1.5 required" ) From e46aa88a82da95afce56ca0bab8149513e55ce2c Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Mon, 11 Nov 2024 16:00:16 +0100 Subject: [PATCH 2/2] Transactions in transactins is undefined behavior, so remove this. --- sentry_sdk/integrations/arq.py | 3 ++- tests/integrations/opentelemetry/test_utils.py | 4 +--- tests/integrations/rq/test_rq.py | 13 +++++-------- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/sentry_sdk/integrations/arq.py b/sentry_sdk/integrations/arq.py index 097a89eb22..b0f9a6a443 100644 --- a/sentry_sdk/integrations/arq.py +++ b/sentry_sdk/integrations/arq.py @@ -104,7 +104,8 @@ async def _sentry_run_job(self, job_id, score): with sentry_sdk.isolation_scope() as scope: scope._name = "arq" scope.set_transaction_name( - DEFAULT_TRANSACTION_NAME, source=TRANSACTION_SOURCE_TASK, + DEFAULT_TRANSACTION_NAME, + source=TRANSACTION_SOURCE_TASK, ) scope.clear_breadcrumbs() diff --git a/tests/integrations/opentelemetry/test_utils.py b/tests/integrations/opentelemetry/test_utils.py index b6f1072480..fde66bf590 100644 --- a/tests/integrations/opentelemetry/test_utils.py +++ b/tests/integrations/opentelemetry/test_utils.py @@ -334,9 +334,7 @@ def test_span_data_for_db_query(): ), ( SpanKind.SERVER, - Status( - StatusCode.ERROR, "I'm a teapot" - ), + Status(StatusCode.ERROR, "I'm a teapot"), { "http.method": "POST", "http.route": "/some/route", diff --git a/tests/integrations/rq/test_rq.py b/tests/integrations/rq/test_rq.py index 3ce6197554..f8bb8f0fe0 100644 --- a/tests/integrations/rq/test_rq.py +++ b/tests/integrations/rq/test_rq.py @@ -5,7 +5,6 @@ from fakeredis import FakeStrictRedis import sentry_sdk -from sentry_sdk import start_transaction from sentry_sdk.integrations.rq import RqIntegration from sentry_sdk.utils import parse_version @@ -164,16 +163,14 @@ def test_tracing_enabled( queue = rq.Queue(connection=FakeStrictRedis()) worker = rq.SimpleWorker([queue], connection=queue.connection) - with start_transaction(op="rq transaction") as transaction: - queue.enqueue(crashing_job, foo=None) - worker.work(burst=True) + queue.enqueue(crashing_job, foo=None) + worker.work(burst=True) - error_event, envelope, _ = events + error_event, transaction = events assert error_event["transaction"] == "tests.integrations.rq.test_rq.crashing_job" - assert error_event["contexts"]["trace"]["trace_id"] == transaction.trace_id - - assert envelope["contexts"]["trace"] == error_event["contexts"]["trace"] + assert transaction["transaction"] == "tests.integrations.rq.test_rq.crashing_job" + assert transaction["contexts"]["trace"] == error_event["contexts"]["trace"] @pytest.mark.forked