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

fix(celery): close celery.apply spans even without after_task_publish, when using apply_async #10676

Open
wants to merge 23 commits into
base: main
Choose a base branch
from

Conversation

wantsui
Copy link
Collaborator

@wantsui wantsui commented Sep 16, 2024

Credits go to @tabgok for going through this with me in great detail!

The instrumentation for the Celery integration relies on various Celery signals in order to start and end the span when calling on apply_async.

The integration can fail in two known places, which can lead to broken context propagation (and unexpected traces).

Issue 1: Closing signals don't get called.

  • dd-trace-py expects the signal before_task_publish to start the span then after_task_publish to close the span. If the after_task_publish signal never gets called (which can happen if a Celery exception occurs while processing the app), then the span won't finish.
  • The same thing above can also happen to task_prerun and task_postrun.

Issue 2: Even if after_task_publish is called, the integration previously only closed the span if the task or task id was present. However, it is possible that there is a lingering span kept open but doesn't return a task_id and instead just return nothing. This is bad because that means the remnants of that celery.apply span is never closed either.

To address both, this PR patches apply_async so that there is a check to see if there is a span lingering around and closes it when apply_task is called.

Meanwhile, the fix to celery/utils.py is to address the fact that some times, the returning response returns an empty body, in which case, we should not try to run body.get('id') and just return None.

Open Item

  1. At the moment, the span is closed but any underlying errors won't be surfaced on the span. It is possible to do this in an except clause, but I'm not sure if I should.

To track this, I added new logs in debug mode:

The after_task_publish signal was not called, so manually closing span

and

The task_postrun signal was not called, so manually closing span

Checklist

  • PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

@wantsui wantsui requested a review from tabgok September 16, 2024 21:13
@datadog-dd-trace-py-rkomorn
Copy link

datadog-dd-trace-py-rkomorn bot commented Sep 16, 2024

Datadog Report

Branch report: close-apply-async-celery-spans
Commit report: 5b96ae7
Test service: dd-trace-py

✅ 0 Failed, 673 Passed, 523 Skipped, 22m 41.31s Total duration (14m 7.95s time saved)

Copy link
Contributor

github-actions bot commented Sep 16, 2024

CODEOWNERS have been resolved as:

releasenotes/notes/fix-celery-apply-async-span-close-b7a8db188459f5b5.yaml  @DataDog/apm-python
ddtrace/contrib/internal/celery/app.py                                  @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/internal/celery/signals.py                              @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/internal/celery/utils.py                                @DataDog/apm-core-python @DataDog/apm-idm-python
tests/contrib/celery/test_integration.py                                @DataDog/apm-core-python @DataDog/apm-idm-python

@pr-commenter
Copy link

pr-commenter bot commented Sep 16, 2024

Benchmarks

Benchmark execution time: 2024-09-27 14:33:35

Comparing candidate commit 5b96ae7 in PR branch close-apply-async-celery-spans with baseline commit 0bd5ea0 in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 267 metrics, 51 unstable metrics.

@wantsui wantsui changed the title fix(celery) Close celery.apply spans when using apply_async, even when after_task_publish isn't called fix(celery): Close celery.apply spans even without after_task_publish, when using apply_async Sep 17, 2024
@wantsui wantsui changed the title fix(celery): Close celery.apply spans even without after_task_publish, when using apply_async fix(celery): close celery.apply spans even without after_task_publish, when using apply_async Sep 17, 2024
@wantsui wantsui marked this pull request as ready for review September 20, 2024 15:50
@wantsui wantsui requested review from a team as code owners September 20, 2024 15:50
@@ -131,6 +131,6 @@ def retrieve_task_id(context):
if headers:
# Protocol Version 2 (default from Celery 4.0)
return headers.get("id")
else:
elif body:
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: usually good to have one PR per change you're making. This is a one-liner so arguable, but it makes it easier to rollback/make sure you're not missing something in the future

ddtrace/contrib/internal/celery/app.py Show resolved Hide resolved
---
fixes:
- |
tracing(celery): Fixes an issue where `celery.apply` spans didn't close if the after_task_publish or task_postrun signals didn't get called.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: would add a little bit about why they weren't called (exception generated in handling)

wantsui and others added 2 commits September 23, 2024 11:06
… id when headers has data (but no task id), which wrongly kept spans open.
…the task id when headers has data (but no task id), which wrongly kept spans open."

This reverts commit b4556d2.
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.

3 participants