2021-04-13 16:35:26 +00:00
|
|
|
import * as Sentry from "@sentry/browser";
|
|
|
|
import { Integrations } from "@sentry/tracing";
|
|
|
|
import { VERSION } from "../constants";
|
|
|
|
import { SentryIgnoredError } from "../common/errors";
|
|
|
|
import { me } from "./Users";
|
2021-04-22 21:49:30 +00:00
|
|
|
import { config } from "./Config";
|
|
|
|
import { Config } from "authentik-api";
|
2021-04-13 16:35:26 +00:00
|
|
|
|
2021-06-06 12:51:43 +00:00
|
|
|
export const TAG_SENTRY_COMPONENT = "authentik.component";
|
2021-06-13 12:08:39 +00:00
|
|
|
export const TAG_SENTRY_CAPABILITIES = "authentik.capabilities";
|
2021-05-15 17:53:43 +00:00
|
|
|
|
2021-06-13 12:08:39 +00:00
|
|
|
export function configureSentry(canDoPpi: boolean = false): Promise<Config> {
|
2021-04-22 21:49:30 +00:00
|
|
|
return config().then((config) => {
|
2021-04-13 16:35:26 +00:00
|
|
|
if (config.errorReportingEnabled) {
|
|
|
|
Sentry.init({
|
|
|
|
dsn: "https://a579bb09306d4f8b8d8847c052d3a1d3@sentry.beryju.org/8",
|
|
|
|
release: `authentik@${VERSION}`,
|
2021-07-13 08:57:27 +00:00
|
|
|
tunnel: "/api/v2beta/sentry/",
|
2021-04-13 16:35:26 +00:00
|
|
|
integrations: [
|
|
|
|
new Integrations.BrowserTracing({
|
|
|
|
tracingOrigins: [window.location.host, "localhost"],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
tracesSampleRate: 0.6,
|
|
|
|
environment: config.errorReportingEnvironment,
|
2021-04-20 15:32:38 +00:00
|
|
|
beforeSend: async (event: Sentry.Event, hint: Sentry.EventHint): Promise<Sentry.Event | null> => {
|
2021-04-13 16:35:26 +00:00
|
|
|
if (hint.originalException instanceof SentryIgnoredError) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-06-08 21:10:17 +00:00
|
|
|
if ((hint.originalException as Error | undefined)?.hasOwnProperty("name")) {
|
|
|
|
if ((hint.originalException as Error | undefined)?.name == 'NetworkError') {
|
2021-04-27 13:54:57 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2021-04-20 15:32:38 +00:00
|
|
|
if (hint.originalException instanceof Response) {
|
2021-04-22 17:51:32 +00:00
|
|
|
const response = hint.originalException as Response;
|
|
|
|
// We only care about server errors
|
|
|
|
if (response.status < 500) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-07-09 17:06:30 +00:00
|
|
|
// Need to clone the response, otherwise the .text() and .json() can't be re-used
|
|
|
|
const resCopy = response.clone();
|
|
|
|
const body = await resCopy.json();
|
2021-04-22 17:51:32 +00:00
|
|
|
event.message = `${response.status} ${response.url}: ${JSON.stringify(body)}`
|
2021-04-20 15:32:38 +00:00
|
|
|
}
|
2021-04-13 16:35:26 +00:00
|
|
|
if (event.exception) {
|
|
|
|
me().then(user => {
|
|
|
|
Sentry.showReportDialog({
|
|
|
|
eventId: event.event_id,
|
|
|
|
user: {
|
|
|
|
email: user.user.email,
|
|
|
|
name: user.user.name,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return event;
|
|
|
|
},
|
|
|
|
});
|
2021-06-13 12:08:39 +00:00
|
|
|
Sentry.setTag(TAG_SENTRY_CAPABILITIES, config.capabilities.join(","));
|
2021-05-15 17:53:43 +00:00
|
|
|
if (window.location.pathname.includes("if/")) {
|
|
|
|
// Get the interface name from URL
|
|
|
|
const intf = window.location.pathname.replace(/.+if\/(.+)\//, "$1");
|
|
|
|
Sentry.setTag(TAG_SENTRY_COMPONENT, `web/${intf}`);
|
|
|
|
}
|
2021-04-13 16:35:26 +00:00
|
|
|
console.debug("authentik/config: Sentry enabled.");
|
2021-04-26 15:45:09 +00:00
|
|
|
if (config.errorReportingSendPii && canDoPpi) {
|
2021-04-13 16:35:26 +00:00
|
|
|
me().then(user => {
|
|
|
|
Sentry.setUser({ email: user.user.email });
|
|
|
|
console.debug("authentik/config: Sentry with PII enabled.");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
});
|
|
|
|
}
|