diff --git a/packages/core/src/analytics/posthogAiBetaServerClient/featureFlagEvaluation.ts b/packages/core/src/analytics/posthogAiBetaServerClient/featureFlagEvaluation.ts index e15d9d465..1acaa1dfd 100644 --- a/packages/core/src/analytics/posthogAiBetaServerClient/featureFlagEvaluation.ts +++ b/packages/core/src/analytics/posthogAiBetaServerClient/featureFlagEvaluation.ts @@ -3,14 +3,12 @@ import { kv } from "@vercel/kv"; import type { PostHog } from "posthog-node"; const KV_KEY = "posthog-feature-flag-local-evaluation"; -const ONE_DAY = 24 * 60 * 60 * 1000; -const ONE_MINUTE = 60 * 1000; const log = aiLogger("analytics:feature-flags"); const setKv = async (response: Response) => { const value = await response.text(); - await kv.set(KV_KEY, value, { ex: ONE_MINUTE }); + await kv.set(KV_KEY, value, { ex: 60 }); }; const getKv = async () => { @@ -38,6 +36,8 @@ export const cachedFetch: PostHog["fetch"] = async (url, options) => { const cachedResult = result.clone(); await setKv(cachedResult); log.info("evaluations cached to KV"); + } else { + log.error("failed to load evaluations", { status: result.status }); } return result; @@ -50,6 +50,8 @@ export const cachedFetch: PostHog["fetch"] = async (url, options) => { return await fetch(url, options); }; +const ONE_DAY = 24 * 60 * 60 * 1000; +const ONE_MINUTE = 60 * 1000; export const featureFlagsPollingInterval = // prevent polling timeout from stacking when HMR replaces posthogAiBetaServerClient process.env.NODE_ENV === "development" ? ONE_DAY : ONE_MINUTE; diff --git a/packages/core/src/analytics/posthogAiBetaServerClient/index.ts b/packages/core/src/analytics/posthogAiBetaServerClient/index.ts index 0597d2286..48a7d4d18 100644 --- a/packages/core/src/analytics/posthogAiBetaServerClient/index.ts +++ b/packages/core/src/analytics/posthogAiBetaServerClient/index.ts @@ -1,4 +1,5 @@ import { PostHog } from "posthog-node"; +import invariant from "tiny-invariant"; import { cachedFetch, @@ -7,7 +8,8 @@ import { const host = process.env.NEXT_PUBLIC_POSTHOG_HOST as string; const apiKey = process.env.NEXT_PUBLIC_POSTHOG_API_KEY || "*"; -const personalApiKey = process.env.POSTHOG_PERSONAL_KEY; +const personalApiKey = process.env.POSTHOG_PERSONAL_KEY_FLAGS; +invariant(personalApiKey, "POSTHOG_PERSONAL_KEY_FLAGS is required"); /** * This is the posthog nodejs client configured to send events to the @@ -15,7 +17,6 @@ const personalApiKey = process.env.POSTHOG_PERSONAL_KEY; */ export const posthogAiBetaServerClient = new PostHog(apiKey, { host, - personalApiKey, // We evaluate user feature flags on the server to prevent round-trips to posthog. // See https://posthog.com/docs/feature-flags/local-evaluation @@ -23,4 +24,5 @@ export const posthogAiBetaServerClient = new PostHog(apiKey, { // Instead we cache them in KV through a custom fetch implementation. fetch: cachedFetch, featureFlagsPollingInterval, + personalApiKey, }); diff --git a/turbo.json b/turbo.json index 77afe9df6..7bd629f0a 100644 --- a/turbo.json +++ b/turbo.json @@ -131,6 +131,7 @@ "STRICT_CSP", "TELEMETRY_ENABLED", "UPSTASH_*", - "WOLFRAM_CLIENT_SECRET" + "WOLFRAM_CLIENT_SECRET", + "POSTHOG_PERSONAL_KEY_FLAGS" ] }