/** * Web OTel bootstrap (TASK-0135). * * Next.js calls `register()` ONCE per runtime. It runs in the Node.js * server runtime (the App Router's RSC/server-action/request path); the * edge runtime is NOT used by this app (the middleware is pure cookie * gating, no `export const runtime`), so a single guard is sufficient. * * Same init contract as the API/worker/realtime: `OTEL_ENABLED=true` + * `OTEL_EXPORTER_OTLP_ENDPOINT` → export spans/metrics to the collector; * otherwise every primitive in `@factory/observability` is a no-op (the * package's lazy instruments bind to nothing). OTEL_* are read from * process.env directly — the web app has no central env schema to extend. */ import { initOtel, shutdownOtel } from "@factory/observability"; export async function register(): Promise { if (typeof window !== "undefined") return; // browser bundle — never init here const otelEndpoint = process.env.OTEL_EXPORTER_OTLP_ENDPOINT; await initOtel({ serviceName: "factory-web", environment: process.env.NODE_ENV, ...(process.env.OTEL_ENABLED === "true" && otelEndpoint ? { endpoint: otelEndpoint } : {}) }); // Flush on graceful shutdown (Next dev: Ctrl-C / SIGTERM). const flush = (): void => { shutdownOtel().catch(() => undefined); }; process.on("SIGINT", flush); process.on("SIGTERM", flush); }