Skip to content

Commit 53770bc

Browse files
eli-r-phclaude
andauthored
fix(celery): keep a task's own celery tags over propagated ones (#1209)
Tags propagated from the publishing task's context overrode the running task's celery_* values, so events inside a child task, a retry or a chord callback reported the publisher's task name, ID and retry count. The task's own values now win. Other propagated tags are unchanged. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 133d7d1 commit 53770bc

3 files changed

Lines changed: 57 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
pypi/posthog: patch
3+
---
4+
5+
Events captured inside a Celery task now carry that task's own `celery_*` properties. Before, a task published from inside another task, such as a child task, a retry or a chord callback, reported the publishing task's `celery_task_name`, `celery_task_id` and `celery_retry_count`.

‎posthog/integrations/celery.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,9 @@ def _on_task_prerun(self, *args, **kwargs):
313313
self._apply_propagated_identity(request)
314314
self._enrich_task_span(task_id)
315315

316-
merged_tags = {**task_properties, **context_tags}
316+
# The task's own celery_* values win: tags propagated from a task
317+
# that published this one carry that task's values.
318+
merged_tags = {**context_tags, **task_properties}
317319
for key, value in merged_tags.items():
318320
contexts.tag(key, value)
319321

‎posthog/test/integrations/test_celery_integration.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from types import SimpleNamespace
44
from unittest.mock import Mock, patch
55

6+
from parameterized import parameterized
7+
68
from posthog import contexts
79
from posthog.integrations.celery import (
810
CONTEXT_DISTINCT_ID_HEADER,
@@ -260,6 +262,53 @@ def test_before_task_publish_nested_headers_round_trips_to_worker(self):
260262
# close out the task lifecycle so the test doesn't leave context hanging around
261263
integration._on_task_success(sender=task)
262264

265+
@parameterized.expand(
266+
[
267+
# name, publishing task (name, id, retries), running task (name, id, retries)
268+
("child_task", ("app.parent", "parent-1", 0), ("app.child", "child-1", 0)),
269+
("retry", ("app.flaky", "flaky-1", 0), ("app.flaky", "flaky-1", 1)),
270+
(
271+
"chord_callback",
272+
("app.header", "header-1", 0),
273+
("app.callback", "callback-1", 0),
274+
),
275+
]
276+
)
277+
def test_task_published_from_a_task_keeps_its_own_celery_tags(
278+
self, _name, publisher, worker
279+
):
280+
integration = PosthogCeleryIntegration(client=Mock())
281+
282+
def run_task(name, task_id, retries, headers):
283+
request = SimpleNamespace(
284+
headers=headers, delivery_info={}, hostname="worker-1", retries=retries
285+
)
286+
task = SimpleNamespace(name=name, request=request)
287+
integration._on_task_prerun(sender=task, task_id=task_id)
288+
return task
289+
290+
publisher_task = run_task(
291+
*publisher,
292+
{
293+
CONTEXT_DISTINCT_ID_HEADER: "user-1",
294+
CONTEXT_TAGS_HEADER: '{"request_id": "abc"}',
295+
},
296+
)
297+
headers = {}
298+
integration._on_before_task_publish(sender=worker[0], headers=headers)
299+
integration._on_task_success(sender=publisher_task)
300+
301+
worker_task = run_task(*worker, headers["headers"])
302+
tags = contexts.get_tags()
303+
integration._on_task_success(sender=worker_task)
304+
305+
worker_name, worker_id, worker_retries = worker
306+
self.assertEqual(tags["celery_task_name"], worker_name)
307+
self.assertEqual(tags["celery_task_id"], worker_id)
308+
self.assertEqual(tags["celery_retry_count"], worker_retries)
309+
# Other context still propagates from the publishing task.
310+
self.assertEqual(tags["request_id"], "abc")
311+
263312
def test_before_task_publish_respects_propagate_context_flag(self):
264313
integration = PosthogCeleryIntegration(propagate_context=False)
265314
headers = {}

0 commit comments

Comments
 (0)