Files
authentik/web/src/common/sentry/apply.ts
Teffen Ellis 3fb3e3e0a3 web: Fix Sentry Initialization (#25450)
web: let the error-reporting setting govern Sentry in production

`init({ enabled: process.env.NODE_ENV !== "production" })` disabled the
SDK in exactly the builds that ship: the container's web stage sets
`NODE_ENV=production` (lifecycle/container/Dockerfile), and the bundler
inlines it. An administrator who turned error reporting on in their
config therefore received nothing, while `browserTracingIntegration` was
registered only in production — the opposite polarity — so development
had the SDK on with no tracing integration behind it.

Consolidates the decision into `sentryEnabled(cfg, debug)`:

- The administrator's `errorReporting.enabled` decides, in every
  environment; `CanDebug` still forces it on.
- Development additionally honors `?disable-sentry`, so a noisy local
  session can opt out for one load without a rebuild.
- The tracing integration is registered whenever the SDK runs. It has
  always been configured with the automatic instrumentation off, since
  the router opens the spans itself.

`enabled: true` stays on the `init` call so the decision is readable off
the client, which is how the router outlets gate their spans.

Tidy.
2026-08-25 16:22:56 +01:00

63 lines
2.0 KiB
TypeScript

/**
* @file Initializes Sentry as an import side effect.
*
* Imported first from each interface entrypoint so reporting is live before the
* element modules evaluate and custom elements register — errors thrown during
* that window used to escape, because initialization ran in an element
* constructor.
*
* The enable/disable policy is {@linkcode isSentryEnabled}, which is a pure
* function so it can be tested without a browser.
*/
import { globalAK } from "#common/global";
import {
DEFAULT_SENTRY_BROWSER_OPTIONS,
isSentryEnabled,
setSentryCapabilities,
setSentryInterface,
} from "#common/sentry/utils";
import { readInterfaceRouteParam } from "#elements/router/utils";
import { ConsoleLogger } from "#logger/browser";
import { CapabilitiesEnum } from "@goauthentik/api";
import { browserTracingIntegration, init, spotlightBrowserIntegration } from "@sentry/browser";
import { type Integration } from "@sentry/core/browser";
const { errorReporting, capabilities } = globalAK().config;
const debug = capabilities.includes(CapabilitiesEnum.CanDebug);
if (isSentryEnabled({ errorReporting, debug, search: window.location.search })) {
const logger = ConsoleLogger.prefix("sentry");
const integrations: Integration[] = [
browserTracingIntegration({
// https://docs.sentry.io/platforms/javascript/tracing/instrumentation/automatic-instrumentation/#custom-routing
instrumentNavigation: false,
instrumentPageLoad: false,
traceFetch: false,
}),
];
if (debug) {
logger.debug("Enabled Spotlight");
integrations.push(spotlightBrowserIntegration());
}
init({
...DEFAULT_SENTRY_BROWSER_OPTIONS,
integrations,
tracePropagationTargets: [window.location.origin],
dsn: errorReporting?.sentryDsn,
tracesSampleRate: debug ? 1.0 : errorReporting?.tracesSampleRate,
environment: errorReporting?.environment,
});
setSentryCapabilities(capabilities);
setSentryInterface(readInterfaceRouteParam());
}