Skip to content

feat(flags): minimize $feature_flag_called events for non-experiment flags#748

Merged
haacked merged 8 commits into
mainfrom
haacked/minimal-flag-called-events
Jul 18, 2026
Merged

feat(flags): minimize $feature_flag_called events for non-experiment flags#748
haacked merged 8 commits into
mainfrom
haacked/minimal-flag-called-events

Conversation

@haacked

@haacked haacked commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

$feature_flag_called events carry the full enriched properties dict (super properties, context tags, custom event properties, system context) on every flag call, even for the vast majority of flags that aren't linked to an experiment. This PR minimizes those events for non-experiment flags when the server enables it, cutting event size and ingestion cost without losing anything needed for exposure analysis.

The event is trimmed to a strict allowlist iff the server-controlled gate is on (minimalFlagCalledEvents in the v2 /flags response, or minimal_flag_called_events in the local-evaluation payload) and the evaluated flag's has_experiment is exactly false. Any missing signal, gate absent, has_experiment unknown, or the flag linked to an experiment, sends the full legacy shape unchanged. The gate is stored alongside cached flag state so it survives polls, 304 responses, and external flag_cache round-trips.

The stale-cache fallback path (_get_stale_flag_fallback) never populates flag_details, so has_experiment stays unknown there and those events correctly fall back to the full shape instead of risking an incorrect minimization.

The reason for the server-gate is we can't just roll this out to everyone. It could break existing insights, etc. We're going to have to make an announcement first and give people time to adjust.

However, we can use the gate to roll this new behavior out for new teams (for example) after we figure out comms for that. The main goal is to establish $feature_flag_called as a special system event and not a general event given we don't charge for these events.

💚 How did you test it?

uv run pytest posthog/test/test_feature_flag_called_minimization.py posthog/test/test_flag_definition_cache.py (49 tests). The new test file covers the gate on/off, has_experiment true/false/missing, local vs. remote evaluation, group context preservation, and the async cache provider with the gate enabled.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

haacked added 2 commits July 16, 2026 13:13
Emit a minimal $feature_flag_called event iff the server-controlled gate
is on (top-level minimalFlagCalledEvents in the v2 /flags response, or
minimal_flag_called_events in the local-evaluation payload) and the
evaluated flag's has_experiment is false. The minimal shape is a strict
allowlist applied to the fully-enriched properties dict, so super
properties, context tags, custom properties, and system context are all
stripped from minimized events. Any missing signal (gate absent, legacy
response shape, has_experiment missing) sends the full legacy shape.
The gate is stored alongside cached flag state so it survives polls,
304 responses, and external cache round-trips.

Generated-By: PostHog Code
Task-Id: ffe402fd-d75c-4043-8e5d-d2fe513cac6f
Add $is_server and $geoip_disable (already present) to the strict allowlist
for minimized $feature_flag_called events. Refactor has_experiment
extraction into a helper function to reduce duplication and improve
maintainability. Add tests for group context preservation and async
cache provider with minimization gate enabled.
@haacked haacked changed the title Minimize $feature_flag_called events for non-experiment flags feat(flags): minimize $feature_flag_called events for non-experiment flags Jul 16, 2026
@github-actions

github-actions Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-07-18 00:42:26 UTC
Duration: 338278ms

✅ All Tests Passed!

111/111 tests passed


Capture_V1 Tests

