Disable PostHog feature flags in prod.

This commit is contained in:
Sam Rose 2024-08-12 16:32:25 +01:00
parent b01c111567
commit b5465f1b63
No known key found for this signature in database
3 changed files with 23 additions and 3 deletions

View File

@ -145,6 +145,7 @@ const environment = {
COOKIE_DOMAIN: process.env.COOKIE_DOMAIN, COOKIE_DOMAIN: process.env.COOKIE_DOMAIN,
PLATFORM_URL: process.env.PLATFORM_URL || "", PLATFORM_URL: process.env.PLATFORM_URL || "",
POSTHOG_TOKEN: process.env.POSTHOG_TOKEN, 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", POSTHOG_API_HOST: process.env.POSTHOG_API_HOST || "https://us.i.posthog.com",
ENABLE_ANALYTICS: process.env.ENABLE_ANALYTICS, ENABLE_ANALYTICS: process.env.ENABLE_ANALYTICS,
TENANT_FEATURE_FLAGS: process.env.TENANT_FEATURE_FLAGS, TENANT_FEATURE_FLAGS: process.env.TENANT_FEATURE_FLAGS,

View File

@ -10,6 +10,7 @@ export function init(opts?: PostHogOptions) {
console.log("initializing posthog client...") console.log("initializing posthog client...")
posthog = new PostHog(env.POSTHOG_TOKEN, { posthog = new PostHog(env.POSTHOG_TOKEN, {
host: env.POSTHOG_API_HOST, host: env.POSTHOG_API_HOST,
personalApiKey: env.POSTHOG_PERSONAL_TOKEN,
...opts, ...opts,
}) })
} else { } else {
@ -186,10 +187,23 @@ export class FlagSet<V extends Flag<any>, T extends { [key: string]: V }> {
tags[`identity.tenantId`] = identity?.tenantId tags[`identity.tenantId`] = identity?.tenantId
tags[`identity._id`] = identity?._id 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 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)) console.log("posthog flags", JSON.stringify(posthogFlags))
for (const [name, value] of Object.entries(posthogFlags.featureFlags)) { for (const [name, value] of Object.entries(posthogFlags.featureFlags)) {

View File

@ -153,6 +153,7 @@ describe("feature flags", () => {
mockPosthogFlags(posthogFlags) mockPosthogFlags(posthogFlags)
env.POSTHOG_TOKEN = "test" env.POSTHOG_TOKEN = "test"
env.POSTHOG_API_HOST = "https://us.i.posthog.com" env.POSTHOG_API_HOST = "https://us.i.posthog.com"
env.POSTHOG_PERSONAL_TOKEN = "test"
} }
const ctx = { user: { license: { features: licenseFlags || [] } } } const ctx = { user: { license: { features: licenseFlags || [] } } }
@ -160,7 +161,11 @@ describe("feature flags", () => {
await withEnv(env, async () => { await withEnv(env, async () => {
// We need to pass in node-fetch here otherwise nock won't get used // We need to pass in node-fetch here otherwise nock won't get used
// because posthog-node uses axios under the hood. // because posthog-node uses axios under the hood.
init({ fetch: nodeFetch }) init({
fetch: (url, opts) => {
return nodeFetch(url, opts)
},
})
const fullIdentity: IdentityContext = { const fullIdentity: IdentityContext = {
_id: "us_1234", _id: "us_1234",