Commit 1ad4f2b
authored
feat: add evaluate_flags() API for single-call flag evaluation (#137)
* feat: add evaluate_flags() API for single-call flag evaluation
Add Client#evaluate_flags(distinct_id, ...) returning a
FeatureFlagEvaluations snapshot, and a flags: option on capture so
a single /flags call can power both flag branching and event
enrichment per request.
The snapshot exposes is_enabled, get_flag, get_flag_payload, plus
only_accessed / only([keys]) filter helpers. flag_keys: scopes the
underlying /flags request itself. is_enabled and get_flag fire
$feature_flag_called events with full metadata (id, version, reason,
request_id), deduped through the existing per-distinct_id cache.
get_flag_payload does not record access or fire an event.
The dedup + capture in get_feature_flag_result is extracted into
_capture_feature_flag_called_if_needed and shared between the existing
path and the snapshot's access-recording.
Existing is_feature_enabled, get_feature_flag, get_feature_flag_result,
get_feature_flag_payload, and capture(send_feature_flags:) continue
to work unchanged.
Generated-By: PostHog Code
Task-Id: fe67a5bd-7d33-4568-9148-39d181660a5a
* feat: incorporate posthog-python#539 review feedback
Mirrors the changes that landed on the Python PR after review:
- only_accessed returns an empty snapshot when nothing has been
accessed (drops the warn-and-fall-back-to-all-flags behavior — that
was a misguided safety net that surprised callers in the
early-pre-access pattern).
- capture(flags:, send_feature_flags:) now warns when both are passed,
uses the snapshot, and ignores send_feature_flags. The snapshot
guarantees the event carries the values branched on; the precedence
was previously implicit.
- $feature_flag_called events now carry response-level errors
(errors_while_computing_flags, quota_limited) combined with per-flag
errors (flag_missing) as a comma-joined $feature_flag_error, matching
the granularity of the legacy single-flag path.
- capture_exception now accepts a flags: kwarg and forwards it to the
inner capture() so $exception events can carry the same flag context
as other events.
- Phase 2 deprecation warnings ship alongside Phase 1: is_feature_enabled,
get_feature_flag, get_feature_flag_result, get_feature_flag_payload,
and capture(send_feature_flags:) emit Kernel.warn(..., category:
:deprecated) pointing at evaluate_flags(). Public methods bypass
each other internally (via a new private _get_feature_flag_result)
so a single user-level call emits exactly one warning.
Generated-By: PostHog Code
Task-Id: fe67a5bd-7d33-4568-9148-39d181660a5a
* feat: address Dustin's review feedback on evaluate_flags PR
Snapshot:
- Rename is_enabled → enabled? for Ruby idiom (the legacy
client-level is_feature_enabled keeps its name but is now deprecated).
- Canonicalize the recorded $feature_flag_response across
enabled?/get_flag so a variant flag accessed both ways collapses to
one $feature_flag_called event (with the variant value) instead of
two with mismatched booleans. Same fix for missing flags: response
is nil regardless of which method was called.
Client / poller:
- Drop the feature_flags_log_warnings config option in favor of
standard log-level gating: warnings flow through logger.warn so
callers silence them via their existing log config (mirrors the
posthog-android feedback).
- Skip the remote /flags call when flag_keys are all locally
resolved. Without flag_keys we still hit the server because we don't
know about server-side-only flags.
- Forward disable_geoip to the /flags request body as geoip_disable.
- Use a Set for flag_keys lookup to keep the per-flag check O(1).
- Normalize remote and local-eval payloads via
FeatureFlagResult.parse_payload (made public — same contract as
FeatureFlagResult.from_value_and_payload uses internally).
- Guard capture(flags:) against non-snapshot values: log a warning
and ignore instead of raising NoMethodError.
- Move _get_feature_flag_result into the private section.
Deprecation warnings:
- Drop the :deprecated category — Ruby suppresses those by default
since 2.7.2 and most users wouldn't see them. Use Kernel.warn with
a [posthog-ruby] DEPRECATION prefix; standard log-level / IO
silencing still works.
- Emit each deprecation at most once per (method, client) pair via
Concurrent::Set so high-volume callers don't pay a per-call cost.
Generated-By: PostHog Code
Task-Id: fe67a5bd-7d33-4568-9148-39d181660a5a
* chore: update changeset to match enabled? rename
The changeset was written before the is_enabled → enabled? rename
that came out of Dustin's review; align the example snippet and
description so the generated CHANGELOG entry uses the final API
naming. Also softens "DeprecationWarning" to "one-time deprecation
warning per method" since we dropped the :deprecated category in
favor of always-visible Kernel.warn.
Generated-By: PostHog Code
Task-Id: fe67a5bd-7d33-4568-9148-39d181660a5a1 parent d847947 commit 1ad4f2b
7 files changed
Lines changed: 928 additions & 62 deletions
File tree
- .changeset
- lib
- posthog
- spec/posthog
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
42 | 46 | | |
43 | 47 | | |
44 | 48 | | |
| |||
50 | 54 | | |
51 | 55 | | |
52 | 56 | | |
53 | | - | |
54 | | - | |
55 | 57 | | |
56 | 58 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
49 | 50 | | |
50 | 51 | | |
51 | 52 | | |
| |||
72 | 73 | | |
73 | 74 | | |
74 | 75 | | |
| 76 | + | |
| 77 | + | |
75 | 78 | | |
76 | 79 | | |
77 | 80 | | |
| |||
120 | 123 | | |
121 | 124 | | |
122 | 125 | | |
123 | | - | |
| 126 | + | |
| 127 | + | |
124 | 128 | | |
125 | 129 | | |
126 | 130 | | |
127 | 131 | | |
128 | 132 | | |
129 | 133 | | |
| 134 | + | |
| 135 | + | |
130 | 136 | | |
131 | 137 | | |
132 | 138 | | |
| |||
1124 | 1130 | | |
1125 | 1131 | | |
1126 | 1132 | | |
| 1133 | + | |
1127 | 1134 | | |
1128 | 1135 | | |
1129 | 1136 | | |
| |||
0 commit comments