fix(celery): close celery.apply
spans even without after_task_publish, when using apply_async [backport 2.12]
#10891
+99
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Backport 0d28e08 from #10676 to 2.12.
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 if the expected signals don't trigger, which can lead to broken context propagation (and unexpected traces).
Example:
before_task_publish
to start the span thenafter_task_publish
to close the span. If theafter_task_publish
signal never gets called (which can happen if a Celery exception occurs while processing the app), then the span won't finish.task_prerun
andtask_postrun
.Solution
This PR patches
apply_async
so that there is a check to see if there is a span lingering around and closes it whenapply_task
is called.If an internal exception happens, the error will be marked on the
celery.apply
span.To track this, I added new logs in debug mode:
and
There's a related PR #10848 that works to improve how we extract information based on the protocols, that also affects when spans get closed or not.
Special Thanks:
APMS-13158
Checklist
Reviewer Checklist
APMS-13158