Skip to content

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

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 3 commits into from
Feb 27, 2025

Conversation

dmarticus
Copy link
Contributor

with PostHog/posthog#28564, we now start to respond different with the /decide and /local_evaluation APIs if users have gone over their quota limit. Now we need to change the SDKs to handle these new responses.

Copy link

vercel bot commented Feb 22, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
posthog-js ✅ Ready (Inspect) Visit Preview Feb 25, 2025 7:21pm

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

This PR implements quota limiting support for feature flags in the PostHog JavaScript SDK, handling cases where users exceed their feature flag usage limits.

  • Added QuotaLimitedResource enum in src/posthog-featureflags.ts to identify quota-limited resources
  • Added quota limit check in /decide endpoint response handling to prevent flag processing when limits are exceeded
  • Added warning message directing users to documentation when feature flag quota is exceeded
  • Added comprehensive test suite in src/__tests__/posthog-core.loaded.test.ts covering quota limiting scenarios
  • Implemented graceful degradation by preserving existing flag states when quota is exceeded

2 file(s) reviewed, 4 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines 114 to 115
mockLogger = jest.spyOn(console, 'warn').mockImplementation()
instance = await createPosthog()
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Console warning spy is created but no tests verify warning messages for quota limiting

Comment on lines 107 to 110
enum QuotaLimitedResource {
FeatureFlags = 'feature_flags',
Recordings = 'recordings',
}
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Consider making this enum exported since it may be useful for other parts of the codebase that need to handle quota limiting

@@ -302,6 +307,15 @@ export class PostHogFeatureFlags {
}

this._flagsLoadedFromRemote = !errorsLoading
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Setting _flagsLoadedFromRemote before checking quota could be misleading since flags aren't actually loaded when quota is exceeded

Suggested change
this._flagsLoadedFromRemote = !errorsLoading
if (response.json && response.json.quotaLimited?.includes(QuotaLimitedResource.FeatureFlags)) {
// log a warning and then early return
logger.warn(
'You have hit your feature flags quota limit, and will not be able to load feature flags until the quota is reset. Please visit https://posthog.com/docs/billing/limits-alerts to learn more.'
)
return
}
this._flagsLoadedFromRemote = !errorsLoading

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, but the variable name _flagsLoadedFromRemote is a bit misleading too – it implies that we're trying to use the flags from the /decide service, not that we actually end up using them.

Copy link

github-actions bot commented Feb 22, 2025

Size Change: +3.72 kB (+0.11%)

Total Size: 3.32 MB

Filename Size Change
dist/array.full.es5.js 270 kB +376 B (+0.14%)
dist/array.full.js 373 kB +372 B (+0.1%)
dist/array.full.no-external.js 372 kB +372 B (+0.1%)
dist/array.js 183 kB +372 B (+0.2%)
dist/array.no-external.js 182 kB +372 B (+0.2%)
dist/main.js 184 kB +372 B (+0.2%)
dist/module.full.js 373 kB +372 B (+0.1%)
dist/module.full.no-external.js 372 kB +372 B (+0.1%)
dist/module.js 183 kB +372 B (+0.2%)
dist/module.no-external.js 182 kB +372 B (+0.2%)
ℹ️ View Unchanged
Filename Size
dist/all-external-dependencies.js 218 kB
dist/customizations.full.js 14 kB
dist/dead-clicks-autocapture.js 14.5 kB
dist/exception-autocapture.js 9.51 kB
dist/external-scripts-loader.js 2.64 kB
dist/recorder-v2.js 115 kB
dist/recorder.js 115 kB
dist/surveys-preview.js 70.1 kB
dist/surveys.js 74.8 kB
dist/tracing-headers.js 1.76 kB
dist/web-vitals.js 10.4 kB

compressed-size-action

@dmarticus dmarticus requested a review from a team February 25, 2025 18:46
@@ -15,7 +15,7 @@ describe('loaded() with flags', () => {
...config,
loaded: (ph) => {
ph.capture = jest.fn()
ph._send_request = jest.fn(({ callback }) => callback?.({ status: 200, json: {} }))
ph._send_request = jest.fn(({ callback }) => callback?.({ statusCode: 200, json: {} }))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

had to fix a squiggly red line

Copy link
Contributor

Choose a reason for hiding this comment

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

Squiggly red lines are the worst!

if (response.json && response.json.quotaLimited?.includes(QuotaLimitedResource.FeatureFlags)) {
// log a warning and then early return
logger.warn(
'You have hit your feature flags quota limit, and will not be able to load feature flags until the quota is reset. Please visit https://posthog.com/docs/billing/limits-alerts to learn more.'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This warning feels a bit too long – what do folks think?

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems to be a lot longer than the other libraries. EIther we should go more detailed in all of them (with the URL), or keep them all short and make sure searching PostHog will find that page easily.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I have a docs PR for this here: PostHog/posthog.com#10653, but that's mostly for the API and it doesn't include information on resolution. It feels appropriate to put that in the logs for the SDKs. I think I'll do this everywhere, actually – if it were me using the SDK I'd be please to see a link to how to resolve a warning log in the log itself.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed!

@@ -302,6 +307,15 @@ export class PostHogFeatureFlags {
}

this._flagsLoadedFromRemote = !errorsLoading
Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, but the variable name _flagsLoadedFromRemote is a bit misleading too – it implies that we're trying to use the flags from the /decide service, not that we actually end up using them.

Copy link
Contributor

@haacked haacked left a comment

Choose a reason for hiding this comment

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

:shipit: Looks good to me.

@@ -15,7 +15,7 @@ describe('loaded() with flags', () => {
...config,
loaded: (ph) => {
ph.capture = jest.fn()
ph._send_request = jest.fn(({ callback }) => callback?.({ status: 200, json: {} }))
ph._send_request = jest.fn(({ callback }) => callback?.({ statusCode: 200, json: {} }))
Copy link
Contributor

Choose a reason for hiding this comment

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

Squiggly red lines are the worst!

if (response.json && response.json.quotaLimited?.includes(QuotaLimitedResource.FeatureFlags)) {
// log a warning and then early return
logger.warn(
'You have hit your feature flags quota limit, and will not be able to load feature flags until the quota is reset. Please visit https://posthog.com/docs/billing/limits-alerts to learn more.'
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems to be a lot longer than the other libraries. EIther we should go more detailed in all of them (with the URL), or keep them all short and make sure searching PostHog will find that page easily.

@dmarticus dmarticus added the bump minor Bump minor version when this PR gets merged label Feb 26, 2025
@dmarticus dmarticus merged commit ace91db into main Feb 27, 2025
28 checks passed
@dmarticus dmarticus deleted the feat/quota-limited-flags branch February 27, 2025 00:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump minor Bump minor version when this PR gets merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants