trails/apps/journal/server.ts
Ullrich Schäfer f070914362
feat(journal): per-request requestId propagated through logs
Every HTTP request now gets a requestId (inbound X-Request-Id header is
honored, otherwise a fresh UUID is minted) and the value is echoed on
the response. The server wraps the request in
\`requestContext.run({ requestId }, ...)\` — an AsyncLocalStorage scope —
so pino's \`mixin\` callback can read it on every log call without the
caller threading it through.

Net effect: \`logger.info({ ... }, \"db error\")\` from a loader, action,
or downstream lib now lands in JSON with a \`requestId\` field, making
cross-handler debugging trivial (\`grep requestId=abc-123\` returns the
full request trace).

Out of scope here:
- Planner gets the same treatment (separate, smaller PR after this lands).
- BRouter / Fedify outbound calls don't propagate the requestId yet —
  those are HTTP boundaries where we'd add it as a header, but the
  audit value was the in-process trace.

Tests:
- logger.server.test.ts (2 cases — als-bound info tags requestId; no
  context = no tag).

Full repo: pnpm typecheck, pnpm lint, pnpm test all green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 12:31:30 +02:00

180 lines
7 KiB
TypeScript

import * as Sentry from "@sentry/node";
import { nodeSentryConfig, drop404s } from "@trails-cool/sentry-config";
import { createRequestListener } from "@react-router/node";
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
import { createReadStream, statSync } from "node:fs";
import { join, extname, resolve } from "node:path";
import { logger, requestContext } from "./app/lib/logger.server.ts";
import { randomUUID } from "node:crypto";
import { httpRequestDuration, registry } from "./app/lib/metrics.server.ts";
import { createBoss, startWorker } from "@trails-cool/jobs";
import { getDatabaseUrl } from "@trails-cool/db";
import postgres from "postgres";
// Sentry DSN is read from env so self-hosted instances don't ship their
// errors to the trails.cool flagship Sentry by default. The flagship
// keeps its DSN as the fallback; setting SENTRY_DSN="" (or any other
// truthy value) overrides. SENTRY_DISABLED=true skips init entirely.
const FLAGSHIP_JOURNAL_SENTRY_DSN =
"https://a32ffcc575d34be072e91b20f247eeee@o4509530546634752.ingest.de.sentry.io/4509530555547728";
const sentryDsn = process.env.SENTRY_DSN ?? FLAGSHIP_JOURNAL_SENTRY_DSN;
if (process.env.SENTRY_DISABLED !== "true" && sentryDsn !== "") {
Sentry.init({
dsn: sentryDsn,
...nodeSentryConfig("journal server"),
beforeSend: drop404s,
});
}
const port = Number(process.env.PORT ?? 3000);
const CLIENT_DIR = resolve(import.meta.dirname, "build", "client");
const MIME: Record<string, string> = {
".js": "application/javascript",
".css": "text/css",
".html": "text/html",
".json": "application/json",
".png": "image/png",
".jpg": "image/jpeg",
".svg": "image/svg+xml",
".ico": "image/x-icon",
".woff": "font/woff",
".woff2": "font/woff2",
};
function serveStatic(req: IncomingMessage, res: ServerResponse): boolean {
if (req.method !== "GET" && req.method !== "HEAD") return false;
const url = new URL(req.url ?? "/", `http://${req.headers.host}`);
const filePath = resolve(join(CLIENT_DIR, url.pathname));
if (!filePath.startsWith(CLIENT_DIR)) return false;
try {
if (!statSync(filePath).isFile()) return false;
} catch {
return false;
}
res.setHeader("Content-Type", MIME[extname(filePath)] ?? "application/octet-stream");
if (url.pathname.startsWith("/assets/")) {
res.setHeader("Cache-Control", "public, immutable, max-age=31536000");
}
createReadStream(filePath).pipe(res);
return true;
}
const listener = createRequestListener({
build: () => import("./build/server/index.js" as string) as never,
});
async function handleMetrics(_req: IncomingMessage, res: ServerResponse): Promise<void> {
const metrics = await registry.metrics();
res.writeHead(200, { "Content-Type": registry.contentType });
res.end(metrics);
}
const version = process.env.SENTRY_RELEASE ?? "dev";
async function handleHealth(_req: IncomingMessage, res: ServerResponse): Promise<void> {
const client = postgres(getDatabaseUrl(), { max: 1 });
try {
await client`SELECT 1`;
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ status: "ok", version, db: "connected" }));
} catch {
res.writeHead(503, { "Content-Type": "application/json" });
res.end(JSON.stringify({ status: "degraded", version, db: "unreachable" }));
} finally {
await client.end();
}
}
const server = createServer((req, res) => {
const url = req.url ?? "/";
const start = Date.now();
// Honor an inbound X-Request-Id header (e.g. from Caddy or a probe)
// so request IDs propagate across the proxy hop. Mint a fresh one if
// absent. Echo on the response so clients can correlate.
const inbound = req.headers["x-request-id"];
const requestId =
(Array.isArray(inbound) ? inbound[0] : inbound) || randomUUID();
res.setHeader("X-Request-Id", requestId);
requestContext.run({ requestId }, () => {
if (!url.startsWith("/assets/") && url !== "/api/health" && url !== "/api/metrics") {
res.on("finish", () => {
const duration = Date.now() - start;
logger.info({ method: req.method, path: url, status: res.statusCode, duration }, "request");
httpRequestDuration.observe(
{ method: req.method ?? "GET", route: url.split("?")[0]!, status: String(res.statusCode) },
duration / 1000,
);
});
}
if (url === "/api/health") { handleHealth(req, res); return; }
if (url === "/api/metrics") { handleMetrics(req, res); return; }
if (!serveStatic(req, res)) {
listener(req, res);
}
});
});
server.listen(port, async () => {
logger.info({ port }, "Journal server listening");
// Seed first-party OAuth2 clients
const { seedOAuthClient } = await import("./app/lib/oauth.server.ts");
await seedOAuthClient("trails-cool-mobile", "trailscool://auth/callback", true);
// Pre-flight the demo user so a persona username that clashes with a
// real account blocks job scheduling rather than silently attaching
// synthetic rows to a human.
let enableDemoJobs = false;
if (process.env.DEMO_BOT_ENABLED === "true") {
const { ensureDemoUser, DemoPersonaUsernameClashError } = await import(
"./app/lib/demo-bot.server.ts"
);
try {
const id = await ensureDemoUser();
logger.info({ id }, "demo-bot user ensured");
enableDemoJobs = true;
} catch (err) {
if (err instanceof DemoPersonaUsernameClashError) {
logger.error(
{ username: err.username },
"demo persona username clash — demo jobs disabled for this process",
);
} else {
logger.error({ err }, "demo-bot ensureDemoUser failed");
}
}
}
// Start background job worker
const jobs: Parameters<typeof startWorker>[1] = [];
if (enableDemoJobs) {
const { demoBotGenerateJob } = await import("./app/jobs/demo-bot-generate.ts");
const { demoBotPruneJob } = await import("./app/jobs/demo-bot-prune.ts");
jobs.push(demoBotGenerateJob, demoBotPruneJob);
}
// Notifications jobs always run (no feature flag): a journal without
// notifications wired up would silently drop fan-out enqueues.
const { notificationsFanoutJob } = await import("./app/jobs/notifications-fanout.ts");
const { notificationsPurgeJob } = await import("./app/jobs/notifications-purge.ts");
const { komootBulkImportJob } = await import("./app/jobs/komoot-bulk-import.ts");
const { importBatchesSweepJob } = await import("./app/jobs/import-batches-sweep.ts");
const { sendWelcomeEmailJob } = await import("./app/jobs/send-welcome-email.ts");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
jobs.push(notificationsFanoutJob, notificationsPurgeJob, komootBulkImportJob as any, importBatchesSweepJob, sendWelcomeEmailJob);
const boss = createBoss(getDatabaseUrl());
await startWorker(boss, jobs);
// Register the started boss so feature code can enqueue jobs against
// the same instance via getBoss() / enqueueOptional().
const { setBoss } = await import("./app/lib/boss.server.ts");
setBoss(boss);
logger.info("Background job worker started");
});