0

I am trying to prevent Sentry from logging errors to my Sentry dashboard while I'm working on my local server (i.e http://127.0.0.1:8000/). The only time I want sentry to log errors to my dashboard is when my code is in production. How can I go about this? I have tried this below, but it doesn't work:

if DEBUG == True
    sentry_sdk.init(
        dsn=os.environ.get('SENTRY_DSN', None),
        integrations=[DjangoIntegration()],
    
        # Set traces_sample_rate to 1.0 to capture 100%
        # of transactions for performance monitoring.
        # We recommend adjusting this value in production.
        traces_sample_rate=1.0,
    
        # If you wish to associate users to errors (assuming you are using
        # django.contrib.auth) you may enable sending PII data.
        send_default_pii=True
    )
PercySherlock
  • 371
  • 1
  • 2
  • 12

4 Answers4

5

There are two ways you can do this:

The first option is to import sys and check for runserver (ref:https://stackoverflow.com/a/49874564/15205504)

import sys

if (len(sys.argv) >= 2 and sys.argv[1] != 'runserver'):
    sentry_sdk.init(
        dsn=os.environ.get('SENTRY_DSN', None),
        integrations=[DjangoIntegration()],

        # Set traces_sample_rate to 1.0 to capture 100%
        # of transactions for performance monitoring.
        # We recommend adjusting this value in production.
        traces_sample_rate=1.0,

        # If you wish to associate users to errors (assuming you are using
        # django.contrib.auth) you may enable sending PII data.
        send_default_pii=True
)

The second option is to specify an environment type in your settings.py. For instance, if your production server is Heroku, you can create an env_type variable in Heroku or your .env file and set it to 'HEROKU', then use it like this:

env_type = os.environ.get('env_type', 'LOCAL')

if env_type == 'HEROKU':
    sentry_sdk.init(
        dsn=os.environ.get('SENTRY_DSN', None),
        integrations=[DjangoIntegration()],

        # Set traces_sample_rate to 1.0 to capture 100%
        # of transactions for performance monitoring.
        # We recommend adjusting this value in production.
        traces_sample_rate=1.0,

        # If you wish to associate users to errors (assuming you are using
        # django.contrib.auth) you may enable sending PII data.
        send_default_pii=True
)
Toluwalemi
  • 391
  • 5
  • 16
2

Setting the dsn to None will no-op all SDK operations, so your config should work unless you are also setting the SENTRY_DSN variable in your local environment.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 01 '22 at 13:10
0

I also use a similar method to what is suggested in the approved answer but I prefer len(sys.argv) > 1 rather than len(sys.argv) >= 2. Using len(sys.argv) >= 2 may raise 'out of range' errors in apache production.

INSTANCE_RUNNING_ON_LOCALHOST = False
if len(sys.argv) > 1: # this check is needed to prevent 'out of range' error in apache production.
    INSTANCE_RUNNING_ON_LOCALHOST = (sys.argv[1] == 'runserver') # Determine if Django is running under the development server. #Ref: https://stackoverflow.com/questions/12027545/determine-if-django-is-running-under-the-development-server/12028260 

if not INSTANCE_RUNNING_ON_LOCALHOST:
    sentry_sdk.init(
        dsn=os.environ.get('SENTRY_DSN', None),
        integrations=[
            DjangoIntegration(),
        ],

        # Set traces_sample_rate to 1.0 to capture 100%
        # of transactions for performance monitoring.
        # We recommend adjusting this value in production.
        traces_sample_rate=1.0,

        # If you wish to associate users to errors (assuming you are using
        # django.contrib.auth) you may enable sending PII data.
        send_default_pii=True
    )
Chandragupta Borkotoky
  • 1,596
  • 1
  • 11
  • 16
-1

Try like this:

  if not DEBUG:
    sentry_sdk.init(
        dsn="SENTRY_DSN",
        integrations=[DjangoIntegration()],

        # Set traces_sample_rate to 1.0 to capture 100%
        # of transactions for performance monitoring.
        # We recommend adjusting this value in production.
        traces_sample_rate=1.0,

        # If you wish to associate users to errors (assuming you are using
        # django.contrib.auth) you may enable sending PII data.
        send_default_pii=True
    )
Buz Geniq
  • 1
  • 1