Skip to content

chore(flags): expose flag_definition_cache_provider#462

Merged
matheus-vb merged 2 commits intomasterfrom
matheus-vb/module-level-flag-definition-cache-provider
Mar 12, 2026
Merged

chore(flags): expose flag_definition_cache_provider#462
matheus-vb merged 2 commits intomasterfrom
matheus-vb/module-level-flag-definition-cache-provider

Conversation

@matheus-vb
Copy link
Member

@matheus-vb matheus-vb commented Mar 11, 2026

Problem

The PostHog monolith wants to use a HyperCacheFlagProvider (see posthog#50737) to read flag definitions directly from Redis instead of polling the API every 90 seconds per worker. However, the SDK's module-level API has no way to accept an external cache provider — the flag_definition_cache_provider parameter only exists on the Client constructor, not as a module-level variable.

Solution

Module-level flag_definition_cache_provider

Expose flag_definition_cache_provider as a module-level variable in posthog/__init__.py (matching the pattern used by all other SDK settings like personal_api_key, poll_interval, etc.) and pass it through setup() to the Client constructor. This allows the monolith to set posthoganalytics.flag_definition_cache_provider = HyperCacheFlagProvider(...) before any flags are evaluated.

Changes

posthog/__init__.py

  • Added flag_definition_cache_provider module-level variable (defaults to None)
  • Passed it through setup() to the Client constructor

How did you test this code?

  • Existing tests pass (make test)

Downstream dependency

This PR must be merged and released before posthog#50737 can land, since the monolith PR sets posthoganalytics.flag_definition_cache_provider at startup.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 11, 2026

Last reviewed commit: 875b64b

@github-actions
Copy link
Contributor

github-actions bot commented Mar 11, 2026

posthog-python Compliance Report

Date: 2026-03-11 23:15:32 UTC
Duration: 159373ms

✅ All Tests Passed!

29/29 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 518ms
Format Validation.Event Has Uuid 1507ms
Format Validation.Event Has Lib Properties 1508ms
Format Validation.Distinct Id Is String 1507ms
Format Validation.Token Is Present 1507ms
Format Validation.Custom Properties Preserved 1507ms
Format Validation.Event Has Timestamp 1507ms
Retry Behavior.Retries On 503 9519ms
Retry Behavior.Does Not Retry On 400 3506ms
Retry Behavior.Does Not Retry On 401 3508ms
Retry Behavior.Respects Retry After Header 9515ms
Retry Behavior.Implements Backoff 23518ms
Retry Behavior.Retries On 500 7515ms
Retry Behavior.Retries On 502 7512ms
Retry Behavior.Retries On 504 7514ms
Retry Behavior.Max Retries Respected 23522ms
Deduplication.Generates Unique Uuids 1505ms
Deduplication.Preserves Uuid On Retry 7515ms
Deduplication.Preserves Uuid And Timestamp On Retry 14523ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 7504ms
Deduplication.No Duplicate Events In Batch 1508ms
Deduplication.Different Events Have Different Uuids 1507ms
Compression.Sends Gzip When Enabled 1507ms
Batch Format.Uses Proper Batch Structure 1507ms
Batch Format.Flush With No Events Sends Nothing 1005ms
Batch Format.Multiple Events Batched Together 1506ms
Error Handling.Does Not Retry On 403 3508ms
Error Handling.Does Not Retry On 413 3509ms
Error Handling.Retries On 408 7510ms

@haacked
Copy link
Collaborator

haacked commented Mar 11, 2026

The PR description mentions additional changes (stale cache fallback for failed flags in client.py, types.py, and a new test), but I don't see those in this PR.

Is the PR description incorrect or did you forget to push?

@matheus-vb
Copy link
Member Author

matheus-vb commented Mar 11, 2026

Oops those changes were supposed to be on another PR, will clean up the description

Copy link
Collaborator

@haacked haacked left a comment

Choose a reason for hiding this comment

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

:shipit:

@matheus-vb matheus-vb merged commit 1729be4 into master Mar 12, 2026
23 checks passed
@matheus-vb matheus-vb deleted the matheus-vb/module-level-flag-definition-cache-provider branch March 12, 2026 00:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants