peppa/apps/web/instrumentation.ts
Асель 6e54fc69a6 PEP-91: Peppa reports its own errors to Bugsink — backend to PEP-api, browser to PEP-web
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:39:46 +05:00

30 lines
1.6 KiB
TypeScript

// Runs once when the Next server starts: the web's heartbeat for service monitoring (PEP-20)
// and the runner-client mode (PEP-21): the web enqueues runner commands, the watchdog runs them.
export async function register() {
if (process.env.NEXT_RUNTIME !== "nodejs") return;
const { beat, installCrashReporting } = await import("@peppa/core");
const { db } = await import("@/lib/db");
// PEP-91: a crash of the web goes to Bugsink before launchd restarts it.
installCrashReporting(db, "web");
const { setRunnerClient } = await import("@peppa/runner");
setRunnerClient(true, db);
const version = process.env.NEXT_PUBLIC_PEPPA_VERSION;
const tick = () => beat(db, "web", { version }).catch((e) => console.error(`heartbeat: ${(e as Error).message}`));
void tick();
setInterval(tick, 60_000).unref();
}
/** PEP-91: an error while rendering or handling a request → Bugsink (PEP-api). */
export async function onRequestError(err: unknown, request: { path: string; method: string; headers: Record<string, string | string[] | undefined> }, context: { routerKind: string; routePath: string; routeType: string }) {
if (process.env.NEXT_RUNTIME !== "nodejs") return;
const { reportPlatformError } = await import("@peppa/core");
const { db } = await import("@/lib/db");
const ua = request.headers["user-agent"];
await reportPlatformError(db, err, {
service: "web",
where: `${request.method} ${context.routePath}`,
request: { url: request.path, method: request.method, userAgent: typeof ua === "string" ? ua : undefined },
tags: { route_type: context.routeType },
});
}