Skip to content

feat(flags): roll new flags to 1% of cloud customers #30905

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 4 commits into from
Apr 8, 2025

Conversation

dmarticus
Copy link
Contributor

Problem

we're scaling the new /flags service and we want to do it intentionally. For starters, I want to start rolling out traffic to our customers on the us.posthog.com cloud instance – this way, if there's funky behavior, it just affects our customers, not our customers' customers.

Changes

The way I'm doing this is to basically hash the feature flag keys and then compare those values with a rollout percentage. This way we can guarantee that the same API keys see the same rollout value without hard-coding anything.

This rollout function also takes in Sets that can contain hashed values to explicitly include or exclude, which override the rollout percentage. The idea here is that if want to roll specific customers on or off, we can easily do that. I've done it for our public API key, for example (this isn't just an example though, I want to turn this on for us)

Does this work well for both Cloud and self-hosted?

should work the same for both

How did you test this code?

wrote tests for hashing function, and then

@dmarticus dmarticus requested a review from a team April 7, 2025 23:44
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 a hash-based rollout for PreviewFlagsV2, enabling controlled exposure by rolling out new flags to 1% of cloud customers.

  • Updated /frontend/src/loadPostHogJS.tsx to include a new PREVIEW_FLAGS_V2_CONFIG for toggling preview flags.
  • Introduced a stable hash-based rollout function in /frontend/src/lib/utils.tsx that respects inclusion/exclusion sets.
  • Added tests in /frontend/src/lib/utils.test.ts to confirm behavior under various rollout scenarios, including percentage-based rollouts and explicit overrides.

3 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile

excludedHashes?: Set<string>
}

function hashString(str: string): string {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just out of curiosity, why did you hand roll a hash function? The purpose was to hide the api keys. I’m not familiar enough with your implementation to understand if it’s one-way secure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to do something simple; but you're right, I should pick a tried-and-true implementation. I've updated it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ha! I'm allergic to hand-rolled crypto. 😛

Copy link
Contributor

github-actions bot commented Apr 7, 2025

Size Change: +1.54 MB (+15.14%) ⚠️

Total Size: 11.7 MB

Filename Size Change
frontend/dist/toolbar.js 11.7 MB +1.54 MB (+15.14%) ⚠️

compressed-size-action

@@ -25,6 +26,11 @@ const configWithSentry = (config: Partial<PostHogConfig>): Partial<PostHogConfig

export function loadPostHogJS(): void {
if (window.JS_POSTHOG_API_KEY) {
const PREVIEW_FLAGS_V2_CONFIG = {
rolloutPercentage: 1,
includedHashes: new Set(['593cb24f9928bab39ec383c06c908481880d5099']),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

thinking about this more... I almost feel like I should pass this value in as a list of environment variables or something, feels like a better idea than committing hashed tokens (even if they are one-way secure).

@haacked what do you think? I could go either way – environment variables is more work bc more plumbing but arguably more secure.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, it makes it easier if we have to back out, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yup you were right the whole time; I was just tired when I implemented this. After I got home and thought about it and read your comment I was like "huh I'm being an idiot"

Copy link
Contributor

Choose a reason for hiding this comment

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

You're too hard on yourself. 😆

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.

👍

@dmarticus dmarticus merged commit 5b365be into master Apr 8, 2025
110 checks passed
@dmarticus dmarticus deleted the chore/roll-flags-to-1-percent-of-cloud-customers branch April 8, 2025 19:50
dmarticus added a commit that referenced this pull request Apr 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants