From bd1b60e564e3486cf8a8fbe1903b394b472408fd Mon Sep 17 00:00:00 2001 From: Rob Brackett Date: Thu, 17 Oct 2019 23:10:26 -0700 Subject: [PATCH] Use Sentry scope when setting breadcrumbs (#498) We've had a longstanding issue where some of our Sentry errors wind up with a giant pile of accumulated breadcrumbs that seem to be from other requests. I think it might be because we are not creating a scope when manually adding these breadcrumbs for errors, and they aren't getting cleared when you call `capture_message` like they used to in the old SDK. --- web_monitoring/diffing_server.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/web_monitoring/diffing_server.py b/web_monitoring/diffing_server.py index cec968e1f..792596d20 100644 --- a/web_monitoring/diffing_server.py +++ b/web_monitoring/diffing_server.py @@ -308,19 +308,21 @@ def write_error(self, status_code, **kwargs): # Pass non-raised (i.e. we manually called `send_error()`), non-user # errors to Sentry.io. if actual_error is None and response['code'] >= 500: - # TODO: this breadcrumb should happen at the start of the request - # handler, but we need to test and make sure crumbs are properly - # attached to *this* HTTP request and don't bleed over to others, - # since Sentry's special support for Tornado has been dropped. - headers = dict(self.request.headers) - if 'Authorization' in headers: - headers['Authorization'] = '[removed]' - sentry_sdk.add_breadcrumb(category='request', data={ - 'url': self.request.full_url(), - 'method': self.request.method, - 'headers': headers, - }) - sentry_sdk.capture_message(f'{self._reason} (status: {response["code"]})') + with sentry_sdk.push_scope(): + # TODO: this breadcrumb should happen at the start of the + # request handler, but we need to test and make sure crumbs are + # properly attached to *this* HTTP request and don't bleed over + # to others, since Sentry's special support for Tornado has + # been dropped. + headers = dict(self.request.headers) + if 'Authorization' in headers: + headers['Authorization'] = '[removed]' + sentry_sdk.add_breadcrumb(category='request', data={ + 'url': self.request.full_url(), + 'method': self.request.method, + 'headers': headers, + }) + sentry_sdk.capture_message(f'{self._reason} (status: {response["code"]})') # Fill in full info if configured to do so if self.settings.get('serve_traceback') and 'exc_info' in kwargs: