Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude some URLs in Django for Sentry Performance tracing

Tags:

django

sentry

I have a Django application with a health check endpoint that's using django-health-check.

In the url_patterns I have added the following line:

  url(r'^ht/', include('health_check.urls')),

The issue is that the health check is filling all the Sentry transaction limits.

How can I exclude the health check endpoint in Sentry?

like image 651
Saber Avatar asked Oct 18 '25 09:10

Saber


1 Answers

The way to handle this kind of cases is to use a sampling function to control the sample rate according to the URL or other parameters.

def traces_sampler(ctx):
    if 'wsgi_environ' in ctx:
        url = ctx['wsgi_environ'].get('PATH_INFO', '')
        if url.startswith('/ht/'):
            return 0  # Don't trace any
    return 1  # Trace all

sentry_sdk.init(
    # ...
    traces_sampler=traces_sampler,
)

Here is a more complete example.

def traces_sampler(ctx):
    if ctx['parent_sampled'] is not None:
        # If this transaction has a parent, we usually want to sample it
        # if and only if its parent was sampled.
        return ctx['parent_sampled']
    op = ctx['transaction_context']['op']
    if 'wsgi_environ' in ctx:
        # Get the URL for WSGI requests
        url = ctx['wsgi_environ'].get('PATH_INFO', '')
    elif 'asgi_scope' in ctx:
        # Get the URL for ASGI requests
        url = ctx['asgi_scope'].get('path', '')
    else:
        # Other kinds of transactions don't have a URL
        url = ''
    if op == 'http.server':
        # Conditions only relevant to operation "http.server"
        if url.startswith('/ht/'):
            return 0  # Don't trace any of these transactions
    return 0.1  # Trace 10% of other transactions

sentry_sdk.init(
    # ...
    traces_sampler=traces_sampler,
)
like image 159
Antoine Pinsard Avatar answered Oct 20 '25 23:10

Antoine Pinsard