Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -15,7 +15,8 @@
|
||||
*/
|
||||
import * as Sentry from "@sentry/node";
|
||||
|
||||
import { createNoopErrorReporter, type ErrorReportEvent, type ErrorReportSeverity, type ErrorReporter } from "@hivegrid/error-reporter";
|
||||
import { createNoopErrorReporter, sanitizeSentryEvent, type ErrorReportEvent, type ErrorReportSeverity, type ErrorReporter } from "@hivegrid/error-reporter";
|
||||
import { getWebServiceName } from "../build-surface";
|
||||
|
||||
type SentrySeverityLevel = "fatal" | "error" | "warning" | "log" | "info" | "debug";
|
||||
|
||||
@@ -32,6 +33,7 @@ export interface ServerSentryConfig {
|
||||
environment: string | undefined;
|
||||
release: string | undefined;
|
||||
tracesSampleRate: number;
|
||||
service: "hivegrid-web" | "hivegrid-admin" | "hivegrid-portal" | undefined;
|
||||
}
|
||||
|
||||
export function parseServerSampleRate(raw: string | undefined, fallback: number): number {
|
||||
@@ -44,15 +46,28 @@ export function parseServerSampleRate(raw: string | undefined, fallback: number)
|
||||
}
|
||||
|
||||
export function readServerSentryConfig(env: NodeJS.ProcessEnv = process.env): ServerSentryConfig {
|
||||
const dsn = env.SENTRY_DSN_WEB || undefined;
|
||||
return {
|
||||
dsn: env.SENTRY_DSN_WEB || undefined,
|
||||
dsn,
|
||||
environment: env.SENTRY_ENVIRONMENT || env.NODE_ENV || undefined,
|
||||
release: env.SENTRY_RELEASE || undefined,
|
||||
release: env.SENTRY_RELEASE || env.APP_REVISION || undefined,
|
||||
tracesSampleRate: parseServerSampleRate(env.SENTRY_TRACES_SAMPLE_RATE, 0.1),
|
||||
service: dsn ? requireServerSentryService(env) : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
export function requireServerSentryService(
|
||||
env: Readonly<{ WEB_RUNTIME_SURFACE?: string }> = process.env
|
||||
): "hivegrid-web" | "hivegrid-admin" | "hivegrid-portal" {
|
||||
const surface = env.WEB_RUNTIME_SURFACE;
|
||||
if (surface !== "core" && surface !== "admin" && surface !== "portal") {
|
||||
throw new Error("WEB_RUNTIME_SURFACE must be explicitly set to core, admin, or portal before server Sentry initialization");
|
||||
}
|
||||
return getWebServiceName(surface);
|
||||
}
|
||||
|
||||
let initialized = false;
|
||||
let initializedService: ServerSentryConfig["service"];
|
||||
|
||||
/**
|
||||
* Initializes the Sentry Node SDK for SSR. Idempotent — returns `true`
|
||||
@@ -62,12 +77,18 @@ let initialized = false;
|
||||
export function initSentryServer(config: ServerSentryConfig = readServerSentryConfig()): boolean {
|
||||
if (initialized) return true;
|
||||
if (!config.dsn) return false;
|
||||
if (!config.service) {
|
||||
throw new Error("Server Sentry service identity is required when SENTRY_DSN_WEB is configured");
|
||||
}
|
||||
|
||||
Sentry.init({
|
||||
dsn: config.dsn,
|
||||
environment: config.environment,
|
||||
release: config.release,
|
||||
tracesSampleRate: config.tracesSampleRate,
|
||||
initialScope: {
|
||||
tags: { service: config.service },
|
||||
},
|
||||
// PII strategy: NO IP, NO email — only the user-id is retained when
|
||||
// a downstream loader calls `Sentry.setUser({ id })`.
|
||||
sendDefaultPii: false,
|
||||
@@ -84,9 +105,13 @@ export function initSentryServer(config: ServerSentryConfig = readServerSentryCo
|
||||
},
|
||||
}),
|
||||
],
|
||||
beforeSend(event) {
|
||||
return sanitizeSentryEvent(event as unknown as Record<string, unknown>) as typeof event | null;
|
||||
},
|
||||
});
|
||||
|
||||
initialized = true;
|
||||
initializedService = config.service;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -107,8 +132,9 @@ function buildWebErrorReporter(): ErrorReporter {
|
||||
const tags: Record<string, string | undefined> = {
|
||||
component: event.component,
|
||||
operation: event.operation,
|
||||
service: initializedService,
|
||||
};
|
||||
const sentryLevel: SentrySeverityLevel = event.severity ? SEVERITY_TO_SENTRY_LEVEL[event.severity] ?? "error" : "error";
|
||||
const sentryLevel: SentrySeverityLevel = event.severity ? (SEVERITY_TO_SENTRY_LEVEL[event.severity] ?? "error") : "error";
|
||||
const captureContext = {
|
||||
level: sentryLevel,
|
||||
extra: event as Record<string, unknown>,
|
||||
@@ -133,4 +159,5 @@ export const webErrorReporter: ErrorReporter = buildWebErrorReporter();
|
||||
/** Test-only: reset the init guard for unit tests. */
|
||||
export function __resetSentryServerForTests(): void {
|
||||
initialized = false;
|
||||
initializedService = undefined;
|
||||
}
|
||||
Reference in new issue
Block a user