-
Notifications
You must be signed in to change notification settings - Fork 27
feat: add evaluate_flags/2 API for single-call flag evaluation #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c81ae0d
feat: add evaluate_flags/2 API for single-call flag evaluation
dmarticus 60fa3e9
feat: propagate $feature_flag_error to $feature_flag_called
dmarticus c59530f
feat: deprecate check/check!/get_feature_flag_result(!) (Phase 2)
dmarticus 144c934
fix: address review feedback on evaluate_flags() PR
dmarticus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| --- | ||
| hex/posthog: minor | ||
| --- | ||
|
|
||
| Add `PostHog.FeatureFlags.evaluate_flags/2` and the `PostHog.FeatureFlags.Evaluations` snapshot so a single `/flags` call can power both flag branching and event enrichment for one request: | ||
|
|
||
| ```elixir | ||
| {:ok, snapshot} = PostHog.FeatureFlags.evaluate_flags("user-123") | ||
|
|
||
| if PostHog.FeatureFlags.Evaluations.enabled?(snapshot, "new-dashboard") do | ||
| render_new_dashboard() | ||
| end | ||
|
|
||
| PostHog.FeatureFlags.set_in_context(snapshot) | ||
| PostHog.capture("page_viewed", %{distinct_id: "user-123"}) | ||
| ``` | ||
|
|
||
| The snapshot exposes `enabled?/2`, `get_flag/2`, `get_flag_payload/2`, `only/2`, `only_accessed/1`, `accessed/1`, `keys/1`, and `event_properties/1`. Pass `flag_keys: [...]` to `evaluate_flags/2` to scope the underlying `/flags` request itself. When `distinct_id` cannot be resolved, `evaluate_flags/2` returns an empty snapshot whose accessors are no-ops (matching the cross-SDK behavior). | ||
|
|
||
| `$feature_flag_called` events fired from `check/3`, `check!/3`, `get_feature_flag_result/4`, and the new snapshot path now attach `$feature_flag_id`, `$feature_flag_version`, `$feature_flag_reason`, `$feature_flag_request_id`, `$feature_flag_payload`, `$feature/<key>`, and `$feature_flag_error` (combining `errors_while_computing_flags` and, on the snapshot path, `flag_missing`) when the response provides them. JSON-encoded payloads in `/flags` responses are now decoded before being attached to events and the `:payload` field on `%PostHog.FeatureFlags.Result{}`. The struct also gains `:id`, `:version`, `:reason`, `:request_id`, `:evaluated_at`, and `:errors_while_computing`. | ||
|
|
||
| `check/3`, `check!/3`, `get_feature_flag_result/4`, and `get_feature_flag_result!/4` are now marked `@deprecated` and emit compile-time warnings pointing at `evaluate_flags/2`. They continue to return the same values; removal is planned for the next major. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Compared to the other SDKs, we're missing
$feature/<key>,$feature_flag_payloadThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — added in 144c934.
log_feature_flag_usage/4now attaches$feature/<key>(string key, e.g."$feature/my-flag" => "variant1") and$feature_flag_payload(omitted when nil) on every$feature_flag_calledevent. Both the existing single-flag path and the new snapshot path emit them.New assertions in the "fires $feature_flag_called with full metadata" test cover both properties.