Skip to content

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

Merged
haacked merged 3 commits into
mainfrom
haacked/minimal-flag-called-events
Jul 20, 2026
Merged

feat(flags): minimize $feature_flag_called events for non-experiment flags#261
haacked merged 3 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 (Config.DefaultEventProperties, request context, system context) on every flag call, even for flags not linked to an experiment. This PR trims those events 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 definitions payload) and the flag's has_experiment is exactly false. Any missing signal — gate absent, has_experiment unknown, experiment-linked flag, legacy response shapes — sends the full legacy shape unchanged.

This is server-gated because rolling it out unconditionally could break existing insights; announcement/comms come before enablement. The goal is to establish $feature_flag_called as a special system event.

Implementation specifics for this SDK:

  • The local-evaluation gate rides the poller's atomic flagsState swaps: lock-free reads, preserved across 304 Not Modified refreshes, reset on quota-limit. When local evaluation is inconclusive and the SDK falls back to a remote /flags request, the gate is reset and re-read from that response, since it is now the source of the flag value.
  • Enforcement happens at serialization time in both wire formats (legacy /batch and capture-v1) from one shared allowlist, so DefaultEventProperties, request-context properties, system context, and the snapshot path's $feature/<key> / $feature_flag_payload extras are excluded structurally rather than deleted piecemeal.
  • Kept properties: $feature_flag, $feature_flag_response, $feature_flag_has_experiment, $feature_flag_id, $feature_flag_version, $feature_flag_reason, $feature_flag_request_id, $feature_flag_evaluated_at, $feature_flag_error, locally_evaluated, $groups, $process_person_profile, $geoip_disable, $is_server, $session_id, $window_id, $device_id, plus $lib/$lib_version added at serialization. $geoip_disable and $is_server stay because they are processing-control/classification sentinels; the session/window/device IDs are linkage identifiers.
  • If a BeforeSend hook replaces the message, the minimal marker is lost and the event degrades fail-safe to the full shape.
  • No SDK configuration is added; the gate is server-controlled per project.

Part of a cross-SDK rollout; reference implementation and fuller context: PostHog/posthog-python#748.

💚 How did you test it?

  • go test -race -count=1 -timeout=5m ./... — ok
  • go vet ./... — clean
  • gofmt -l . — clean
  • bin/api-diff — public API snapshot up to date

Test coverage added:

  • Gate matrix for both sources (v2 /flags response and local-evaluation definitions payload): gated + no experiment, gated + experiment, gated + unknown has_experiment, ungated.
  • Exact-key-set assertions on minimal events (remote single-flag, local-eval single-flag, and EvaluateFlags snapshot paths), so any property leaking into — or dropping out of — the minimal shape fails the test.
  • Remote-fallback gate source in both directions: local gate on + ungated remote response stays full; local gate off + gated remote response goes minimal.
  • 304 ETag-only state swap preserves the cached gate.
  • Capture-v1 wire shape, including the $session_id top-level lift on a minimal event and $groups retention.

A changeset (.changeset/minimal-flag-called-events.md, minor) documents the behavior.

📝 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 pnpm changeset to generate a changeset file

Created with PostHog Code

When the /flags?v=2 response carries top-level minimalFlagCalledEvents
or the local-evaluation definitions payload carries
minimal_flag_called_events, and the evaluated flag has
has_experiment=false, serialize the $feature_flag_called event with a
strict allowlist of evaluation properties and skip system context,
DefaultEventProperties, and the snapshot path's $feature/<key> and
payload extras. Any missing signal (gate absent, has_experiment
unknown, experiment-linked flag, legacy response shapes) keeps the full
event shape. The local-eval gate is cached in the poller's atomic state
alongside flag definitions and survives 304 responses; quota-limit
resets clear it.

Generated-By: PostHog Code
Task-Id: ffe402fd-d75c-4043-8e5d-d2fe513cac6f
@haacked haacked self-assigned this Jul 16, 2026
@github-actions

github-actions Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

posthog-go Compliance Report

Date: 2026-07-18 04:27:17 UTC
Duration: 169248ms

