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

feat(celery): Send queue name to Sentry #2984

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

szokeasaurusrex
Copy link
Member

@szokeasaurusrex szokeasaurusrex commented Apr 16, 2024

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.

Closes GH-2961

@szokeasaurusrex szokeasaurusrex self-assigned this Apr 16, 2024
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/celery-queue-consumer-default-exchange branch 2 times, most recently from b02c1eb to 759bd53 Compare April 17, 2024 08:52
szokeasaurusrex added a commit that referenced this pull request Apr 17, 2024
Struggling to get this test to work due to complex test setup
@szokeasaurusrex
Copy link
Member Author

Due to the current way we test Celery, it appears there is no straightforward way to test this change in our test suite. The problem is that our tests run with Celery's eager mode, which means the tasks are run synchronously in the same process, without any queueing. Since there is no queue, we never set messaging.destination.name.

We should improve our Celery tests, and add a test for this change, in a separate PR.

@szokeasaurusrex szokeasaurusrex marked this pull request as ready for review April 17, 2024 12:41
def _inner(*args, **kwargs):
# type: (*Any, **Any) -> Any
try:
return f(*args, **kwargs)
with sentry_sdk.start_span(
op=OP.QUEUE_TASK_CELERY, description=task.name
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a better op we could use here? queue.task.celery seemed like the most fitting op; however, since the parent transaction also has op set to queue.task.celery, this leads to the span having the same op as its parent, which could be somewhat confusing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, could be confusing. Lets see what @cleptric spec on develop docs will look like and then see if we should add a new op for this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be queue.process if I get the context here right.
We'll put celery on messaging.system.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this would be a new span op @cleptric? I do not see queue.process listed anywhere on our list of span ops.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sentrivana
Copy link
Contributor

sentrivana commented Apr 17, 2024

Due to the current way we test Celery, it appears there is no straightforward way to test this change in our test suite. The problem is that our tests run with Celery's eager mode, which means the tasks are run synchronously in the same process, without any queueing. Since there is no queue, we never set messaging.destination.name.

We should improve our Celery tests, and add a test for this change, in a separate PR.

Just a sidenote, but this is also basically the reason why we can't test #2377 properly at the moment. So as soon as we can run the tests in non-eager mode we also unblock at least one other feature.

@szokeasaurusrex szokeasaurusrex changed the title feat(celery): Send queue name to sentry_sdk feat(celery): Send queue name to Sentry Apr 17, 2024
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/celery-queue-consumer-default-exchange branch from 759bd53 to 0e3c823 Compare April 17, 2024 12:48
Copy link
Member

@antonpirker antonpirker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one assert in the tests I do not get.

And because of the "always-eager" there is no way we can tests for messaging.destination.name right? Do you have any other PR plan on how we could run tests with a queue?

tests/integrations/celery/test_celery.py Show resolved Hide resolved
sentry_sdk/integrations/celery/__init__.py Show resolved Hide resolved
def _inner(*args, **kwargs):
# type: (*Any, **Any) -> Any
try:
return f(*args, **kwargs)
with sentry_sdk.start_span(
op=OP.QUEUE_TASK_CELERY, description=task.name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, could be confusing. Lets see what @cleptric spec on develop docs will look like and then see if we should add a new op for this.

@szokeasaurusrex
Copy link
Member Author

And because of the "always-eager" there is no way we can tests for messaging.destination.name right? Do you have any other PR plan on how we could run tests with a queue?

I could not find an obvious way to test messaging.destination.name, since with the "always-eager" mode, the queueing information does not get set properly. We would need to find how to have a real queue in the tests. Or, perhaps, we could work around this with some mocking logic

@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/celery-queue-consumer-default-exchange branch 2 times, most recently from 8028917 to 5a99aad Compare April 24, 2024 13:55
Base automatically changed from sentry-sdk-2.0 to master April 25, 2024 09:13
szokeasaurusrex added a commit that referenced this pull request Apr 25, 2024
Struggling to get this test to work due to complex test setup
@szokeasaurusrex
Copy link
Member Author

@antonpirker Turns out that we can test for messaging.destination.name by mocking out the task request. I just added some commits to test the changes.

@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/celery-queue-consumer-default-exchange branch from 2fea931 to 3bf4c12 Compare April 26, 2024 10:18
@antonpirker
Copy link
Member

Failing tests should be fixed by this pr: #3030

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
The previous test only checked that the queue was set on the span when the queue had the default name ("celery"). This test adds a check to ensure the queue also gets set on the span when it has a non-default value.
This test checks that `messaging.destination.name` is only set for the default exchange. Other exchanges should not set this value, since the routing key may be different from the queue name.
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/celery-queue-consumer-default-exchange branch from f1671f5 to 67e2531 Compare April 30, 2024 15:16
Copy link

@edwardgou-sentry edwardgou-sentry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worked for me in testing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add queue or topic to Celery transactions/spans
5 participants