Skip to content

Commit 68baae7

Browse files
authored
fix: keep flag payload reads out of only_accessed (#112)
1 parent a59a634 commit 68baae7

3 files changed

Lines changed: 24 additions & 21 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
hex/posthog: patch
3+
---
4+
5+
Do not count `get_flag_payload/2` calls as accesses for `only_accessed/1`.

lib/posthog/feature_flags/evaluations.ex

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ defmodule PostHog.FeatureFlags.Evaluations do
88
paying the cost of one round-trip per flag.
99
1010
Each snapshot owns a small `Agent` linked to the calling process that tracks
11-
which flags were accessed via `enabled?/2`, `get_flag/2`, and
12-
`get_flag_payload/2`. The Agent exits with the calling process — no manual
13-
cleanup is required.
11+
which flags were accessed via `enabled?/2` and `get_flag/2`. The Agent exits
12+
with the calling process — no manual cleanup is required.
1413
1514
## Querying
1615
1716
Use `enabled?/2`, `get_flag/2`, and `get_flag_payload/2` to read individual
18-
flags. `enabled?/2` and `get_flag/2` fire a `$feature_flag_called` event
19-
with full metadata (id, version, reason, request_id) on each call;
20-
`get_flag_payload/2` records the access without firing an event.
17+
flags. `enabled?/2` and `get_flag/2` record access and fire a
18+
`$feature_flag_called` event with full metadata (id, version, reason,
19+
request_id) on each call; `get_flag_payload/2` does neither.
2120
2221
{:ok, snapshot} = PostHog.FeatureFlags.evaluate_flags("user-123")
2322
@@ -41,9 +40,9 @@ defmodule PostHog.FeatureFlags.Evaluations do
4140
## Filtering
4241
4342
Use `only_accessed/1` to narrow a snapshot to flags accessed so far via
44-
`enabled?/2`, `get_flag/2`, or `get_flag_payload/2`. Use `only/2` to narrow
45-
by an explicit key list. Both return a fresh snapshot with its own access
46-
tracker — calls on the filtered view do not back-propagate to the parent.
43+
`enabled?/2` or `get_flag/2`. Use `only/2` to narrow by an explicit key list.
44+
Both return a fresh snapshot with its own access tracker — calls on the
45+
filtered view do not back-propagate to the parent.
4746
4847
narrowed = PostHog.FeatureFlags.Evaluations.only_accessed(snapshot)
4948
PostHog.FeatureFlags.set_in_context(narrowed)
@@ -157,14 +156,13 @@ defmodule PostHog.FeatureFlags.Evaluations do
157156

158157
@doc """
159158
Returns the configured payload for the flag, or `nil` for unknown flags or
160-
flags without a payload. Records the access.
159+
flags without a payload.
161160
162-
Does **not** fire a `$feature_flag_called` event.
161+
Does **not** record access for `only_accessed/1` or fire a
162+
`$feature_flag_called` event.
163163
"""
164164
@spec get_flag_payload(t(), String.t()) :: any() | nil
165-
def get_flag_payload(%__MODULE__{flags: flags} = snapshot, key) when is_binary(key) do
166-
record_access(snapshot, key)
167-
165+
def get_flag_payload(%__MODULE__{flags: flags}, key) when is_binary(key) do
168166
case Map.fetch(flags, key) do
169167
{:ok, %Result{payload: payload}} -> payload
170168
:error -> nil
@@ -178,8 +176,8 @@ defmodule PostHog.FeatureFlags.Evaluations do
178176
def keys(%__MODULE__{flags: flags}), do: flags |> Map.keys() |> Enum.sort()
179177

180178
@doc """
181-
Returns the sorted list of keys accessed via `enabled?/2`, `get_flag/2`, or
182-
`get_flag_payload/2` on this snapshot.
179+
Returns the sorted list of keys accessed via `enabled?/2` or `get_flag/2` on
180+
this snapshot.
183181
184182
Includes keys that were accessed but absent from the snapshot.
185183
"""
@@ -190,7 +188,7 @@ defmodule PostHog.FeatureFlags.Evaluations do
190188

191189
@doc """
192190
Returns a copy of the snapshot scoped to the flags accessed so far via
193-
`enabled?/2`, `get_flag/2`, or `get_flag_payload/2`.
191+
`enabled?/2` or `get_flag/2`.
194192
195193
Returns an empty snapshot when nothing has been accessed yet — including
196194
no flags would be more surprising than helpful, since the caller asked for

test/posthog/feature_flags/evaluations_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ defmodule PostHog.FeatureFlags.EvaluationsTest do
1010
alias PostHog.API
1111
alias PostHog.FeatureFlags
1212
alias PostHog.FeatureFlags.Evaluations
13-
alias PostHog.FeatureFlags.Result
1413

1514
setup :setup_supervisor
1615
setup :verify_on_exit!
@@ -250,11 +249,12 @@ defmodule PostHog.FeatureFlags.EvaluationsTest do
250249
assert Evaluations.get_flag_payload(snapshot, "unknown-flag") == nil
251250
end
252251

253-
test "does not fire a $feature_flag_called event", %{snapshot: snapshot} do
252+
test "does not fire a $feature_flag_called event or count as access", %{snapshot: snapshot} do
254253
Evaluations.get_flag_payload(snapshot, "variant-flag")
255254
Evaluations.get_flag_payload(snapshot, "unknown-flag")
256255

257256
assert all_captured() == []
257+
assert Evaluations.accessed(snapshot) == []
258258
end
259259
end
260260

@@ -280,10 +280,10 @@ defmodule PostHog.FeatureFlags.EvaluationsTest do
280280
assert Evaluations.keys(narrowed) == ["variant-flag"]
281281
end
282282

283-
test "narrows the snapshot to flags accessed via get_flag_payload/2", %{snapshot: snapshot} do
283+
test "does not include flags read only via get_flag_payload/2", %{snapshot: snapshot} do
284284
Evaluations.get_flag_payload(snapshot, "variant-flag")
285285
narrowed = Evaluations.only_accessed(snapshot)
286-
assert Evaluations.keys(narrowed) == ["variant-flag"]
286+
assert Evaluations.keys(narrowed) == []
287287
end
288288

289289
test "returns an empty snapshot when nothing has been accessed", %{snapshot: snapshot} do

0 commit comments

Comments
 (0)