elastic / elastic/apm-agent-python

Celery task context sticks to the main transaction which is misleading

Open
#2,302 0 comments 0 reactions 0 assignees View on GitHub
agent-python community triage
Dominant language
Python
Stars
431
Forks
239
Avg merge
5d 10h
Merged PRs (30d)
7

Description

**Describe the bug**: ...

Celery task execution sticks to the main transaction of the django view. This is really misleading cause you may think that your view\endpoint works really slow, but in reality user already received response.

![Image](https://github.com/user-attachments/assets/a232fb26-c5ba-4a3b-82d0-1717f8952365)

**Environment (please complete the following information)**
- OS: Linux (heroku)
- Python version: 3.11
- Framework and version [e.g. Django 2.1]: Django 5
- APM Server version:
- Agent version: elastic-apm = "^6.23.0"
- Celery celery = {extras = ["sqs"], version = "^5.5.1"}

**Additional context**
`elasticapm.contrib.django.middleware.TracingMiddleware` middleware in the beginning (first).
Celery task is called trough .apply_async or .delay() doesn't matter.

Any ideas why it may happen? I assume it should be displayed just like it was called and that's it, without internal context sticked to the main View\endpoint transaction.

Contributor guide

Open the contributing guide

Research direction

Reproduce with Django 5, Python 3.11, elastic-apm 6.23.0, and Celery 5.5.1, placing elasticapm.contrib.django.middleware.TracingMiddleware first and invoking tasks through both .apply_async and .delay(). Compare the task and view transaction timing and context; done means the task is displayed independently after the view response rather than remaining attached to the main view transaction.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
backend, observability
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.