Search code examples
loggingamazon-elastic-beanstalksentry

Disable EB healh check logs in sentry


I've set up entry in a django project. I want to disable EB health check entries.This is the configuration.

def ignore_healthcheck_breadcrumbs(crumb, hint):
    # Ignore all breadcrumbs from ELB health checks
    if crumb.get('category') == 'request':
        request = crumb.get('data', {}).get('request')
        if request and request.get('headers', {}).get('User-Agent') == 'ELB-HealthChecker/2.0':
            return None
    return crumb

def before_send(event, hint):
    request = event.get('request')
    if request and 'HTTP_USER_AGENT' in request:
        user_agent = request['HTTP_USER_AGENT']
        if 'ELB-HealthChecker/2.0' in user_agent:
            return None
    return event

sentry_sdk.init(
    dsn="secret",
    integrations=[DjangoIntegration()],
    traces_sample_rate=1.0,
    before_send=before_send,
    before_breadcrumb=ignore_healthcheck_breadcrumbs,
    send_default_pii=True
)

I tried to do it with middleware like this.

class SentryEbsHealthCheckMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        logger.info("MIDDLEWARE LOGGING")
        logger.info(request.META)
        logger.info("MIDDLEWARE LOGGING")
        hub = Hub.current
        if request.META.get('HTTP_USER_AGENT') == 'ELB-HealthChecker/2.0':
            # Disable Sentry for ELB health checks
            with hub.push_scope():
                hub.scope.clear()
            hub.flush()
            logger.info("FLUSHING WORKED")
            return HttpResponse(status=200)
        else:
            logger.info("FLUSHING DIDN'T WORK")
            response = self.get_response(request)
            return response

The interesting part is that I see all messages in logs even FLUSHING WORKED but new logs keep adding up in sentry. What can I do?


Solution

  • It can be done by traces_sampler

    def traces_sampler(sampling_context):
        if sampling_context['wsgi_environ']['HTTP_USER_AGENT'] == 'ELB-HealthChecker/2.0':
            return 0
        else:
            return 1
    
    sentry_sdk.init(
        dsn="secret",
        integrations=[DjangoIntegration()],
        traces_sampler=traces_sampler,
        send_default_pii=True
    )