94/94 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint 516ms
Endpoint And Method.Does Not Use Legacy Endpoints 1009ms
Required Headers.Has Authorization Bearer Header 1009ms
Required Headers.Has Content Type Json 1008ms
Required Headers.Has Posthog Sdk Info Format 1008ms
Required Headers.Has Posthog Attempt Header 1008ms
Required Headers.Has Posthog Request Id 1009ms
Required Headers.Has Posthog Request Timestamp 1008ms
Required Headers.Has User Agent 1008ms
Body Format.Body Has Created At And Batch 1007ms
Body Format.No Api Key In Body 1009ms
Body Format.No Sent At In Body 1008ms
Event Format.Event Has Required Root Fields 1008ms
Event Format.Event Uuid Is Valid 1008ms
Event Format.Event Timestamp Is Rfc3339 1008ms
Event Format.Distinct Id Is String 1008ms
Event Format.Distinct Id At Root Not Properties 1008ms
Event Format.Custom Properties Preserved 1009ms
Event Format.Set Properties Preserved 1008ms
Event Format.Set Once Properties Preserved 1009ms
Event Format.Groups Properties Preserved 1008ms
Event Format.Sdk Generates Uuid If Not Provided 1008ms
Event Format.Event Has Required Root Fields Batch 1011ms
Event Format.Event Uuid Is Valid Batch 1012ms
Event Format.Event Timestamp Is Rfc3339 Batch 1011ms
Event Format.Distinct Id Is String Batch 1013ms
Event Format.Distinct Id At Root Not Properties Batch 1011ms
Event Format.Custom Properties Preserved Batch 1011ms
Event Format.Set Properties Preserved Batch 1012ms
Event Format.Set Once Properties Preserved Batch 1013ms
Event Format.Groups Properties Preserved Batch 1011ms
Event Format.Sdk Generates Uuid If Not Provided Batch 1012ms
Batch Behavior.Multiple Events In Single Batch 1507ms
Batch Behavior.Batch Envelope Smoke 1014ms
Batch Behavior.Flush With No Events Sends Nothing 1005ms
Batch Behavior.Flush At Triggers Batch 1507ms
Batch Behavior.Created At Reflects Batch Creation Time 510ms
Deduplication.Generates Unique Uuids 1507ms
Deduplication.Different Events Same Content Different Uuids 1507ms
Deduplication.Preserves Uuid On Retry 7511ms
Deduplication.Preserves Timestamp On Retry 7516ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 7507ms
Deduplication.No Duplicate Events In Batch 1508ms
Header Behavior On Retry.Attempt Header Starts At One 1008ms
Header Behavior On Retry.Attempt Header Increments On Retry 14519ms
Header Behavior On Retry.Request Id Preserved On Retry 7513ms
Header Behavior On Retry.Different Requests Have Different Request Ids 3514ms
Header Behavior On Retry.Request Timestamp Changes On Retry 7514ms
Response Format Validation.Success Response Has Uuid Keyed Results 1005ms
Response Format Validation.Success Response Has Ok For Each Event 1507ms
Response Format Validation.Success No Retry After When All Ok 1506ms
Response Format Validation.Success Retry After Present When Retry Events 2510ms
Response Format Validation.Success No Retry After When Drop Only 1507ms
Response Format Validation.Response Echoes Request Id 1008ms
Retry Behavior.Retries On 408 7513ms
Retry Behavior.Retries On 500 7512ms
Retry Behavior.Retries On 503 9512ms
Retry Behavior.Retries On 504 7511ms
Retry Behavior.Retryable Errors Have Retry After 4513ms
Retry Behavior.Respects Retry After On Retryable Error 12511ms
Retry Behavior.Does Not Retry On 400 3509ms
Retry Behavior.Does Not Retry On 401 3508ms
Retry Behavior.Does Not Retry On 402 3507ms
Retry Behavior.Does Not Retry On 413 3507ms
Retry Behavior.Does Not Retry On 415 3507ms
Retry Behavior.Non Retryable Errors Have No Retry After 3508ms
Retry Behavior.Implements Backoff 23519ms
Retry Behavior.Max Retries Respected 23532ms
Partial Batch Handling.Handles 200 Full Success 3000ms
Partial Batch Handling.Handles 200 With All Ok 4507ms
Partial Batch Handling.Does Not Retry Dropped Events 4508ms
Partial Batch Handling.Does Not Retry Limited Events 4508ms
Partial Batch Handling.Prunes Ok Events On Partial Retry 7514ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry 7513ms
Partial Batch Handling.Retries Only Retry Events From Partial 7507ms
Partial Batch Handling.Partial Retry Preserves Uuids 7515ms
Partial Batch Handling.Partial Retry Attempt Header Increments 7512ms
Partial Batch Handling.Partial Retry Request Id Preserved 7512ms
Partial Batch Handling.Respects Retry After On Partial 9511ms
Partial Batch Handling.Unknown Result Treated As Terminal 4505ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry 4509ms
Compression.Sends Gzip Content Encoding 1006ms
Compression.No Content Encoding When Disabled 1009ms
Compression.Compressed Body Is Decompressible 1008ms
Error Handling.Does Not Retry On Unknown 4Xx 3508ms
Event Options.Cookieless Mode Override 1008ms
Event Options.Disable Skew Correction Override 1009ms
Event Options.Process Person Profile Override 1008ms
Event Options.Product Tour Id Override 1007ms
Event Options.Unset Options Omitted 1009ms
Event Options.Options Override In Batch 1011ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties 1009ms
Geoip And Historical Migration.Historical Migration Set In Body 1008ms
Geoip And Historical Migration.Historical Migration Absent By Default 1009ms

