// apps/web-next/instrumentation.ts
// Next.js convention: this module runs once on server boot (before any request handler).
// Initializes the OTel SDK here so PII scrub processors are active from the very first
// request — before bindAll() fires. Calling initOtelServerNode here (not inside bindAll)
// closes the startup window where @sentry/nextjs auto-instrumentation could send
// unscrubbed errors (C1 fix).

export async function register() {
  if (
    process.env["NEXT_RUNTIME"] === "nodejs" ||
    process.env["NEXT_RUNTIME"] === "edge"
  ) {
    const { initOtelServerNode } = await import(
      "@repo/core-shared/instrumentation/otel/init-server-node"
    );
    initOtelServerNode({
      dsn: process.env["WEB_NEXT_SENTRY_DSN"] ?? "",
      serviceName: "web-next",
      environment: process.env["NODE_ENV"] ?? "development",
    });
  }
}
