Skip to content

fix: Dedupe feature flag called events by response#150

Merged
marandaneto merged 5 commits into
mainfrom
fix/ff-called-value-dedupe
Jun 25, 2026
Merged

fix: Dedupe feature flag called events by response#150
marandaneto merged 5 commits into
mainfrom
fix/ff-called-value-dedupe

Conversation

@marandaneto

Copy link
Copy Markdown
Member

💡 Motivation and Context

Repeated feature flag reads currently emit duplicate $feature_flag_called events for the same distinct ID, flag, and returned response value. This adds per-supervisor deduplication so duplicate reads for the same response are suppressed while still emitting again when the response changes.

💚 How did you test it?

  • mix format --check-formatted
  • mix test test/posthog/feature_flags/evaluations_test.exs
  • mix test

📝 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

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

A coding agent prepared this PR from the dedicated fix/ff-called-value-dedupe worktree at the user's request. The implementation keeps deduplication scoped to the PostHog supervisor by registering an Agent-backed cache under the existing registry, suppresses repeated $feature_flag_called captures for the same distinct ID/flag/response tuple, preserves no-op behavior when no supervisor is running, and adds tests for same-value dedupe and changed-value recapture.

@marandaneto marandaneto self-assigned this Jun 24, 2026
@github-actions

github-actions Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

posthog-elixir Compliance Report

Date: 2026-06-24 20:42:22 UTC
Duration: 107381ms

⚠️ Some Tests Failed

42/45 tests passed, 3 failed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 610ms
Format Validation.Event Has Uuid 610ms
Format Validation.Event Has Lib Properties 610ms
Format Validation.Distinct Id Is String 609ms
Format Validation.Token Is Present 611ms
Format Validation.Custom Properties Preserved 610ms
Format Validation.Event Has Timestamp 610ms
Retry Behavior.Retries On 503 5615ms
Retry Behavior.Does Not Retry On 400 2613ms
Retry Behavior.Does Not Retry On 401 2613ms
Retry Behavior.Respects Retry After Header 5615ms
Retry Behavior.Implements Backoff 15612ms
Retry Behavior.Retries On 500 5616ms
Retry Behavior.Retries On 502 5615ms
Retry Behavior.Retries On 504 5615ms
Retry Behavior.Max Retries Respected 15617ms
Deduplication.Generates Unique Uuids 620ms
Deduplication.Preserves Uuid On Retry 5615ms
Deduplication.Preserves Uuid And Timestamp On Retry 10620ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 5617ms
Deduplication.No Duplicate Events In Batch 614ms
Deduplication.Different Events Have Different Uuids 612ms
Compression.Sends Gzip When Enabled 610ms
Batch Format.Uses Proper Batch Structure 609ms
Batch Format.Flush With No Events Sends Nothing 607ms
Batch Format.Multiple Events Batched Together 615ms
Error Handling.Does Not Retry On 403 2610ms
Error Handling.Does Not Retry On 413 2613ms
Error Handling.Retries On 408 5616ms

Feature_Flags Tests

⚠️ 13/16 tests passed, 3 failed

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

Failures

request_payload.disable_geoip_omitted_defaults_to_false

Field 'geoip_disable' not found in /flags request body at path 'geoip_disable'. Available keys: ['groups', 'api_key', 'distinct_id', 'flag_keys_to_evaluate', 'group_properties', 'person_properties']

request_lifecycle.mock_response_value_is_returned_to_caller

Last action result missing field 'value'. Keys: ['error', 'success']

side_effect_events.get_feature_flag_captures_feature_flag_called_event

Expected 1 events with name '$feature_flag_called', got 0

@greptile-apps

greptile-apps Bot commented Jun 24, 2026

Copy link
Copy Markdown

Reviews (1): Last reviewed commit: "fix: dedupe feature flag called events b..." | Re-trigger Greptile

Comment thread lib/posthog/feature_flags/called_cache.ex Outdated
Comment thread lib/posthog/feature_flags/called_cache.ex Outdated
Comment thread test/posthog/feature_flags/evaluations_test.exs
@marandaneto marandaneto marked this pull request as ready for review June 24, 2026 17:33
@marandaneto marandaneto requested a review from a team as a code owner June 24, 2026 17:33
@martosaur

Copy link
Copy Markdown
Contributor

Using agent as a cache is a bit dangerous for production as that means all feature flag calls will be synchronously routed through a single process, creating a bottleneck.

@marandaneto

Copy link
Copy Markdown
Member Author

Using agent as a cache is a bit dangerous for production as that means all feature flag calls will be synchronously routed through a single process, creating a bottleneck.

Mm good point, any suggestion?

@martosaur

Copy link
Copy Markdown
Contributor

Usually an ETS table is used in some form with an attached process that manages it's lifecycle/eviction/etc. There's also a way to piggyback on the Registry for convenience. Not sure which way I would personally go, would likely need to spend some time exploring to get a feel for what works best. I can take a look at this if you're ok with waiting.

Comment on lines +19 to +20
def first_seen?(supervisor_name, distinct_id, flag_key, value) do
key = {to_string(distinct_id), flag_key, value}

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.

this doesn't account for groups in the cache key

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.

log_feature_flag_usage doesn't account for it either, but it should be able to be provided to FeatureFlags.check, get_feature_flag_result, evaluate_flags via the body

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yeah groups aren't accounted for in a few SDKs, i think i'd do this as a follow up since i can check/update the spec and do all at once later

@marandaneto

marandaneto commented Jun 24, 2026

Copy link
Copy Markdown
Member Author

@martosaur sounds good, not in a rush
i changed to an ETS table, if you think this is better, we can merge this as is and make it better later if you come up with a better plan

@marandaneto marandaneto requested review from a team and dustinbyrne June 24, 2026 20:55
@marandaneto marandaneto merged commit 4d2af12 into main Jun 25, 2026
30 checks passed
@marandaneto marandaneto deleted the fix/ff-called-value-dedupe branch June 25, 2026 15:22
@marandaneto

Copy link
Copy Markdown
Member Author

@martosaur sounds good, not in a rush i changed to an ETS table, if you think this is better, we can merge this as is and make it better later if you come up with a better plan

merged this but happy to hear if theres a better way, we can fix forward.

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.

3 participants