Skip to content

Commit df95074

Browse files
committed
.
1 parent ddb05b2 commit df95074

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

sentry_sdk/integrations/rq.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@
3535
DEFAULT_TRANSACTION_NAME = "unknown RQ task"
3636

3737

38+
JOB_PROPERTY_TO_ATTRIBUTE = {
39+
"id": "messaging.message.id",
40+
}
41+
42+
QUEUE_PROPERTY_TO_ATTRIBUTE = {
43+
"name": "messaging.destination.name",
44+
}
45+
46+
3847
class RqIntegration(Integration):
3948
identifier = "rq"
4049
origin = f"auto.queue.{identifier}"
@@ -54,8 +63,8 @@ def setup_once():
5463
old_perform_job = Worker.perform_job
5564

5665
@ensure_integration_enabled(RqIntegration, old_perform_job)
57-
def sentry_patched_perform_job(self, job, *args, **kwargs):
58-
# type: (Any, Job, *Queue, **Any) -> bool
66+
def sentry_patched_perform_job(self, job, queue, *args, **kwargs):
67+
# type: (Any, Job, Queue, *Any, **Any) -> bool
5968
with sentry_sdk.new_scope() as scope:
6069
try:
6170
transaction_name = job.func_name or DEFAULT_TRANSACTION_NAME
@@ -76,9 +85,9 @@ def sentry_patched_perform_job(self, job, *args, **kwargs):
7685
name=transaction_name,
7786
source=TRANSACTION_SOURCE_TASK,
7887
origin=RqIntegration.origin,
79-
attributes=_prepopulate_attributes(job),
88+
attributes=_prepopulate_attributes(job, queue),
8089
):
81-
rv = old_perform_job(self, job, *args, **kwargs)
90+
rv = old_perform_job(self, job, queue, *args, **kwargs)
8291

8392
if self.is_horse:
8493
# We're inside of a forked process and RQ is
@@ -169,9 +178,18 @@ def _capture_exception(exc_info, **kwargs):
169178
sentry_sdk.capture_event(event, hint=hint)
170179

171180

172-
JOB_PROPERTY_TO_ATTRIBUTE = {}
181+
def _prepopulate_attributes(job, queue):
182+
# type: (Job, Queue) -> dict[str, Any]
183+
attributes = {
184+
"messaging.system": "rq",
185+
}
186+
187+
for prop, attr in JOB_PROPERTY_TO_ATTRIBUTE.items():
188+
if getattr(job, prop, None) is not None:
189+
attributes[attr] = getattr(job, prop)
173190

191+
for prop, attr in QUEUE_PROPERTY_TO_ATTRIBUTE.items():
192+
if getattr(queue, prop, None) is not None:
193+
attributes[attr] = getattr(queue, prop)
174194

175-
def _prepopulate_attributes(job):
176-
attributes = {}
177195
return attributes

tests/integrations/rq/test_rq.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,12 @@ def test_tracing_enabled(
165165

166166
assert error_event["transaction"] == "tests.integrations.rq.test_rq.crashing_job"
167167
assert transaction["transaction"] == "tests.integrations.rq.test_rq.crashing_job"
168-
assert transaction["contexts"]["trace"] == error_event["contexts"]["trace"]
168+
for trace_key in error_event["contexts"]["trace"]:
169+
assert trace_key in transaction["contexts"]["trace"]
170+
assert (
171+
error_event["contexts"]["trace"][trace_key]
172+
== transaction["contexts"]["trace"][trace_key]
173+
)
169174

170175

171176
def test_tracing_disabled(

0 commit comments

Comments
 (0)