Feature_Flags Tests

17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 1006ms
Request Payload.Flags Request Uses V2 Query Param 1007ms
Request Payload.Flags Request Hits Flags Path Not Decide 1006ms
Request Payload.Flags Request Omits Authorization Header 1007ms
Request Payload.Token In Flags Body Matches Init 1006ms
Request Payload.Groups Round Trip 1007ms
Request Payload.Groups Default To Empty Object 1006ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 1006ms
Request Payload.Disable Geoip Omitted Defaults To False 1006ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 1007ms
Request Lifecycle.No Flags Request On Init Alone 502ms
Request Lifecycle.No Flags Request On Normal Capture 1507ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 1010ms
Request Lifecycle.Mock Response Value Is Returned To Caller 1003ms
Retry Behavior.Retries Flags On 502 1006ms
Retry Behavior.Retries Flags On 504 1006ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 1509ms

@greptile-apps

greptile-apps Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "Add $is_server to minimal flag called pr..." | Re-trigger Greptile

Comment thread posthog/client.py Outdated
Comment thread posthog/client.py Outdated
Comment thread posthog/client.py
haacked added 3 commits July 16, 2026 16:14
evaluate_flags() snapshots fire $feature_flag_called on deferred access,
so reading the live client gate at send time let a gate change from a
LATER response reshape events for flags evaluated earlier. The gate is
now captured on the snapshot at construction and passed through to the
capture funnel, so each event's shape matches the evaluation that
produced it. The single-flag path is unchanged: it fires immediately
after evaluation, so the live gate is that evaluation's gate.

@dustinbyrne dustinbyrne left a comment

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.

Human-driven, agent-assisted review.

Comment thread posthog/client.py
Comment thread posthog/client.py
haacked added 3 commits July 17, 2026 16:26
Reviewer feedback on PR #748 pointed out that $os, $os_version,
$os_distro, $python_runtime, and $python_version were stripped from
minimal $feature_flag_called events along with the rest of system
context, even though they're cheap, static per process, and useful
for debugging flag behavior by platform. Keep them in the allowlist.
…tion

When evaluating a flag remotely, the gate controlling minimal $feature_flag_called
events is now sourced from that specific response, not the client-wide gate. This
ensures concurrent gate changes (e.g., from a snapshot refresh or another /flags
call) can't reshape an event after the fact. The response's gate is pinned at
evaluation time and carried through to event capture.

Adds a test case verifying this behavior under concurrent gate updates.
Pin the gate from the response received during remote fallback, rather than
reading the client-wide attribute at snapshot construction time. This ensures
the gate is consistent even if the poller updates it concurrently. Add test
to verify snapshot uses its response's gate, not concurrent updates.
@haacked
haacked merged commit 5ef2c23 into main Jul 18, 2026
37 checks passed
@haacked
haacked deleted the haacked/minimal-flag-called-events branch July 18, 2026 00:49
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.

2 participants