Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracing of Celery tasks does not work with Signatures #1416

Open
alexpersin opened this issue Apr 29, 2022 · 2 comments
Open

Tracing of Celery tasks does not work with Signatures #1416

alexpersin opened this issue Apr 29, 2022 · 2 comments
Labels
Enhancement New feature or request Help wanted Extra attention is needed Integration: Celery Triaged Has been looked at recently during old issue triage

Comments

@alexpersin
Copy link

How do you use Sentry?

Sentry Saas (sentry.io)

Version

1.5.10

Steps to Reproduce

The python sentry_sdk celery integration uses _wrap_apply_async to wrap the celery.Task.apply_async method. However it does not wrap celery Signatures as these have their own apply_async method defined in the celery package in celery/canvas.py. This means that tasks defined using signatures are not traced.

To reproduce:

import sentry_sdk
import os
from sentry_sdk.integrations.celery import CeleryIntegration

sentry_sdk.init(os.getenv('SENTRY_DSN'), integrations=[CeleryIntegration()],  traces_sample_rate=1.0, debug=True)

group_result = celery.group([
    celery.signature('foo', kwargs=kwargs),
    celery.signature('bar', kwargs=kwargs)
]).apply_async()

This does not produce traces. Alternatively,

import sentry_sdk
import os
from sentry_sdk.integrations.celery import CeleryIntegration
from celery.app.task import Task 

sentry_sdk.init(os.getenv('SENTRY_DSN'), integrations=[CeleryIntegration()],  traces_sample_rate=1.0, debug=True)

# Not wrapped by sentry sdk
print(celery.group.apply_async.__code__)

# Wrapped by sentry sdk
print(Task.apply_async.__code__)

Running the following fixes the issue:

import celery
from sentry_sdk.integrations.celery import _wrap_apply_async

celery.group.apply_async = _wrap_apply_async(celery.group.apply_async)
celery.chunks.apply_async = _wrap_apply_async(celery.chunks.apply_async)
celery.chain.apply_async = _wrap_apply_async(celery.chain.apply_async)
celery.chord.apply_async = _wrap_apply_async(celery.chord.apply_async)
Signature.apply_async = _wrap_apply_async(Signature.apply_async)

Expected Result

Transactions visible in the UI.

Actual Result

No transaction visible. Stepping through the code with a debugger, the function created with _wrap_apply_async is not called as the Signature.apply_async method is not wrapped.

@sl0thentr0py
Copy link
Member

@alexpersin thx for the report, since you already identified the problem, PR's are always welcome!

@alexpersin
Copy link
Author

My initial fix was slightly wrong, it should be

from celery.canvas import Signature
import celery
from sentry_sdk.integrations.celery import _wrap_apply_async

celery.group.apply_async = _wrap_apply_async(celery.group.apply_async)
celery.chunks.apply_async = _wrap_apply_async(celery.chunks.apply_async)
celery.canvas._chain.apply_async = _wrap_apply_async(celery.canvas._chain.apply_async)
celery.canvas._chord.apply_async = _wrap_apply_async(celery.canvas._chord.apply_async)
Signature.apply_async = _wrap_apply_async(Signature.apply_async)

Confirmed that this works for groups within chains within groups. Chained tasks show up as child transactions, and it works for retried tasks too.

@sl0thentr0py sl0thentr0py added the Help wanted Extra attention is needed label May 10, 2022
@sentrivana sentrivana added the Triaged Has been looked at recently during old issue triage label Nov 6, 2023
@antonpirker antonpirker added this to the Celery Update milestone Jun 7, 2024
@antonpirker antonpirker removed this from the Celery Update milestone Jun 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request Help wanted Extra attention is needed Integration: Celery Triaged Has been looked at recently during old issue triage
Projects
None yet
Development

No branches or pull requests

5 participants