From 0fc683975107dd5ebed871291d7bc385a4de2a30 Mon Sep 17 00:00:00 2001 From: Konstantin Wohlwend Date: Thu, 27 Feb 2025 22:17:53 -0800 Subject: [PATCH] Skip event logs in CI --- apps/backend/src/lib/events.tsx | 43 ++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/apps/backend/src/lib/events.tsx b/apps/backend/src/lib/events.tsx index 01879d44d..cf382d690 100644 --- a/apps/backend/src/lib/events.tsx +++ b/apps/backend/src/lib/events.tsx @@ -2,6 +2,7 @@ import withPostHog from "@/analytics"; import { prismaClient } from "@/prisma-client"; import { runAsynchronouslyAndWaitUntil } from "@/utils/vercel"; import { urlSchema, yupMixed, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields"; +import { getEnvVariable, getNodeEnvironment } from "@stackframe/stack-shared/dist/utils/env"; import { StackAssertionError, throwErr } from "@stackframe/stack-shared/dist/utils/errors"; import { HTTP_METHODS } from "@stackframe/stack-shared/dist/utils/http"; import { filterUndefined, typedKeys } from "@stackframe/stack-shared/dist/utils/objects"; @@ -165,25 +166,27 @@ export async function logEvent( }); // log event in PostHog - await withPostHog(async posthog => { - const distinctId = typeof data === "object" && data && "userId" in data ? (data.userId as string) : `backend-anon-${generateUuid()}`; - for (const eventType of allEventTypes) { - const postHogEventName = `stack_${eventType.id.replace(/^\$/, "system_").replace(/-/g, "_")}`; - posthog.capture({ - event: postHogEventName, - distinctId, - groups: filterUndefined({ - projectId: typeof data === "object" && data && "projectId" in data ? (typeof data.projectId === "string" ? data.projectId : throwErr("Project ID is not a string for some reason?", { data })) : undefined, - }), - timestamp: timeRange.end, - properties: { - data, - is_wide: isWide, - event_started_at: timeRange.start, - event_ended_at: timeRange.end, - }, - }); - } - }); + if (getNodeEnvironment().includes("production") && !getEnvVariable("CI", "")) { + await withPostHog(async posthog => { + const distinctId = typeof data === "object" && data && "userId" in data ? (data.userId as string) : `backend-anon-${generateUuid()}`; + for (const eventType of allEventTypes) { + const postHogEventName = `stack_${eventType.id.replace(/^\$/, "system_").replace(/-/g, "_")}`; + posthog.capture({ + event: postHogEventName, + distinctId, + groups: filterUndefined({ + projectId: typeof data === "object" && data && "projectId" in data ? (typeof data.projectId === "string" ? data.projectId : throwErr("Project ID is not a string for some reason?", { data })) : undefined, + }), + timestamp: timeRange.end, + properties: { + data, + is_wide: isWide, + event_started_at: timeRange.start, + event_ended_at: timeRange.end, + }, + }); + } + }); + } })()); }