Skip to content

Commit

Permalink
feat(celery): Set task ID on span
Browse files Browse the repository at this point in the history
Set Celery task ID on the "queue.process" span, and add tests for this new behavior.

Closes #2974
  • Loading branch information
szokeasaurusrex committed May 15, 2024
1 parent ff10b77 commit 68332d8
Show file tree
Hide file tree
Showing 3 changed files with 21 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_ID = "messaging.message.id"
"""
The message's identifier.
"""

MESSAGING_MESSAGE_RETRY_COUNT = "messaging.message.retry.count"
"""
Number of retries/attempts to process a message.
Expand Down
2 changes: 2 additions & 0 deletions sentry_sdk/integrations/celery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ 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_ID, task.request.id)
with capture_internal_exceptions():
span.set_data(
SPANDATA.MESSAGING_MESSAGE_RETRY_COUNT, task.request.retries
Expand Down
14 changes: 14 additions & 0 deletions tests/integrations/celery/test_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,20 @@ def task(): ...
assert "messaging.destination.name" not in span["data"]


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

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

example_task.apply_async()

(event,) = events
(span,) = event["spans"]
assert "messaging.message.id" in span["data"]


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

0 comments on commit 68332d8

Please sign in to comment.