Skip to content

Commit

Permalink
Disable PostHog feature flags in prod.
Browse files Browse the repository at this point in the history
  • Loading branch information
samwho committed Aug 12, 2024
1 parent b01c111 commit b5465f1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/backend-core/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ const environment = {
COOKIE_DOMAIN: process.env.COOKIE_DOMAIN,
PLATFORM_URL: process.env.PLATFORM_URL || "",
POSTHOG_TOKEN: process.env.POSTHOG_TOKEN,
POSTHOG_PERSONAL_TOKEN: process.env.POSTHOG_PERSONAL_TOKEN,
POSTHOG_API_HOST: process.env.POSTHOG_API_HOST || "https://us.i.posthog.com",
ENABLE_ANALYTICS: process.env.ENABLE_ANALYTICS,
TENANT_FEATURE_FLAGS: process.env.TENANT_FEATURE_FLAGS,
Expand Down
18 changes: 16 additions & 2 deletions packages/backend-core/src/features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function init(opts?: PostHogOptions) {
console.log("initializing posthog client...")
posthog = new PostHog(env.POSTHOG_TOKEN, {
host: env.POSTHOG_API_HOST,
personalApiKey: env.POSTHOG_PERSONAL_TOKEN,
...opts,
})
} else {
Expand Down Expand Up @@ -186,10 +187,23 @@ export class FlagSet<V extends Flag<any>, T extends { [key: string]: V }> {
tags[`identity.tenantId`] = identity?.tenantId
tags[`identity._id`] = identity?._id

if (posthog && identity?.type === IdentityType.USER) {
// Until we're confident this performs well, we're only enabling it in QA
// and test environments.
const usePosthog = env.isTest() || env.isQA()
if (usePosthog && posthog && identity?.type === IdentityType.USER) {
tags[`readFromPostHog`] = true

const posthogFlags = await posthog.getAllFlagsAndPayloads(identity._id)
const personProperties: Record<string, string> = {}
if (identity.tenantId) {
personProperties.tenantId = identity.tenantId
}

const posthogFlags = await posthog.getAllFlagsAndPayloads(
identity._id,
{
personProperties,
}
)
console.log("posthog flags", JSON.stringify(posthogFlags))

for (const [name, value] of Object.entries(posthogFlags.featureFlags)) {
Expand Down
7 changes: 6 additions & 1 deletion packages/backend-core/src/features/tests/features.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,19 @@ describe("feature flags", () => {
mockPosthogFlags(posthogFlags)
env.POSTHOG_TOKEN = "test"
env.POSTHOG_API_HOST = "https://us.i.posthog.com"
env.POSTHOG_PERSONAL_TOKEN = "test"
}

const ctx = { user: { license: { features: licenseFlags || [] } } }

await withEnv(env, async () => {
// We need to pass in node-fetch here otherwise nock won't get used
// because posthog-node uses axios under the hood.
init({ fetch: nodeFetch })
init({
fetch: (url, opts) => {
return nodeFetch(url, opts)
},
})

const fullIdentity: IdentityContext = {
_id: "us_1234",
Expand Down

0 comments on commit b5465f1

Please sign in to comment.