Skip to content

feat(capture): add capture_mode config scaffolding (capture v1, 1/6)#701

Draft
eli-r-ph wants to merge 1 commit into
mainfrom
capture-v1/01-config
Draft

feat(capture): add capture_mode config scaffolding (capture v1, 1/6)#701
eli-r-ph wants to merge 1 commit into
mainfrom
capture-v1/01-config

Conversation

@eli-r-ph

Copy link
Copy Markdown

💡 Motivation and Context

First PR in a stacked series adding Capture V1 (POST /i/v1/analytics/events) support to the SDK, at parity with posthog-go's CaptureMode and posthog-rs's capture-v1.

This PR is pure, inert scaffolding: it adds the capture-mode selector and threads it everywhere it will be needed, but changes no runtime behavior — V0 (legacy /batch/) still runs on every path. Later PRs add the v1 serializer, transport, wiring, harness suite, and docs.

  • New CaptureMode enum: V0 (legacy /batch/, the default) and V1 (/i/v1/analytics/events).
  • resolve_capture_mode() with precedence explicit kwarg > POSTHOG_CAPTURE_MODE env > V0. Env accepts v0/legacy and v1/analytics_v1 (mirrors posthog-go's vocabulary); an unrecognized env value warns and defaults to V0 so a typo never silently flips the wire protocol. An invalid explicit kwarg raises ValueError (explicit misconfig should be loud).
  • Plumbed through Client.__init__ (resolved + stored as self.capture_mode), both Consumer(...) constructions (initial + fork-reinit), and the module-level default client. Consumer stores it but does not yet act on it.

💚 How did you test it?

  • New posthog/test/test_capture_mode.py (25 cases): resolution precedence, env aliases/case/whitespace, blank-env default, unrecognized-env warn+default, invalid-kwarg raises, and plumbing to Client.capture_mode + per-Consumer propagation (incl. multi-thread).
  • Regression: ran test_consumer, test_client, test_module, test_client_fork, test_dedicated_ai_endpoint + the new file — 213 passed.
  • ruff format/ruff check clean; mypy clean on new modules; regenerated references/public_api_snapshot.txt.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change (additive, default V0 keeps upgrades transparent).

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Authored with Cursor (Claude Opus 4.8) following a pre-agreed, hardened implementation plan. Design decisions: enum members are Pythonic V0/V1 (not posthog-go's Legacy/AnalyticsV1) but the env var accepts both spellings for cross-SDK consistency; the env typo path defaults defensively while an explicit bad kwarg fails fast. Mode is fully inert here by design to keep this PR a no-op for existing users and easy to review. Changeset (sampo add) and docs are intentionally deferred to the final PR so the feature releases as one entry.

Introduce a CaptureMode enum (V0 legacy /batch/, V1 /i/v1/analytics/events)
and resolve_capture_mode() with precedence kwarg > POSTHOG_CAPTURE_MODE env >
V0. Plumb capture_mode through Client and Consumer (including fork-reinit and
the module-level default client). The mode is resolved and stored but inert in
this change: V0 still runs everywhere, so behavior is unchanged.

First of a stacked series adding Capture V1 support.
@greptile-apps

greptile-apps Bot commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "feat(capture): add capture_mode config s..." | Re-trigger Greptile

Comment on lines +24 to +40
@parameterized.expand(
[
("enum_v0", CaptureMode.V0, CaptureMode.V0),
("enum_v1", CaptureMode.V1, CaptureMode.V1),
("str_v0", "v0", CaptureMode.V0),
("str_v1", "v1", CaptureMode.V1),
("str_legacy_alias", "legacy", CaptureMode.V0),
("str_analytics_v1_alias", "analytics_v1", CaptureMode.V1),
("str_upper_and_padded", " V1 ", CaptureMode.V1),
]
)
def test_explicit_kwarg_takes_precedence_and_coerces(
self, _name, kwarg, expected
) -> None:
# Env is set to the opposite mode to prove the kwarg wins.
with mock.patch.dict(os.environ, {CAPTURE_MODE_ENV_VAR: "v1"}):
self.assertIs(resolve_capture_mode(kwarg), expected)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 In test_explicit_kwarg_takes_precedence_and_coerces, the env is set to "v1" for every sub-case. For the four cases where the kwarg also resolves to V1 (enum_v1, str_v1, str_analytics_v1_alias, str_upper_and_padded), the result is V1 regardless of whether the kwarg or the env won — so these cases never actually exercise the precedence claim. Setting the env to "v0" for those cases would give the test its full demonstrative value.

Suggested change
@parameterized.expand(
[
("enum_v0", CaptureMode.V0, CaptureMode.V0),
("enum_v1", CaptureMode.V1, CaptureMode.V1),
("str_v0", "v0", CaptureMode.V0),
("str_v1", "v1", CaptureMode.V1),
("str_legacy_alias", "legacy", CaptureMode.V0),
("str_analytics_v1_alias", "analytics_v1", CaptureMode.V1),
("str_upper_and_padded", " V1 ", CaptureMode.V1),
]
)
def test_explicit_kwarg_takes_precedence_and_coerces(
self, _name, kwarg, expected
) -> None:
# Env is set to the opposite mode to prove the kwarg wins.
with mock.patch.dict(os.environ, {CAPTURE_MODE_ENV_VAR: "v1"}):
self.assertIs(resolve_capture_mode(kwarg), expected)
@parameterized.expand(
[
# env=v1, kwarg resolves to V0: kwarg must win
("enum_v0", CaptureMode.V0, CaptureMode.V0, "v1"),
# env=v0, kwarg resolves to V1: kwarg must win
("enum_v1", CaptureMode.V1, CaptureMode.V1, "v0"),
("str_v0", "v0", CaptureMode.V0, "v1"),
("str_v1", "v1", CaptureMode.V1, "v0"),
("str_legacy_alias", "legacy", CaptureMode.V0, "v1"),
("str_analytics_v1_alias", "analytics_v1", CaptureMode.V1, "v0"),
("str_upper_and_padded", " V1 ", CaptureMode.V1, "v0"),
]
)
def test_explicit_kwarg_takes_precedence_and_coerces(
self, _name, kwarg, expected, env_value
) -> None:
# Env is set to the opposite mode to prove the kwarg wins.
with mock.patch.dict(os.environ, {CAPTURE_MODE_ENV_VAR: env_value}):
self.assertIs(resolve_capture_mode(kwarg), expected)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@github-actions

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-06-27 19:33:51 UTC
Duration: 530130ms

✅ All Tests Passed!

45/45 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 517ms
Format Validation.Event Has Uuid 10007ms
Format Validation.Event Has Lib Properties 10007ms
Format Validation.Distinct Id Is String 10007ms
Format Validation.Token Is Present 10007ms
Format Validation.Custom Properties Preserved 10007ms
Format Validation.Event Has Timestamp 10007ms
Retry Behavior.Retries On 503 18018ms
Retry Behavior.Does Not Retry On 400 12005ms
Retry Behavior.Does Not Retry On 401 10006ms
Retry Behavior.Respects Retry After Header 16014ms
Retry Behavior.Implements Backoff 30017ms
Retry Behavior.Retries On 500 13011ms
Retry Behavior.Retries On 502 16010ms
Retry Behavior.Retries On 504 16011ms
Retry Behavior.Max Retries Respected 30018ms
Deduplication.Generates Unique Uuids 7002ms
Deduplication.Preserves Uuid On Retry 16016ms
Deduplication.Preserves Uuid And Timestamp On Retry 23019ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 16006ms
Deduplication.No Duplicate Events In Batch 10002ms
Deduplication.Different Events Have Different Uuids 10007ms
Compression.Sends Gzip When Enabled 10007ms
Batch Format.Uses Proper Batch Structure 10007ms
Batch Format.Flush With No Events Sends Nothing 5005ms
Batch Format.Multiple Events Batched Together 10005ms
Error Handling.Does Not Retry On 403 12008ms
Error Handling.Does Not Retry On 413 10007ms
Error Handling.Retries On 408 14013ms

Feature_Flags Tests

16/16 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 9502ms
Request Payload.Flags Request Uses V2 Query Param 10006ms
Request Payload.Flags Request Hits Flags Path Not Decide 10007ms
Request Payload.Flags Request Omits Authorization Header 10007ms
Request Payload.Token In Flags Body Matches Init 10007ms
Request Payload.Groups Round Trip 10007ms
Request Payload.Groups Default To Empty Object 10007ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It 10007ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 10006ms
Request Payload.Disable Geoip Omitted Defaults To False 10007ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 10014ms
Request Lifecycle.No Flags Request On Init Alone 5003ms
Request Lifecycle.No Flags Request On Normal Capture 10508ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 9510ms
Request Lifecycle.Mock Response Value Is Returned To Caller 10003ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 10509ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant