Skip to main content

How do I disable Sentry in specific environments?

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.

  1. Skip initialization (recommended). Use an environment variable to decide whether to call Sentry.init(). When you skip Sentry.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",
    });
    }
  2. Use the enabled option. Set enabled based on the environment. Per the enabled option documentation, setting enabled: false stops 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",
    });
  3. Drop events with beforeSend. If you must initialize Sentry in every environment, return null from beforeSend to drop events. Note that beforeSend applies to error and message events only. It does not drop transactions or spans, so if tracing is enabled you must also drop those with beforeSendTransaction. 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.

Did this answer your question?