Skip to content

Commit

Permalink
feat: Send Celery retry count
Browse files Browse the repository at this point in the history
Send the retry count of a Celery task as a span data attribute on the "queue.process" span. Also, add tests for this feature.

Ref #2961
  • Loading branch information
szokeasaurusrex committed May 15, 2024
1 parent 72ac630 commit ff10b77
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ class SPANDATA:
e.g. the queue name or topic.
"""

MESSAGING_MESSAGE_RETRY_COUNT = "messaging.message.retry.count"
"""
Number of retries/attempts to process a message.
"""

SERVER_ADDRESS = "server.address"
"""
Name of the database host.
Expand Down
5 changes: 5 additions & 0 deletions sentry_sdk/integrations/celery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ def _inner(*args, **kwargs):
op=OP.QUEUE_PROCESS, description=task.name
) as span:
_set_messaging_destination_name(task, span)
with capture_internal_exceptions():
span.set_data(
SPANDATA.MESSAGING_MESSAGE_RETRY_COUNT, task.request.retries
)

return f(*args, **kwargs)
except Exception:
exc_info = sys.exc_info()
Expand Down
31 changes: 31 additions & 0 deletions tests/integrations/celery/test_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,34 @@ def task(): ...
(event,) = events
(span,) = event["spans"]
assert "messaging.destination.name" not in span["data"]


def test_retry_count_zero(init_celery, capture_events):
celery = init_celery(enable_tracing=True)
events = capture_events()

@celery.task()
def task(): ...

task.apply_async()

(event,) = events
(span,) = event["spans"]
assert span["data"]["messaging.message.retry.count"] == 0


@mock.patch("celery.app.task.Task.request")
def test_retry_count_nonzero(mock_request, init_celery, capture_events):
mock_request.retries = 3

celery = init_celery(enable_tracing=True)
events = capture_events()

@celery.task()
def task(): ...

task.apply_async()

(event,) = events
(span,) = event["spans"]
assert span["data"]["messaging.message.retry.count"] == 3

0 comments on commit ff10b77

Please sign in to comment.