Repository navigation
feat!: send capture through v1 by default; capture_ai to /i/v1/ai/events #1017
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eli-r-ph
wants to merge
1
commit into
v1
Choose a base branch
from
v1-capture-default
base: v1
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.
+534
−419
Open
Changes from all commits
Commits
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
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
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
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,58 @@ | ||
| """Intercept capture uploads at the batch submitter for client-level tests. | ||
|
|
||
| Patching the submitter (not the HTTP layer) lets tests assert on the event | ||
| dicts the SDK built, before the wire encoding in ``capture_v1``. Wire shape is | ||
| covered by ``test_capture_v1``. | ||
| """ | ||
|
|
||
| import json | ||
| from unittest import mock | ||
|
|
||
| from requests import Response | ||
|
|
||
| _SUBMITTER = "_send_v1_batch" | ||
|
|
||
|
|
||
| def offline_v1_post(url: str, data=None, **kwargs) -> Response: | ||
| """Stand-in for ``requests.Session.post`` that accepts every v1 event. | ||
|
|
||
| For subprocess tests with no server. Prints the uncompressed request body, | ||
| because the SDK never logs payloads, and answers ``ok`` for each event. | ||
| """ | ||
| print(f"capture request body: {data}", flush=True) # noqa: T201 | ||
| events = json.loads(data)["batch"] | ||
| response = Response() | ||
| response.status_code = 200 | ||
| response._content = json.dumps( | ||
| {"results": {event["uuid"]: {"result": "ok"} for event in events}} | ||
| ).encode() | ||
| return response | ||
|
|
||
|
|
||
| def patch_capture_send(site: str = "client", **kwargs) -> "mock._patch": | ||
| """Patch the submitter where ``posthog.<site>`` imported it. | ||
|
|
||
| ``site="client"`` sees ``sync_mode`` uploads; ``site="consumer"`` sees | ||
| background consumer uploads. | ||
| """ | ||
| return mock.patch(f"posthog.{site}.{_SUBMITTER}", **kwargs) | ||
|
|
||
|
|
||
| def patch_async_capture_send(**kwargs) -> "mock._patch": | ||
| """Patch the submitter the ``AsyncPosthog`` consumer awaits.""" | ||
| return mock.patch("posthog._async_consumer.async_send_v1_batch", **kwargs) | ||
|
|
||
|
|
||
| def sent_batch(send_mock: mock.Mock, call_index: int = -1) -> list[dict]: | ||
| """Return the event batch from one recorded upload (default: the last).""" | ||
| call = send_mock.call_args_list[call_index] | ||
| return call.args[2] if len(call.args) > 2 else call.kwargs["batch"] | ||
|
|
||
|
|
||
| def sent_events(send_mock: mock.Mock) -> list[dict]: | ||
| """Return every event uploaded through ``send_mock``, in send order.""" | ||
| return [ | ||
| event | ||
| for index in range(len(send_mock.call_args_list)) | ||
| for event in sent_batch(send_mock, index) | ||
| ] |
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.