chore: Upgrade Sentry in v1, v2 & web (#15396)
* chore: Upgrade Sentry in v1, v2 & web * fix: Keep ./instrument to be the first line * Remove eslint-plugin-import (fixed in prettier) * Optimistic removal pending some feedback * Simplified and fixed sentry wrapper * Create instrumentation.ts also in APIv1 * sentry key in main nextConfig is deprecated
This commit is contained in:
1 parent
0cc7a9fc22
commit
1e0f7f433f
18 files changed
+1405
-384
No files matched your search
@@ -0,0 +1,17 @@
|
||||
import * as Sentry from "@sentry/nextjs";
|
||||
|
||||
export function register() {
|
||||
if (process.env.NEXT_RUNTIME === "nodejs") {
|
||||
Sentry.init({
|
||||
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
|
||||
// reduce sample rate to 10% on production
|
||||
tracesSampleRate: process.env.NODE_ENV !== "production" ? 1.0 : 0.1,
|
||||
});
|
||||
}
|
||||
|
||||
if (process.env.NEXT_RUNTIME === "edge") {
|
||||
Sentry.init({
|
||||
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,12 @@ const { withAxiom } = require("next-axiom");
|
||||
const { withSentryConfig } = require("@sentry/nextjs");
|
||||
|
||||
const plugins = [withAxiom];
|
||||
|
||||
/** @type {import("next").NextConfig} */
|
||||
const nextConfig = {
|
||||
experimental: {
|
||||
instrumentationHook: true,
|
||||
},
|
||||
transpilePackages: [
|
||||
"@calcom/app-store",
|
||||
"@calcom/core",
|
||||
@@ -87,12 +92,12 @@ const nextConfig = {
|
||||
};
|
||||
|
||||
if (!!process.env.NEXT_PUBLIC_SENTRY_DSN) {
|
||||
nextConfig["sentry"] = {
|
||||
autoInstrumentServerFunctions: true,
|
||||
hideSourceMaps: true,
|
||||
};
|
||||
|
||||
plugins.push(withSentryConfig);
|
||||
plugins.push((nextConfig) =>
|
||||
withSentryConfig(nextConfig, {
|
||||
autoInstrumentServerFunctions: true,
|
||||
hideSourceMaps: true,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = () => plugins.reduce((acc, next) => next(acc), nextConfig);
|
||||
@@ -30,7 +30,7 @@
|
||||
"@calcom/lib": "*",
|
||||
"@calcom/prisma": "*",
|
||||
"@calcom/trpc": "*",
|
||||
"@sentry/nextjs": "^7.73.0",
|
||||
"@sentry/nextjs": "^8.8.0",
|
||||
"bcryptjs": "^2.4.3",
|
||||
"memory-cache": "^0.2.0",
|
||||
"next": "^13.5.4",
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
import { init as SentryInit } from "@sentry/nextjs";
|
||||
|
||||
SentryInit({
|
||||
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
|
||||
});
|
||||
@@ -1,6 +0,0 @@
|
||||
import { init as SentryInit } from "@sentry/nextjs";
|
||||
|
||||
SentryInit({
|
||||
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
|
||||
tracesSampleRate: 1.0,
|
||||
});
|
||||
@@ -2,5 +2,6 @@ const rootConfig = require("../../../packages/config/prettier-preset");
|
||||
|
||||
module.exports = {
|
||||
...rootConfig,
|
||||
importOrder: ["^./instrument", ...rootConfig.importOrder],
|
||||
importOrderParserPlugins: ["typescript", "decorators-legacy"],
|
||||
};
|
||||
@@ -40,8 +40,7 @@
|
||||
"@nestjs/platform-express": "^10.0.0",
|
||||
"@nestjs/swagger": "^7.3.0",
|
||||
"@nestjs/throttler": "^5.1.2",
|
||||
"@sentry/node": "^7.86.0",
|
||||
"@sentry/tracing": "^7.86.0",
|
||||
"@sentry/node": "^8.8.0",
|
||||
"body-parser": "^1.20.2",
|
||||
"class-transformer": "^0.5.1",
|
||||
"class-validator": "^0.14.0",
|
||||
|
||||
+6
-10
@@ -1,11 +1,11 @@
|
||||
import { getEnv } from "@/env";
|
||||
import "./instrument";
|
||||
|
||||
import { HttpExceptionFilter } from "@/filters/http-exception.filter";
|
||||
import { PrismaExceptionFilter } from "@/filters/prisma-exception.filter";
|
||||
import { SentryFilter } from "@/filters/sentry-exception.filter";
|
||||
import { ZodExceptionFilter } from "@/filters/zod-exception.filter";
|
||||
import type { ValidationError } from "@nestjs/common";
|
||||
import { BadRequestException, ValidationPipe, VersioningType } from "@nestjs/common";
|
||||
import { HttpAdapterHost } from "@nestjs/core";
|
||||
import { BaseExceptionFilter, HttpAdapterHost } from "@nestjs/core";
|
||||
import type { NestExpressApplication } from "@nestjs/platform-express";
|
||||
import * as Sentry from "@sentry/node";
|
||||
import * as cookieParser from "cookie-parser";
|
||||
@@ -71,15 +71,11 @@ export const bootstrap = (app: NestExpressApplication): NestExpressApplication =
|
||||
})
|
||||
);
|
||||
|
||||
if (process.env.SENTRY_DSN) {
|
||||
Sentry.init({
|
||||
dsn: getEnv("SENTRY_DSN"),
|
||||
});
|
||||
}
|
||||
|
||||
// Exception filters, new filters go at the bottom, keep the order
|
||||
const { httpAdapter } = app.get(HttpAdapterHost);
|
||||
app.useGlobalFilters(new SentryFilter(httpAdapter));
|
||||
if (process.env.SENTRY_DSN) {
|
||||
Sentry.setupNestErrorHandler(app, new BaseExceptionFilter(httpAdapter));
|
||||
}
|
||||
app.useGlobalFilters(new PrismaExceptionFilter());
|
||||
app.useGlobalFilters(new ZodExceptionFilter());
|
||||
app.useGlobalFilters(new HttpExceptionFilter());
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
import { ArgumentsHost, Catch, Logger, HttpStatus } from "@nestjs/common";
|
||||
import { BaseExceptionFilter } from "@nestjs/core";
|
||||
import * as Sentry from "@sentry/node";
|
||||
import { Request } from "express";
|
||||
|
||||
import { ERROR_STATUS, INTERNAL_SERVER_ERROR } from "@calcom/platform-constants";
|
||||
import { Response } from "@calcom/platform-types";
|
||||
|
||||
@Catch()
|
||||
export class SentryFilter extends BaseExceptionFilter {
|
||||
private readonly logger = new Logger("SentryExceptionFilter");
|
||||
|
||||
handleUnknownError(exception: any, host: ArgumentsHost): void {
|
||||
const ctx = host.switchToHttp();
|
||||
const response = ctx.getResponse<Response>();
|
||||
const request = ctx.getRequest<Request>();
|
||||
const requestId = request.headers["X-Request-Id"];
|
||||
|
||||
this.logger.error(`Sentry Exception Filter: ${exception?.message}`, {
|
||||
exception,
|
||||
body: request.body,
|
||||
headers: request.headers,
|
||||
url: request.url,
|
||||
method: request.method,
|
||||
requestId,
|
||||
});
|
||||
|
||||
// capture if client has been init
|
||||
if (Boolean(Sentry.getCurrentHub().getClient())) {
|
||||
Sentry.captureException(exception);
|
||||
}
|
||||
response.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
|
||||
status: ERROR_STATUS,
|
||||
timestamp: new Date().toISOString(),
|
||||
path: request.url,
|
||||
error: { code: INTERNAL_SERVER_ERROR, message: "Internal server error." },
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { getEnv } from "@/env";
|
||||
import * as Sentry from "@sentry/node";
|
||||
|
||||
if (process.env.SENTRY_DSN) {
|
||||
// Ensure to call this before requiring any other modules!
|
||||
Sentry.init({
|
||||
dsn: getEnv("SENTRY_DSN"),
|
||||
// Add Performance Monitoring by setting tracesSampleRate
|
||||
// We recommend adjusting this value in production
|
||||
// todo: Evaluate? tracesSampleRate: 1.0
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import * as Sentry from "@sentry/nextjs";
|
||||
|
||||
export function register() {
|
||||
if (process.env.NEXT_RUNTIME === "nodejs") {
|
||||
Sentry.init({
|
||||
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
|
||||
// reduce sample rate to 10% on production
|
||||
tracesSampleRate: process.env.NODE_ENV !== "production" ? 1.0 : 0.1,
|
||||
});
|
||||
}
|
||||
|
||||
if (process.env.NEXT_RUNTIME === "edge") {
|
||||
Sentry.init({
|
||||
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -162,6 +162,7 @@ const nextConfig = {
|
||||
// externalize server-side node_modules with size > 1mb, to improve dev mode performance/RAM usage
|
||||
serverComponentsExternalPackages: ["next-i18next"],
|
||||
optimizePackageImports: ["@calcom/ui"],
|
||||
instrumentationHook: true,
|
||||
},
|
||||
i18n: {
|
||||
...i18n,
|
||||
@@ -590,14 +591,14 @@ const nextConfig = {
|
||||
};
|
||||
|
||||
if (!!process.env.NEXT_PUBLIC_SENTRY_DSN) {
|
||||
nextConfig["sentry"] = {
|
||||
autoInstrumentServerFunctions: true,
|
||||
hideSourceMaps: true,
|
||||
// disable source map generation for the server code
|
||||
disableServerWebpackPlugin: !!process.env.SENTRY_DISABLE_SERVER_WEBPACK_PLUGIN,
|
||||
};
|
||||
|
||||
plugins.push(withSentryConfig);
|
||||
plugins.push((nextConfig) =>
|
||||
withSentryConfig(nextConfig, {
|
||||
autoInstrumentServerFunctions: true,
|
||||
hideSourceMaps: true,
|
||||
// disable source map generation for the server code
|
||||
disableServerWebpackPlugin: !!process.env.SENTRY_DISABLE_SERVER_WEBPACK_PLUGIN,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = () => plugins.reduce((acc, next) => next(acc), nextConfig);
|
||||
@@ -60,7 +60,7 @@
|
||||
"@radix-ui/react-switch": "^1.0.0",
|
||||
"@radix-ui/react-toggle-group": "^1.0.0",
|
||||
"@radix-ui/react-tooltip": "^1.0.0",
|
||||
"@sentry/nextjs": "^7.73.0",
|
||||
"@sentry/nextjs": "^8.8.0",
|
||||
"@stripe/react-stripe-js": "^1.10.0",
|
||||
"@stripe/stripe-js": "^1.35.0",
|
||||
"@tanstack/react-query": "^5.17.15",
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
import { init as SentryInit } from "@sentry/nextjs";
|
||||
|
||||
SentryInit({
|
||||
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
|
||||
});
|
||||
@@ -1,6 +0,0 @@
|
||||
import { init as SentryInit } from "@sentry/nextjs";
|
||||
|
||||
SentryInit({
|
||||
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
|
||||
tracesSampleRate: 1.0,
|
||||
});
|
||||
@@ -1,5 +1,4 @@
|
||||
import { getCurrentHub, startTransaction, captureException } from "@sentry/nextjs";
|
||||
import type { Span, Transaction } from "@sentry/types";
|
||||
import { startSpan, captureException } from "@sentry/nextjs";
|
||||
|
||||
/*
|
||||
WHEN TO USE
|
||||
@@ -11,37 +10,6 @@ For smaller loops, the cost incurred may not be very significant on an absolute
|
||||
considering that a million monitored iterations only took roughly 8 seconds when monitored.
|
||||
*/
|
||||
|
||||
const setUpMonitoring = (name: string) => {
|
||||
// Attempt to retrieve the current transaction from Sentry's scope
|
||||
let transaction = getCurrentHub().getScope()?.getTransaction();
|
||||
|
||||
// Check if there's an existing transaction, if not, start a new one
|
||||
if (!transaction) {
|
||||
transaction = startTransaction({
|
||||
op: name,
|
||||
name: name,
|
||||
});
|
||||
}
|
||||
|
||||
// Start a new span in the current transaction
|
||||
const span = transaction.startChild({
|
||||
op: name,
|
||||
description: `Executing ${name}`,
|
||||
});
|
||||
return [transaction, span];
|
||||
};
|
||||
|
||||
// transaction will always be Transaction, since returned in a list with Span type must be listed as either or here
|
||||
const finishMonitoring = (transaction: Transaction | Span, span: Span) => {
|
||||
// Attempt to retrieve the current transaction from Sentry's scope
|
||||
span.finish();
|
||||
|
||||
// If this was a new transaction, finish it
|
||||
if (!getCurrentHub().getScope()?.getTransaction()) {
|
||||
transaction.finish();
|
||||
}
|
||||
};
|
||||
|
||||
const monitorCallbackAsync = async <T extends (...args: any[]) => any>(
|
||||
cb: T,
|
||||
...args: Parameters<T>
|
||||
@@ -49,17 +17,15 @@ const monitorCallbackAsync = async <T extends (...args: any[]) => any>(
|
||||
// Check if Sentry set
|
||||
if (!process.env.NEXT_PUBLIC_SENTRY_DSN) return (await cb(...args)) as ReturnType<T>;
|
||||
|
||||
const [transaction, span] = setUpMonitoring(cb.name);
|
||||
|
||||
try {
|
||||
const result = await cb(...args);
|
||||
return result as ReturnType<T>;
|
||||
} catch (error) {
|
||||
captureException(error);
|
||||
throw error;
|
||||
} finally {
|
||||
finishMonitoring(transaction, span);
|
||||
}
|
||||
return await startSpan({ name: cb.name }, async () => {
|
||||
try {
|
||||
const result = await cb(...args);
|
||||
return result as ReturnType<T>;
|
||||
} catch (error) {
|
||||
captureException(error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const monitorCallbackSync = <T extends (...args: any[]) => any>(
|
||||
@@ -69,17 +35,15 @@ const monitorCallbackSync = <T extends (...args: any[]) => any>(
|
||||
// Check if Sentry set
|
||||
if (!process.env.NEXT_PUBLIC_SENTRY_DSN) return cb(...args) as ReturnType<T>;
|
||||
|
||||
const [transaction, span] = setUpMonitoring(cb.name);
|
||||
|
||||
try {
|
||||
const result = cb(...args);
|
||||
return result as ReturnType<T>;
|
||||
} catch (error) {
|
||||
captureException(error);
|
||||
throw error;
|
||||
} finally {
|
||||
finishMonitoring(transaction, span);
|
||||
}
|
||||
return startSpan({ name: cb.name }, () => {
|
||||
try {
|
||||
const result = cb(...args);
|
||||
return result as ReturnType<T>;
|
||||
} catch (error) {
|
||||
captureException(error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export default monitorCallbackAsync;
|
||||
|
||||
@@ -331,6 +331,7 @@
|
||||
"NEXT_PUBLIC_VERCEL_ENV",
|
||||
"NEXT_PUBLIC_VERCEL_BRANCH_URL",
|
||||
"NEXT_PUBLIC_GTM_ID",
|
||||
"NEXT_RUNTIME",
|
||||
"NEXTAUTH_COOKIE_DOMAIN",
|
||||
"NEXTAUTH_SECRET",
|
||||
"NEXTAUTH_URL",
|
||||
|
||||
Reference in new issue
Block a user