diff --git a/apps/links/src/routes/redirect.route.test.ts b/apps/links/src/routes/redirect.route.test.ts index 57344b01d..0639fb04e 100644 --- a/apps/links/src/routes/redirect.route.test.ts +++ b/apps/links/src/routes/redirect.route.test.ts @@ -1,5 +1,6 @@ import { beforeEach, describe, expect, mock, test } from "bun:test"; import { Elysia } from "elysia"; +import * as actualLinkVisitDelivery from "../lib/link-visit-delivery"; const dbSelect = mock(() => ({ from: () => ({ @@ -12,6 +13,7 @@ const getCachedLink = mock(); const ratelimit = mock(); const resolveDeepLink = mock(); const enqueueLinkVisit = mock(); +const linkVisitDeliveryModule = { ...actualLinkVisitDelivery }; mock.module("@databuddy/env/app", () => ({ config: { urls: { dashboard: "https://dashboard.test" } }, @@ -78,6 +80,7 @@ mock.module("../lib/logging", () => ({ })); mock.module("../lib/link-visit-delivery", () => ({ + ...linkVisitDeliveryModule, enqueueLinkVisit, })); diff --git a/packages/shared/src/evlog-redaction.test.ts b/packages/shared/src/evlog-redaction.test.ts index 5f60e16b8..53d381f42 100644 --- a/packages/shared/src/evlog-redaction.test.ts +++ b/packages/shared/src/evlog-redaction.test.ts @@ -40,6 +40,22 @@ describe("databuddy evlog redaction", () => { expect(resolveEvlogEnvironment({})).toBe("production"); }); + it("preserves established platform precedence over Unkey", () => { + expect( + resolveEvlogEnvironment({ + RAILWAY_ENVIRONMENT_NAME: "staging", + UNKEY_ENVIRONMENT_SLUG: "unkey-preview", + VERCEL_ENV: "preview", + }) + ).toBe("staging"); + expect( + resolveEvlogEnvironment({ + UNKEY_ENVIRONMENT_SLUG: "unkey-preview", + VERCEL_ENV: "preview", + }) + ).toBe("preview"); + }); + it("adds deployment metadata to every service environment", () => { expect( createDatabuddyEvlogEnv("api", { @@ -55,6 +71,22 @@ describe("databuddy evlog redaction", () => { }); }); + it("uses Unkey deployment context outside Railway", () => { + expect( + createDatabuddyEvlogEnv("links", { + NODE_ENV: "production", + UNKEY_ENVIRONMENT_SLUG: "preview", + UNKEY_GIT_COMMIT_SHA: "def456", + UNKEY_REGION: "aws::eu-central-1", + }) + ).toEqual({ + service: "links", + environment: "preview", + region: "aws::eu-central-1", + commitHash: "def456", + }); + }); + it("covers sensitive field names used across services", () => { expect(databuddyEvlogRedactConfig.paths).toContain("headers.authorization"); expect(databuddyEvlogRedactConfig.paths).toContain("headers.cookie"); diff --git a/packages/shared/src/evlog-redaction.ts b/packages/shared/src/evlog-redaction.ts index 3ef4586da..dc1fdf552 100644 --- a/packages/shared/src/evlog-redaction.ts +++ b/packages/shared/src/evlog-redaction.ts @@ -47,6 +47,9 @@ interface EvlogRuntimeEnv { RAILWAY_ENVIRONMENT_NAME?: string; RAILWAY_GIT_COMMIT_SHA?: string; RAILWAY_REPLICA_REGION?: string; + UNKEY_ENVIRONMENT_SLUG?: string; + UNKEY_GIT_COMMIT_SHA?: string; + UNKEY_REGION?: string; VERCEL_ENV?: string; } @@ -57,6 +60,7 @@ export function resolveEvlogEnvironment( env.APP_ENV?.trim() || env.RAILWAY_ENVIRONMENT_NAME?.trim() || env.VERCEL_ENV?.trim() || + env.UNKEY_ENVIRONMENT_SLUG?.trim() || (env.NODE_ENV === "development" || env.NODE_ENV === "test" ? env.NODE_ENV : "production") @@ -70,8 +74,8 @@ export function createDatabuddyEvlogEnv( return { service, environment: resolveEvlogEnvironment(env), - region: env.RAILWAY_REPLICA_REGION, - commitHash: env.RAILWAY_GIT_COMMIT_SHA, + region: env.RAILWAY_REPLICA_REGION ?? env.UNKEY_REGION, + commitHash: env.RAILWAY_GIT_COMMIT_SHA ?? env.UNKEY_GIT_COMMIT_SHA, }; }