|
| 1 | +""" |
| 2 | +Celery integration example for the PostHog Python SDK. |
| 3 | +
|
| 4 | +Demonstrates how to use ``PosthogCeleryIntegration`` with: |
| 5 | +- producer-side and worker-side instrumentation (publishing events and context propagation) |
| 6 | +- context propagation (distinct ID, session ID, tags) from producer to worker |
| 7 | +- task lifecycle events (published, started, success, failure, retry) |
| 8 | +- exception capture from failed tasks |
| 9 | +- ``task_filter`` customization hook |
| 10 | +
|
| 11 | +Setup: |
| 12 | + 1. Set ``POSTHOG_PROJECT_API_KEY`` and ``POSTHOG_HOST`` in your environment |
| 13 | + 2. Install dependencies: pip install posthog celery redis |
| 14 | + 3. Start Redis: redis-server |
| 15 | + 4. Start the worker: celery -A examples.celery_integration worker --loglevel=info |
| 16 | + 5. Run the producer: python -m examples.celery_integration |
| 17 | +""" |
| 18 | + |
| 19 | +import os |
| 20 | +import time |
| 21 | +from typing import Any, Optional |
| 22 | + |
| 23 | +from celery import Celery |
| 24 | +from celery.signals import worker_process_shutdown |
| 25 | + |
| 26 | +import posthog |
| 27 | +from posthog.integrations.celery import PosthogCeleryIntegration |
| 28 | + |
| 29 | + |
| 30 | +# --- Configuration --- |
| 31 | + |
| 32 | +POSTHOG_PROJECT_API_KEY = os.getenv("POSTHOG_PROJECT_API_KEY", "phc_...") |
| 33 | +POSTHOG_HOST = os.getenv("POSTHOG_HOST", "http://localhost:8000") |
| 34 | + |
| 35 | +app = Celery( |
| 36 | + "examples.celery_integration", |
| 37 | + broker="redis://localhost:6379/0", |
| 38 | +) |
| 39 | + |
| 40 | + |
| 41 | +# --- Integration wiring --- |
| 42 | + |
| 43 | + |
| 44 | +def configure_posthog() -> None: |
| 45 | + posthog.api_key = POSTHOG_PROJECT_API_KEY |
| 46 | + posthog.host = POSTHOG_HOST |
| 47 | + posthog.enable_local_evaluation = ( |
| 48 | + False # to not require personal_api_key for this example |
| 49 | + ) |
| 50 | + posthog.setup() |
| 51 | + |
| 52 | + |
| 53 | +def task_filter(task_name: Optional[str], task_properties: dict[str, Any]) -> bool: |
| 54 | + if task_name is not None and task_name.endswith(".health_check"): |
| 55 | + return False |
| 56 | + return True |
| 57 | + |
| 58 | + |
| 59 | +def create_integration() -> PosthogCeleryIntegration: |
| 60 | + return PosthogCeleryIntegration( |
| 61 | + capture_exceptions=True, |
| 62 | + capture_task_lifecycle_events=True, |
| 63 | + propagate_context=True, |
| 64 | + task_filter=task_filter, |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +configure_posthog() |
| 69 | +integration = create_integration() |
| 70 | +integration.instrument() |
| 71 | + |
| 72 | + |
| 73 | +# --- Worker process setup --- |
| 74 | +# On a single host the forked child inherits the PostHog client and |
| 75 | +# integration, so nothing extra is needed. If workers run on different |
| 76 | +# hosts, uncomment the signal and handler below to initialise a fresh |
| 77 | +# client and integration in each worker process. If using a custom flag |
| 78 | +# definition cache provider, reinitialize your client in each worker with |
| 79 | +# the custom provider, and reinstrument the integration with that new client |
| 80 | +# instance. |
| 81 | +# @worker_process_init.connect |
| 82 | +# def on_worker_process_init(**kwargs) -> None: |
| 83 | +# global integration |
| 84 | +# configure_posthog() |
| 85 | +# integration = create_integration() |
| 86 | +# integration.instrument() |
| 87 | +# return |
| 88 | + |
| 89 | + |
| 90 | +# Use this signal to shutdown the integration and PostHog client |
| 91 | +# in the worker processes. Calling shutdown() is important to flush |
| 92 | +# any pending events and is required even if the workers are running |
| 93 | +# on the same host as the producer. |
| 94 | +@worker_process_shutdown.connect |
| 95 | +def on_worker_process_shutdown(**kwargs) -> None: |
| 96 | + integration.shutdown() |
| 97 | + posthog.shutdown() |
| 98 | + |
| 99 | + |
| 100 | +# --- Example tasks --- |
| 101 | + |
| 102 | + |
| 103 | +@app.task |
| 104 | +def health_check() -> dict[str, str]: |
| 105 | + return {"status": "ok"} |
| 106 | + |
| 107 | + |
| 108 | +@app.task(max_retries=3) |
| 109 | +def process_order(order_id: str) -> dict: |
| 110 | + """A task that processes an order successfully.""" |
| 111 | + |
| 112 | + # simulate work |
| 113 | + time.sleep(0.1) |
| 114 | + |
| 115 | + # Custom event inside the task - context tags propagated from the |
| 116 | + # producer (e.g. "source", "release") should appear on this event |
| 117 | + # and this should be attributed to the correct distinct ID and session. |
| 118 | + posthog.capture( |
| 119 | + "celery example order processed", |
| 120 | + properties={"order_id": order_id, "amount": 99.99}, |
| 121 | + ) |
| 122 | + |
| 123 | + return {"order_id": order_id, "status": "completed"} |
| 124 | + |
| 125 | + |
| 126 | +@app.task(bind=True, max_retries=3) |
| 127 | +def send_notification(self, user_id: str, message: str) -> None: |
| 128 | + """A task that may fail and retry.""" |
| 129 | + if self.request.retries < 2: |
| 130 | + raise self.retry( |
| 131 | + exc=ConnectionError("notification service unavailable"), |
| 132 | + countdown=120, |
| 133 | + ) |
| 134 | + return None |
| 135 | + |
| 136 | + |
| 137 | +@app.task |
| 138 | +def failing_task() -> None: |
| 139 | + """A task that always fails.""" |
| 140 | + raise ValueError("something went wrong") |
| 141 | + |
| 142 | + |
| 143 | +# --- Producer code --- |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + print("PostHog Celery Integration Example") |
| 147 | + print("=" * 40) |
| 148 | + print() |
| 149 | + |
| 150 | + # Set up PostHog context before dispatching tasks. |
| 151 | + # The integration propagates this context to workers via task headers. |
| 152 | + with posthog.new_context(fresh=True): |
| 153 | + posthog.identify_context("user-123") |
| 154 | + posthog.set_context_session("session-user-123-abc") |
| 155 | + posthog.tag("source", "celery_integration_example_script") |
| 156 | + posthog.tag("release", "v1.2.3") |
| 157 | + |
| 158 | + print("Dispatching tasks...") |
| 159 | + |
| 160 | + # This task is intentionally filtered and should not emit task events. |
| 161 | + result = health_check.delay() |
| 162 | + print(f" health_check dispatched (filtered): {result.id}") |
| 163 | + |
| 164 | + # This task will produce events: |
| 165 | + # celery task published (sender side) |
| 166 | + # celery task started (worker side) |
| 167 | + # order processed (custom event, should carry propagated context tags) |
| 168 | + # celery task success (worker side, includes duration) |
| 169 | + result = process_order.delay("order-456") |
| 170 | + print(f" process_order dispatched: {result.id}") |
| 171 | + |
| 172 | + # This task will produce events: |
| 173 | + # celery task published |
| 174 | + # celery task started |
| 175 | + # celery task retry (with reason) |
| 176 | + # celery task started (retry attempt) |
| 177 | + # celery task success |
| 178 | + result = send_notification.delay("user-123", "Hello!") |
| 179 | + print(f" send_notification dispatched: {result.id}") |
| 180 | + |
| 181 | + # This task will produce events: |
| 182 | + # celery task published |
| 183 | + # celery task started |
| 184 | + # celery task failure (with error_type and error_message) |
| 185 | + result = failing_task.delay() |
| 186 | + print(f" failing_task dispatched: {result.id}") |
| 187 | + |
| 188 | + print() |
| 189 | + print("Tasks dispatched. Check your Celery worker logs and PostHog for events.") |
| 190 | + print() |
| 191 | + |
| 192 | + # Shut down the integration and client in producer process |
| 193 | + integration.shutdown() |
| 194 | + posthog.shutdown() |
0 commit comments