Skip to content

Commit a70685e

Browse files
feat(celery): Send queue name to Sentry
Send the queue name to Sentry for Celery tasks using the default exchange. The queue name is sent as span data with the key `messaging.destination.name`. Ref GH-2961
1 parent 9cf6377 commit a70685e

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

‎sentry_sdk/integrations/celery/__init__.py‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,20 +306,38 @@ def _inner(*args, **kwargs):
306306
return _inner # type: ignore
307307

308308

309+
def _set_messaging_destination_name(task, span):
310+
# type: (Any, Span) -> None
311+
"""Set "messaging.destination.name" tag for span"""
312+
with capture_internal_exceptions():
313+
delivery_info = task.request.delivery_info
314+
routing_key = delivery_info.get("routing_key")
315+
if delivery_info.get("exchange") == "" and routing_key is not None:
316+
# Empty exchange indicates the default exchange, meaning the tasks
317+
# are sent to the queue with the same name as the routing key.
318+
span.set_data("messaging.destination.name", routing_key)
319+
320+
309321
def _wrap_task_call(task, f):
310322
# type: (Any, F) -> F
311323

312324
# Need to wrap task call because the exception is caught before we get to
313325
# see it. Also celery's reported stacktrace is untrustworthy.
314326

315327
# functools.wraps is important here because celery-once looks at this
316-
# method's name.
328+
# method's name. @ensure_integration_enabled internally calls functools.wraps,
329+
# but if we ever remove the @ensure_integration_enabled decorator, we need
330+
# to add @functools.wraps(f) here.
317331
# https://github.com/getsentry/sentry-python/issues/421
318-
@wraps(f)
332+
@ensure_integration_enabled(CeleryIntegration, f)
319333
def _inner(*args, **kwargs):
320334
# type: (*Any, **Any) -> Any
321335
try:
322-
return f(*args, **kwargs)
336+
with sentry_sdk.start_span(
337+
op=OP.QUEUE_TASK_CELERY, description=task.name
338+
) as span:
339+
_set_messaging_destination_name(task, span)
340+
return f(*args, **kwargs)
323341
except Exception:
324342
exc_info = sys.exc_info()
325343
with capture_internal_exceptions():

‎tests/integrations/celery/test_celery.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,17 @@ def dummy_task(x, y):
209209
else:
210210
assert execution_event["contexts"]["trace"]["status"] == "ok"
211211

212-
assert execution_event["spans"] == []
212+
assert len(execution_event["spans"]) == 1
213+
assert (
214+
execution_event["spans"][0].items()
215+
>= {
216+
"trace_id": str(transaction.trace_id),
217+
"same_process_as_parent": True,
218+
"op": "queue.task.celery",
219+
"description": "dummy_task",
220+
"data": ApproxDict(),
221+
}.items()
222+
)
213223
assert submission_event["spans"] == [
214224
{
215225
"data": ApproxDict(),

0 commit comments

Comments
 (0)