-
Notifications
You must be signed in to change notification settings - Fork 159
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
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
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.
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 insrc/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
mockLogger = jest.spyOn(console, 'warn').mockImplementation() | ||
instance = await createPosthog() |
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.
style: Console warning spy is created but no tests verify warning messages for quota limiting
src/posthog-featureflags.ts
Outdated
enum QuotaLimitedResource { | ||
FeatureFlags = 'feature_flags', | ||
Recordings = 'recordings', | ||
} |
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.
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 |
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.
logic: Setting _flagsLoadedFromRemote before checking quota could be misleading since flags aren't actually loaded when quota is exceeded
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 |
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.
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.
Size Change: +3.72 kB (+0.11%) Total Size: 3.32 MB
ℹ️ View Unchanged
|
@@ -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: {} })) |
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.
had to fix a squiggly red line
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.
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.' |
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.
This warning feels a bit too long – what do folks think?
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.
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.
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.
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.
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.
Agreed!
@@ -302,6 +307,15 @@ export class PostHogFeatureFlags { | |||
} | |||
|
|||
this._flagsLoadedFromRemote = !errorsLoading |
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.
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.
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.
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: {} })) |
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.
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.' |
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.
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.
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.