Skip to content

Commit c2b0972

Browse files
authored
fix(celery): respect task filters for exceptions (#807)
1 parent c95c9f9 commit c2b0972

3 files changed

Lines changed: 63 additions & 4 deletions

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+
Respect Celery task filters for exception capture

‎posthog/integrations/celery.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,18 @@ def _handle_task_end(
365365
if exception:
366366
task_properties["error_type"] = type(exception).__name__
367367
task_properties["error_message"] = str(exception)
368-
if self.capture_exceptions:
369-
self._capture_exception(exception)
370368

371369
task_name = task_properties.get("celery_task_name")
372-
if self.capture_task_lifecycle_events and self._should_track(
373-
task_name, task_properties
370+
should_track = False
371+
if self.capture_task_lifecycle_events or (
372+
exception and self.capture_exceptions
374373
):
374+
should_track = self._should_track(task_name, task_properties)
375+
376+
if exception and self.capture_exceptions and should_track:
377+
self._capture_exception(exception)
378+
379+
if self.capture_task_lifecycle_events and should_track:
375380
self._capture_event(f"celery task {state}", properties=task_properties)
376381
except Exception:
377382
logger.exception("Failed to process Celery %s state", state)

‎posthog/test/integrations/test_celery_integration.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,55 @@ def test_task_filter_applies_to_worker_lifecycle_events(self):
509509

510510
mock_client.capture.assert_not_called()
511511

512+
def test_task_filter_applies_to_failure_exception_and_lifecycle_event(self):
513+
mock_client = Mock()
514+
task_filter = Mock(return_value=False)
515+
integration = PosthogCeleryIntegration(
516+
client=mock_client,
517+
task_filter=task_filter,
518+
)
519+
request = SimpleNamespace(headers={}, delivery_info={})
520+
task = SimpleNamespace(name="app.tasks.filtered_failure", request=request)
521+
exception = ValueError("task failed")
522+
context_before = contexts._get_current_context()
523+
524+
integration._on_task_prerun(sender=task, task_id="task-filtered-failure")
525+
task_filter.reset_mock()
526+
integration._on_task_failure(
527+
sender=task,
528+
task_id="task-filtered-failure",
529+
exception=exception,
530+
)
531+
532+
task_filter.assert_called_once()
533+
mock_client.capture.assert_not_called()
534+
mock_client.capture_exception.assert_not_called()
535+
self.assertIs(contexts._get_current_context(), context_before)
536+
537+
def test_task_filter_applies_to_retry_lifecycle_event(self):
538+
mock_client = Mock()
539+
task_filter = Mock(return_value=False)
540+
integration = PosthogCeleryIntegration(
541+
client=mock_client,
542+
task_filter=task_filter,
543+
)
544+
request = SimpleNamespace(headers={}, delivery_info={})
545+
task = SimpleNamespace(name="app.tasks.filtered_retry", request=request)
546+
context_before = contexts._get_current_context()
547+
548+
integration._on_task_prerun(sender=task, task_id="task-filtered-retry")
549+
task_filter.reset_mock()
550+
integration._on_task_retry(
551+
sender=task,
552+
task_id="task-filtered-retry",
553+
reason=ConnectionError("broker down"),
554+
)
555+
556+
task_filter.assert_called_once()
557+
mock_client.capture.assert_not_called()
558+
mock_client.capture_exception.assert_not_called()
559+
self.assertIs(contexts._get_current_context(), context_before)
560+
512561
def test_task_failure_captures_exception_when_lifecycle_events_disabled(self):
513562
mock_client = Mock()
514563
integration = PosthogCeleryIntegration(

0 commit comments

Comments
 (0)