-
Notifications
You must be signed in to change notification settings - Fork 77
feat: add asyncio capture client #899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
marandaneto
wants to merge
13
commits into
main
Choose a base branch
from
feat/async-client-capture-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b45de74
feat: add asyncio capture client
marandaneto dcb1331
fix: preserve in-flight async capture during shutdown
marandaneto 3116cde
fix: sanitize async capture failure logs
marandaneto a96cc23
fix: reset async client logging level
marandaneto ed5ae7c
fix: keep async capture batches within size limit
marandaneto 0496548
fix: track immediate capture operation completion
marandaneto 25fafd1
fix: coordinate async worker flushes
marandaneto 6163ec8
docs: clarify async alternative to sync mode
marandaneto 0fbf6be
address async capture review feedback
marandaneto 61a3d12
fix: preserve context for buffered async events
marandaneto d59c99a
fix: clear event context for deferred lifecycle
marandaneto b7f70b5
fix: harden async capture delivery lifecycle
marandaneto 54cf24c
fix: reject cross-thread capture during shutdown
marandaneto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| pypi/posthog: minor | ||
| --- | ||
|
|
||
| Add an asyncio-native client for buffered and immediate event capture |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,285 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import contextvars | ||
| import inspect | ||
| import json | ||
| import logging | ||
| from collections.abc import Awaitable, Callable | ||
| from dataclasses import dataclass | ||
| from typing import Any, Optional | ||
|
|
||
| from ._async_request import async_batch_post, async_send_v1_batch | ||
| from .capture_compression import CaptureCompression | ||
| from .capture_mode import CaptureMode | ||
| from .consumer import BATCH_SIZE_LIMIT, MAX_MSG_SIZE | ||
| from .request import APIError, DatetimeSerializer, EVENTS_ENDPOINT | ||
|
|
||
| _STOP = object() | ||
| _PROCESSING_EVENT = contextvars.ContextVar( | ||
| "posthog_async_processing_event", default=False | ||
| ) | ||
|
|
||
|
|
||
| def _is_processing_event() -> bool: | ||
| return _PROCESSING_EVENT.get() | ||
|
|
||
|
|
||
| async def _run_outside_processing_event(awaitable): | ||
| token = _PROCESSING_EVENT.set(False) | ||
| try: | ||
| return await awaitable | ||
| finally: | ||
| _PROCESSING_EVENT.reset(token) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class _QueuedEvent: | ||
| event: dict[str, Any] | ||
| context: contextvars.Context | ||
|
|
||
|
|
||
| async def _invoke_callback(callback, *args): | ||
| if inspect.iscoroutinefunction(callback): | ||
| return await callback(*args) | ||
| result = await asyncio.to_thread(callback, *args) | ||
| if inspect.isawaitable(result): | ||
| return await result | ||
| return result | ||
|
|
||
|
|
||
| async def _serialized_event_size(event: dict[str, Any]) -> int: | ||
| serialized = await asyncio.to_thread(json.dumps, event, cls=DatetimeSerializer) | ||
| return len(serialized.encode()) | ||
|
|
||
|
|
||
| class _AsyncConsumer: | ||
| """Consume an asyncio queue and upload capture batches.""" | ||
|
|
||
| log = logging.getLogger("posthog") | ||
|
|
||
| def __init__( | ||
| self, | ||
| queue: asyncio.Queue[Any], | ||
| api_key: str, | ||
| *, | ||
| host: Optional[str], | ||
| on_error: Optional[Callable[[Exception, list[dict[str, Any]]], Any]], | ||
| process_event: Callable[[dict[str, Any]], Awaitable[Optional[dict[str, Any]]]], | ||
| flush_at: int, | ||
| flush_interval: float, | ||
| gzip: bool, | ||
| retries: int, | ||
| timeout: int, | ||
| historical_migration: bool, | ||
| capture_mode: CaptureMode, | ||
| capture_compression: CaptureCompression, | ||
| http_client: Optional[Any], | ||
| ) -> None: | ||
| self.queue = queue | ||
| self.api_key = api_key | ||
| self.host = host | ||
| self.on_error = on_error | ||
| self.process_event = process_event | ||
| self.flush_at = flush_at | ||
| self.flush_interval = flush_interval | ||
| self.gzip = gzip | ||
| self.retries = max(0, retries) | ||
| self.timeout = timeout | ||
| self.historical_migration = historical_migration | ||
| self.capture_mode = capture_mode | ||
| self.capture_compression = capture_compression | ||
| self.http_client = http_client | ||
| self._carryover: Optional[tuple[dict[str, Any], int]] = None | ||
| self._flush_event = asyncio.Event() | ||
|
|
||
| async def run(self) -> None: | ||
| self.log.debug("async consumer is running") | ||
| try: | ||
| while True: | ||
| batch, stop = await self.next() | ||
| if batch: | ||
| await self.upload(batch) | ||
| if stop: | ||
| return | ||
| except asyncio.CancelledError: | ||
| raise | ||
| except Exception as error: | ||
| self.log.error( | ||
| "async consumer stopped after an unexpected %s", type(error).__name__ | ||
| ) | ||
| finally: | ||
| self.log.debug("async consumer exited") | ||
|
|
||
| def request_flush(self) -> None: | ||
| self._flush_event.set() | ||
|
|
||
| async def _get_or_flush(self, timeout: float) -> tuple[Any, bool]: | ||
| get_task = asyncio.create_task(self.queue.get()) | ||
| flush_task = asyncio.create_task(self._flush_event.wait()) | ||
| done, pending = await asyncio.wait( | ||
| {get_task, flush_task}, | ||
| timeout=timeout, | ||
| return_when=asyncio.FIRST_COMPLETED, | ||
| ) | ||
| for task in pending: | ||
| task.cancel() | ||
| if pending: | ||
| await asyncio.gather(*pending, return_exceptions=True) | ||
|
|
||
| if get_task in done: | ||
| return get_task.result(), False | ||
| if flush_task in done: | ||
| self._flush_event.clear() | ||
| return None, True | ||
| return None, False | ||
|
|
||
| async def _process_queued_event( | ||
| self, event: dict[str, Any] | ||
| ) -> Optional[dict[str, Any]]: | ||
| token = _PROCESSING_EVENT.set(True) | ||
| try: | ||
| return await self.process_event(event) | ||
| finally: | ||
| _PROCESSING_EVENT.reset(token) | ||
|
|
||
| async def upload(self, batch: list[dict[str, Any]]) -> None: | ||
| try: | ||
| await self.request(batch) | ||
| except Exception as error: | ||
| self.log.error( | ||
| "async capture upload failed (%s, status=%s)", | ||
| type(error).__name__, | ||
| getattr(error, "status", None), | ||
| ) | ||
| if self.on_error: | ||
| try: | ||
| await _invoke_callback(self.on_error, error, batch) | ||
| except Exception as callback_error: | ||
| self.log.error( | ||
| "on_error handler failed (%s)", type(callback_error).__name__ | ||
| ) | ||
| finally: | ||
| for _ in batch: | ||
| self.queue.task_done() | ||
|
|
||
| async def next(self) -> tuple[list[dict[str, Any]], bool]: | ||
| items: list[dict[str, Any]] = [] | ||
| total_size = 0 | ||
| stop = False | ||
| started = asyncio.get_running_loop().time() | ||
|
|
||
| if self._carryover is not None: | ||
| carried_item, carried_size = self._carryover | ||
| self._carryover = None | ||
| items.append(carried_item) | ||
| total_size = carried_size | ||
|
|
||
| while len(items) < self.flush_at: | ||
| remaining = self.flush_interval - ( | ||
| asyncio.get_running_loop().time() - started | ||
| ) | ||
| if remaining <= 0: | ||
| break | ||
|
|
||
| queued, flush_requested = await self._get_or_flush(remaining) | ||
| if flush_requested or queued is None: | ||
| break | ||
|
|
||
| if queued is _STOP: | ||
| self.queue.task_done() | ||
| stop = True | ||
| break | ||
|
|
||
| try: | ||
| process_task = queued.context.run( | ||
| asyncio.create_task, self._process_queued_event(queued.event) | ||
| ) | ||
| item = await process_task | ||
| except Exception as error: | ||
| self.log.error( | ||
| "unable to process queued event, dropping (%s)", | ||
| type(error).__name__, | ||
| ) | ||
| self.queue.task_done() | ||
| continue | ||
|
|
||
| if item is None: | ||
| self.queue.task_done() | ||
| continue | ||
|
|
||
| try: | ||
| item_size = await _serialized_event_size(item) | ||
| except Exception: | ||
| self.log.error("unable to serialize queued event for sizing, dropping") | ||
| self.queue.task_done() | ||
| continue | ||
|
|
||
| if item_size > MAX_MSG_SIZE: | ||
| self.log.error( | ||
| "Event %s (%d bytes) exceeds the %dKiB limit, dropping.", | ||
| item.get("event"), | ||
| item_size, | ||
| MAX_MSG_SIZE // 1024, | ||
| ) | ||
| self.queue.task_done() | ||
| continue | ||
|
|
||
| if items and total_size + item_size > BATCH_SIZE_LIMIT: | ||
| self._carryover = (item, item_size) | ||
| self.log.debug("hit async batch size limit (size: %d)", total_size) | ||
| break | ||
|
|
||
| items.append(item) | ||
| total_size += item_size | ||
|
|
||
| return items, stop | ||
|
|
||
| async def request(self, batch: list[dict[str, Any]]) -> None: | ||
| if self.capture_mode == CaptureMode.V1: | ||
| await async_send_v1_batch( | ||
| self.api_key, | ||
| self.host, | ||
| batch, | ||
| compression=self.capture_compression, | ||
| timeout=self.timeout, | ||
| max_retries=self.retries, | ||
| historical_migration=self.historical_migration, | ||
| ) | ||
| return | ||
|
|
||
| last_error: Optional[Exception] = None | ||
| for attempt in range(self.retries + 1): | ||
| try: | ||
| await async_batch_post( | ||
| self.api_key, | ||
| self.host, | ||
| batch=batch, | ||
| path=EVENTS_ENDPOINT, | ||
| gzip=self.gzip, | ||
| timeout=self.timeout, | ||
| historical_migration=self.historical_migration, | ||
| client=self.http_client, | ||
| ) | ||
| return | ||
| except Exception as error: | ||
| last_error = error | ||
| if not self._is_retryable(error) or attempt >= self.retries: | ||
| raise | ||
| retry_after = getattr(error, "retry_after", None) | ||
| delay = max( | ||
| min(2**attempt, 30), | ||
| min(retry_after, 30) if retry_after and retry_after > 0 else 0, | ||
| ) | ||
| await asyncio.sleep(delay) | ||
|
|
||
| if last_error is not None: # pragma: no cover - loop always raises first | ||
| raise last_error | ||
|
|
||
| @staticmethod | ||
| def _is_retryable(error: Exception) -> bool: | ||
| if not isinstance(error, APIError): | ||
| return True | ||
| if not isinstance(error.status, int): | ||
| return False | ||
| return not (400 <= error.status < 500 and error.status not in (408, 429)) | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.