✅ 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 24ms
Endpoint And Method.Does Not Use Legacy Endpoints 6ms
Required Headers.Has Authorization Bearer Header 7ms
Required Headers.Has Content Type Json 6ms
Required Headers.Has Posthog Sdk Info Format 15ms
Required Headers.Has Posthog Attempt Header 18ms
Required Headers.Has Posthog Request Id 6ms
Required Headers.Has Posthog Request Timestamp 6ms
Required Headers.Has User Agent 7ms
Body Format.Body Has Created At And Batch 6ms
Body Format.No Api Key In Body 6ms
Body Format.No Sent At In Body 7ms
Event Format.Event Has Required Root Fields 6ms
Event Format.Event Uuid Is Valid 5ms
Event Format.Event Timestamp Is Rfc3339 7ms
Event Format.Distinct Id Is String 6ms
Event Format.Distinct Id At Root Not Properties 6ms
Event Format.Custom Properties Preserved 6ms
Event Format.Set Properties Preserved 6ms
Event Format.Set Once Properties Preserved 6ms
Event Format.Groups Properties Preserved 6ms
Event Format.Sdk Generates Uuid If Not Provided 6ms
Event Format.Event Has Required Root Fields Batch 9ms
Event Format.Event Uuid Is Valid Batch 9ms
Event Format.Event Timestamp Is Rfc3339 Batch 7ms
Event Format.Distinct Id Is String Batch 20ms
Event Format.Distinct Id At Root Not Properties Batch 9ms
Event Format.Custom Properties Preserved Batch 8ms
Event Format.Set Properties Preserved Batch 9ms
Event Format.Set Once Properties Preserved Batch 8ms
Event Format.Groups Properties Preserved Batch 9ms
Event Format.Sdk Generates Uuid If Not Provided Batch 8ms
Batch Behavior.Multiple Events In Single Batch 31ms
Batch Behavior.Batch Envelope Smoke 10ms
Batch Behavior.Flush With No Events Sends Nothing 4ms
Batch Behavior.Flush At Triggers Batch 1007ms
Batch Behavior.Created At Reflects Batch Creation Time 7ms
Deduplication.Generates Unique Uuids 15ms
Deduplication.Different Events Same Content Different Uuids 9ms
Deduplication.Preserves Uuid On Retry 5135ms
Deduplication.Preserves Timestamp On Retry 5136ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 5138ms
Deduplication.No Duplicate Events In Batch 31ms
Header Behavior On Retry.Attempt Header Starts At One 7ms
Header Behavior On Retry.Attempt Header Increments On Retry 10135ms
Header Behavior On Retry.Request Id Preserved On Retry 5136ms
Header Behavior On Retry.Different Requests Have Different Request Ids 2012ms
Header Behavior On Retry.Request Timestamp Changes On Retry 5137ms
Response Format Validation.Success Response Has Uuid Keyed Results 7ms
Response Format Validation.Success Response Has Ok For Each Event 28ms
Response Format Validation.Success No Retry After When All Ok 28ms
Response Format Validation.Success Retry After Present When Retry Events 129ms
Response Format Validation.Success No Retry After When Drop Only 27ms
Response Format Validation.Response Echoes Request Id 7ms
Retry Behavior.Retries On 408 5132ms
Retry Behavior.Retries On 500 5136ms
Retry Behavior.Retries On 503 5136ms
Retry Behavior.Retries On 504 5136ms
Retry Behavior.Retryable Errors Have Retry After 2132ms
Retry Behavior.Respects Retry After On Retryable Error 8139ms
Retry Behavior.Does Not Retry On 400 2133ms
Retry Behavior.Does Not Retry On 401 2132ms
Retry Behavior.Does Not Retry On 402 2133ms
Retry Behavior.Does Not Retry On 413 2132ms
Retry Behavior.Does Not Retry On 415 2133ms
Retry Behavior.Non Retryable Errors Have No Retry After 2133ms
Retry Behavior.Implements Backoff 15145ms
Retry Behavior.Max Retries Respected 15146ms
Partial Batch Handling.Handles 200 Full Success 2009ms
Partial Batch Handling.Handles 200 With All Ok 3032ms
Partial Batch Handling.Does Not Retry Dropped Events 3031ms
Partial Batch Handling.Does Not Retry Limited Events 3031ms
Partial Batch Handling.Prunes Ok Events On Partial Retry 5136ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry 5137ms
Partial Batch Handling.Retries Only Retry Events From Partial 5134ms
Partial Batch Handling.Partial Retry Preserves Uuids 5136ms
Partial Batch Handling.Partial Retry Attempt Header Increments 5136ms
Partial Batch Handling.Partial Retry Request Id Preserved 5136ms
Partial Batch Handling.Respects Retry After On Partial 5136ms
Partial Batch Handling.Unknown Result Treated As Terminal 3029ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry 3035ms
Compression.Sends Gzip Content Encoding 16ms
Compression.No Content Encoding When Disabled 6ms
Compression.Compressed Body Is Decompressible 15ms
Error Handling.Does Not Retry On Unknown 4Xx 2132ms
Event Options.Cookieless Mode Override 7ms
Event Options.Disable Skew Correction Override 6ms
Event Options.Process Person Profile Override 6ms
Event Options.Product Tour Id Override 6ms
Event Options.Unset Options Omitted 6ms
Event Options.Options Override In Batch 8ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties 6ms
Geoip And Historical Migration.Historical Migration Set In Body 6ms
Geoip And Historical Migration.Historical Migration Absent By Default 6ms

Feature_Flags Tests

17/17 tests passed

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

Reviewer feedback on the Python equivalent of this PR pointed out that
$os, $os_version, $os_distro, and $go_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. Merge system context into
the minimal event the same way the full event does, for both the v0
and v1 wire formats.
@haacked
haacked marked this pull request as ready for review July 18, 2026 00:51
@haacked
haacked requested a review from a team as a code owner July 18, 2026 00:51
Extract Capture.selectedProperties() to remove the duplicated minimal/full
branching between APIfy (v0) and apifyEvent (v1). Add a direct test locking
the full 17-entry minimal allowlist contract, and harden the 304 gate-
persistence test to fail loudly on unexpected requests.
@haacked
haacked merged commit 9e9ad63 into main Jul 20, 2026
25 checks passed
@haacked
haacked deleted the haacked/minimal-flag-called-events branch July 20, 2026 17:04
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