Skip to content
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

feat(feature-flags): support quota limiting for feature flags #85 #47

Merged
merged 4 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 2.7.0 – 2025-02-24

1. Add support for quota-limited feature flags

## 2.6.0 - 2025-02-13

1. Add method for fetching decrypted remote config flag payload

## 2.5.1 - 2024-12-19

1. Adds a new, optional `distinct_id` parameter to group identify calls which allows specifying the Distinct ID for the event.
Expand Down
11 changes: 9 additions & 2 deletions lib/posthog/feature_flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,15 @@ def get_all_flags_and_payloads(distinct_id, groups = {}, person_properties = {},
unless flags_and_payloads.key?(:featureFlags)
raise StandardError.new("Error flags response: #{flags_and_payloads}")
end
flags = stringify_keys(flags_and_payloads[:featureFlags] || {})
payloads = stringify_keys(flags_and_payloads[:featureFlagPayloads] || {})

# Check if feature_flags are quota limited
if flags_and_payloads[:quotaLimited]&.include?("feature_flags")
flags = {}
payloads = {}
else
flags = stringify_keys(flags_and_payloads[:featureFlags] || {})
payloads = stringify_keys(flags_and_payloads[:featureFlagPayloads] || {})
end
rescue StandardError => e
@on_error.call(-1, "Error computing flag remotely: #{e}")
raise if raise_on_error
Expand Down
2 changes: 1 addition & 1 deletion lib/posthog/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class PostHog
VERSION = '2.6.0'
VERSION = '2.7.0'
end
43 changes: 43 additions & 0 deletions spec/posthog/feature_flag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4008,6 +4008,49 @@ class PostHog
expect(c.get_remote_config_payload(encrypted_payload_flag_key))
assert_not_requested :post, decide_endpoint
end

it 'handles quota limited response by unsetting all flags' do
flag_res = [
{
"id": 1,
"name": "Beta Feature",
"key": "beta-feature",
"is_simple_flag": false,
"active": true,
"rollout_percentage": 100,
"filters": {
"groups": [
{
"properties": [{"key": "country", "value": "US"}],
"rollout_percentage": 0,
}
],
"payloads": {
"true": "payload-1",
},
},
}
]

stub_request(
:get,
'https://app.posthog.com/api/feature_flag/local_evaluation?token=testsecret'
).to_return(status: 200, body: {"flags": flag_res}.to_json)

stub_request(:post, decide_endpoint)
.to_return(status: 200, body: {
"featureFlags": {"beta-feature": true, "other-flag": false},
"featureFlagPayloads": {"beta-feature": "some-payload"},
"quotaLimited": ["feature_flags"]
}.to_json)

c = Client.new(api_key: API_KEY, personal_api_key: API_KEY, test_mode: true)

result = c.get_all_flags_and_payloads("distinct_id")
expect(result[:featureFlags]).to eq({})
expect(result[:featureFlagPayloads]).to eq({})
assert_requested :post, decide_endpoint, times: 1
end
end

describe 'resiliency' do
Expand Down