Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/links/src/routes/redirect.route.test.ts
Original file line number Diff line number Diff line change
@@ -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: () => ({
Expand All @@ -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" } },
Expand Down Expand Up @@ -78,6 +80,7 @@ mock.module("../lib/logging", () => ({
}));

mock.module("../lib/link-visit-delivery", () => ({
...linkVisitDeliveryModule,
enqueueLinkVisit,
}));

Expand Down
32 changes: 32 additions & 0 deletions packages/shared/src/evlog-redaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand All @@ -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");
Expand Down
8 changes: 6 additions & 2 deletions packages/shared/src/evlog-redaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -57,6 +60,7 @@ export function resolveEvlogEnvironment(
env.APP_ENV?.trim() ||
env.RAILWAY_ENVIRONMENT_NAME?.trim() ||
env.VERCEL_ENV?.trim() ||
Comment on lines 60 to 62

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Unkey overrides existing platform metadata

If a runtime contains both UNKEY_ENVIRONMENT_SLUG and Railway or Vercel metadata, the new ordering selects the Unkey slug first, causing logs to be attributed to the wrong deployment environment.

Suggested change
env.APP_ENV?.trim() ||
env.UNKEY_ENVIRONMENT_SLUG?.trim() ||
env.RAILWAY_ENVIRONMENT_NAME?.trim() ||
env.VERCEL_ENV?.trim() ||
env.APP_ENV?.trim() ||
env.RAILWAY_ENVIRONMENT_NAME?.trim() ||
env.VERCEL_ENV?.trim() ||
env.UNKEY_ENVIRONMENT_SLUG?.trim() ||

env.UNKEY_ENVIRONMENT_SLUG?.trim() ||
(env.NODE_ENV === "development" || env.NODE_ENV === "test"
? env.NODE_ENV
: "production")
Expand All @@ -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,
};
}

Expand Down
Loading