Excluding specific URLs for Sentry traces in Django

Let's say you have a Django app that serves various healthchecks from the /-/ prefix and uses whitenoise to serve static files from /static/, but you don't want to capture performance data on those requests. It will skew the results and also rack up additional charges on your account.

In your Django settings file, you can add this:


def sentry_traces_sampler(sampling_context):
    """Ignore performance for healthcheck and staticfile requests in Sentry"""
    path = sampling_context.get("wsgi_environ", {}).get("PATH_INFO", "")
    ignore = path.startswith("/-/") or path.startswith("/static/")
    return 0 if ignore else SENTRY_TRACES_SAMPLE_RATE

In your call to sentry_sdk.init add the following keyword argument:

    # ...