Skip to content

Commit 0a11c45

Browse files
perf: Only run Sentry in production (calcom#23167)
* Only run Sentry in production * Only run Sentry in production for instrumentation-client --------- Co-authored-by: Keith Williams <keithwillcode@gmail.com>
1 parent 4ae5de5 commit 0a11c45

2 files changed

Lines changed: 55 additions & 40 deletions

File tree

apps/web/instrumentation-client.ts

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,48 @@
33
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
44
import * as Sentry from "@sentry/nextjs";
55

6-
Sentry.init({
7-
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN_CLIENT,
6+
if (process.env.NODE_ENV === "production") {
7+
Sentry.init({
8+
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN_CLIENT,
89

9-
sampleRate: parseFloat(process.env.SENTRY_SAMPLE_RATE ?? "1.0") || 1.0,
10-
// Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
11-
tracesSampleRate: parseFloat(process.env.SENTRY_TRACES_SAMPLE_RATE ?? "0.0") || 0.0,
10+
sampleRate: parseFloat(process.env.SENTRY_SAMPLE_RATE ?? "1.0") || 1.0,
11+
// Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
12+
tracesSampleRate: parseFloat(process.env.SENTRY_TRACES_SAMPLE_RATE ?? "0.0") || 0.0,
1213

13-
// Define how likely Replay events are sampled.
14-
// This sets the sample rate to be 10%. You may want this to be 100% while
15-
// in development and sample at a lower rate in production
16-
// replaysSessionSampleRate: parseFloat(process.env.SENTRY_REPLAYS_SESSION_SAMPLE_RATE ?? "0.0") || 0.0,
14+
// Define how likely Replay events are sampled.
15+
// This sets the sample rate to be 10%. You may want this to be 100% while
16+
// in development and sample at a lower rate in production
17+
// replaysSessionSampleRate: parseFloat(process.env.SENTRY_REPLAYS_SESSION_SAMPLE_RATE ?? "0.0") || 0.0,
1718

18-
// Define how likely Replay events are sampled when an error occurs.
19-
// replaysOnErrorSampleRate: parseFloat(process.env.SENTRY_REPLAYS_ON_ERROR_SAMPLE_RATE ?? "0.0") || 0.0,
19+
// Define how likely Replay events are sampled when an error occurs.
20+
// replaysOnErrorSampleRate: parseFloat(process.env.SENTRY_REPLAYS_ON_ERROR_SAMPLE_RATE ?? "0.0") || 0.0,
2021

21-
// Setting this option to true will print useful information to the console while you're setting up Sentry.
22-
debug: !!process.env.SENTRY_DEBUG,
23-
beforeSend(event) {
24-
if (
25-
event.exception?.values?.some(
26-
(e) =>
27-
// Ignore fake error "UnhandledRejection: Non-Error promise rejection captured with value: Object Not Found Matching Id:3, MethodName:update, ParamCount:4"
28-
// Raised GH issue: https://github.com/getsentry/sentry-javascript/issues/3440
29-
e.value?.includes("Non-Error promise rejection captured with") ||
30-
e.value?.includes("Object Not Found Matching Id")
31-
)
32-
) {
33-
return null;
34-
}
22+
// Setting this option to true will print useful information to the console while you're setting up Sentry.
23+
debug: !!process.env.SENTRY_DEBUG,
24+
beforeSend(event) {
25+
if (
26+
event.exception?.values?.some(
27+
(e) =>
28+
// Ignore fake error "UnhandledRejection: Non-Error promise rejection captured with value: Object Not Found Matching Id:3, MethodName:update, ParamCount:4"
29+
// Raised GH issue: https://github.com/getsentry/sentry-javascript/issues/3440
30+
e.value?.includes("Non-Error promise rejection captured with") ||
31+
e.value?.includes("Object Not Found Matching Id")
32+
)
33+
) {
34+
return null;
35+
}
3536

36-
event.tags = {
37-
...event.tags,
38-
errorSource: "client",
39-
};
40-
return event;
41-
},
42-
});
37+
event.tags = {
38+
...event.tags,
39+
errorSource: "client",
40+
};
41+
return event;
42+
},
43+
});
44+
}
4345

44-
export const onRouterTransitionStart = Sentry.captureRouterTransitionStart;
46+
export function onRouterTransitionStart(url: string, navigationType: "push" | "replace" | "traverse") {
47+
if (process.env.NODE_ENV === "production") {
48+
Sentry.captureRouterTransitionStart(url, navigationType);
49+
}
50+
}

apps/web/instrumentation.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1+
import { type Instrumentation } from 'next'
2+
13
import * as Sentry from "@sentry/nextjs";
24

35
export async function register() {
4-
if (process.env.NEXT_PUBLIC_SENTRY_DSN && process.env.NEXT_RUNTIME === "nodejs") {
5-
await import("./sentry.server.config");
6-
}
7-
8-
if (process.env.NEXT_PUBLIC_SENTRY_DSN && process.env.NEXT_RUNTIME === "edge") {
9-
await import("./sentry.edge.config");
6+
if (process.env.NODE_ENV === "production") {
7+
if (process.env.NEXT_PUBLIC_SENTRY_DSN && process.env.NEXT_RUNTIME === "nodejs") {
8+
await import("./sentry.server.config");
9+
}
10+
if (process.env.NEXT_PUBLIC_SENTRY_DSN && process.env.NEXT_RUNTIME === "edge") {
11+
await import("./sentry.edge.config");
12+
}
1013
}
1114
}
1215

13-
export const onRequestError = Sentry.captureRequestError;
16+
export const onRequestError: Instrumentation.onRequestError = ( err,
17+
request,
18+
context) => {
19+
if (process.env.NODE_ENV === "production") {
20+
Sentry.captureRequestError(err, request, context);
21+
}
22+
};

0 commit comments

Comments
 (0)