Skip to content

Commit 1578832

Browse files
Add enqueued_at and started_at to rq job extra (#1024)
started_at is not persisted in rq<0.9 so it will be missing in older versions Co-authored-by: Neel Shah <[email protected]>
1 parent c067c33 commit 1578832

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

sentry_sdk/integrations/rq.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
from sentry_sdk.integrations import DidNotEnable, Integration
88
from sentry_sdk.integrations.logging import ignore_logger
99
from sentry_sdk.tracing import Transaction, TRANSACTION_SOURCE_TASK
10-
from sentry_sdk.utils import capture_internal_exceptions, event_from_exception
10+
from sentry_sdk.utils import (
11+
capture_internal_exceptions,
12+
event_from_exception,
13+
format_timestamp,
14+
)
1115

1216
try:
1317
from rq.queue import Queue
@@ -129,6 +133,11 @@ def event_processor(event, hint):
129133
"description": job.description,
130134
}
131135

136+
if job.enqueued_at:
137+
extra["rq-job"]["enqueued_at"] = format_timestamp(job.enqueued_at)
138+
if job.started_at:
139+
extra["rq-job"]["started_at"] = format_timestamp(job.started_at)
140+
132141
if "exc_info" in hint:
133142
with capture_internal_exceptions():
134143
if issubclass(hint["exc_info"][0], JobTimeoutException):

tests/integrations/rq/test_rq.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,18 @@ def test_basic(sentry_init, capture_events):
5858
assert exception["stacktrace"]["frames"][-1]["vars"]["foo"] == "42"
5959

6060
assert event["transaction"] == "tests.integrations.rq.test_rq.crashing_job"
61-
assert event["extra"]["rq-job"] == {
62-
"args": [],
63-
"description": "tests.integrations.rq.test_rq.crashing_job(foo=42)",
64-
"func": "tests.integrations.rq.test_rq.crashing_job",
65-
"job_id": event["extra"]["rq-job"]["job_id"],
66-
"kwargs": {"foo": 42},
67-
}
61+
62+
extra = event["extra"]["rq-job"]
63+
assert extra["args"] == []
64+
assert extra["kwargs"] == {"foo": 42}
65+
assert extra["description"] == "tests.integrations.rq.test_rq.crashing_job(foo=42)"
66+
assert extra["func"] == "tests.integrations.rq.test_rq.crashing_job"
67+
assert "job_id" in extra
68+
assert "enqueued_at" in extra
69+
70+
# older versions don't persist started_at correctly
71+
if tuple(map(int, rq.VERSION.split("."))) >= (0, 9):
72+
assert "started_at" in extra
6873

6974

7075
def test_transport_shutdown(sentry_init, capture_events_forksafe):

0 commit comments

Comments
 (0)