Issue
I want to disable Sentry in specific environments, such as production, while keeping it active in QA and testing environments. I need to know which SDK options control whether Sentry initializes and sends events per environment.
Applies To
All SaaS Customers and Self-Hosted Users
SDK Setup
Configuration / Filtering
Resolution
To disable Sentry in an environment, do not initialize the SDK in that environment. This is the only method that removes all instrumentation overhead. Choose one of the following approaches, ordered from most to least complete.
Skip initialization (recommended). Use an environment variable to decide whether to call
Sentry.init(). When you skipSentry.init(), the SDK adds no instrumentation and sends no data.import * as Sentry from "@sentry/react";
if (process.env.REACT_APP_ENVIRONMENT !== "production") {
Sentry.init({
dsn: "your-dsn-here",
});
}Use the
enabledoption. Setenabledbased on the environment. Per theenabledoption documentation, settingenabled: falsestops the SDK from sending events but does not remove all instrumentation overhead.import * as Sentry from "@sentry/react";
Sentry.init({
dsn: "your-dsn-here",
enabled: process.env.REACT_APP_ENVIRONMENT !== "production",
});Drop events with
beforeSend. If you must initialize Sentry in every environment, returnnullfrombeforeSendto drop events. Note thatbeforeSendapplies to error and message events only. It does not drop transactions or spans, so if tracing is enabled you must also drop those withbeforeSendTransaction. This approach still runs full instrumentation.import * as Sentry from "@sentry/react";
Sentry.init({
dsn: "your-dsn-here",
beforeSend(event) {
if (process.env.REACT_APP_ENVIRONMENT === "production") {
return null;
}
return event;
},
beforeSendTransaction(event) {
if (process.env.REACT_APP_ENVIRONMENT === "production") {
return null;
}
return event;
},
});
The environment variable used to detect the environment is framework-specific. Use the one appropriate for your setup.
To turn Sentry on or off at runtime rather than per environment, see how to dynamically disable and enable Sentry after initialization.
