Skip to content

Commit 2088fc5

Browse files
feat(rq): Set messaging.destination.name on consumer spans (#6774)
Set `Queue.name` as the `messaging.destination.name` attribute to support span description inference.
1 parent 70292ef commit 2088fc5

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

sentry_sdk/integrations/rq.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ def setup_once() -> None:
6565

6666
@functools.wraps(old_perform_job)
6767
def sentry_patched_perform_job(
68-
self: "Any", job: "Job", *args: "Queue", **kwargs: "Any"
68+
self: "Any", job: "Job", queue: "Queue", *args: "Any", **kwargs: "Any"
6969
) -> bool:
7070
client = sentry_sdk.get_client()
7171
if client.get_integration(RqIntegration) is None:
72-
return old_perform_job(self, job, *args, **kwargs)
72+
return old_perform_job(self, job, queue, *args, **kwargs)
7373

7474
with sentry_sdk.new_scope() as scope:
7575
scope.clear_breadcrumbs()
@@ -93,13 +93,14 @@ def sentry_patched_perform_job(
9393
"sentry.origin": RqIntegration.origin,
9494
"sentry.span.source": SegmentSource.TASK,
9595
SPANDATA.MESSAGING_MESSAGE_ID: job.id,
96+
SPANDATA.MESSAGING_DESTINATION_NAME: queue.name,
9697
},
9798
parent_span=None,
9899
) as span:
99100
if func_name is not None:
100101
span.set_attribute(SPANDATA.CODE_FUNCTION_NAME, func_name)
101102

102-
rv = old_perform_job(self, job, *args, **kwargs)
103+
rv = old_perform_job(self, job, queue, *args, **kwargs)
103104
else:
104105
transaction = continue_trace(
105106
job.meta.get("_sentry_trace_headers") or {},
@@ -115,8 +116,10 @@ def sentry_patched_perform_job(
115116
with sentry_sdk.start_transaction(
116117
transaction,
117118
custom_sampling_context={"rq_job": job},
118-
):
119-
rv = old_perform_job(self, job, *args, **kwargs)
119+
) as span:
120+
span.set_data(SPANDATA.MESSAGING_DESTINATION_NAME, queue.name)
121+
122+
rv = old_perform_job(self, job, queue, *args, **kwargs)
120123

121124
if self.is_horse:
122125
# We're inside of a forked process and RQ is

tests/integrations/rq/test_rq.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ def test_transaction_no_error(
416416

417417
assert span["attributes"]["sentry.op"] == "queue.task.rq"
418418
assert span["name"] == "tests.integrations.rq.test_rq.do_trick"
419+
assert span["attributes"][SPANDATA.MESSAGING_DESTINATION_NAME] == queue.name
419420
else:
420421
events = capture_events()
421422

@@ -427,6 +428,10 @@ def test_transaction_no_error(
427428
assert envelope["type"] == "transaction"
428429
assert envelope["contexts"]["trace"]["op"] == "queue.task.rq"
429430
assert envelope["transaction"] == "tests.integrations.rq.test_rq.do_trick"
431+
assert (
432+
envelope["contexts"]["trace"]["data"][SPANDATA.MESSAGING_DESTINATION_NAME]
433+
== queue.name
434+
)
430435
assert envelope["extra"]["rq-job"] == DictionaryContaining(
431436
{
432437
"args": ["Maisey"] if send_default_pii else SENSITIVE_DATA_SUBSTITUTE,

0 commit comments

Comments
 (0)