From 567ffb4b015f3f174e010a5360e15152e985be29 Mon Sep 17 00:00:00 2001 From: Burak Yigit Kaya Date: Fri, 4 Oct 2024 21:09:55 +0100 Subject: [PATCH 1/2] fix: Open relevant error when SpotlightMiddleware is on This fixes an issue with the recent SpotlightMiddleware patch where the error triggered the page was not highlighted/opened automatically. It changes the semapics of `capture_event` and methods depending on this a bit: we now return the event_id if the error is sent to spotlight even if it was not sent upstream to Sentry servers. --- sentry_sdk/client.py | 19 +++++++++---------- sentry_sdk/spotlight.py | 16 +++++++++++----- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/sentry_sdk/client.py b/sentry_sdk/client.py index 9d30bb45f2..b1e7868031 100644 --- a/sentry_sdk/client.py +++ b/sentry_sdk/client.py @@ -753,18 +753,16 @@ def capture_event( :returns: An event ID. May be `None` if there is no DSN set or of if the SDK decided to discard the event for other reasons. In such situations setting `debug=True` on `init()` may help. """ - if hint is None: - hint = {} - event_id = event.get("event_id") hint = dict(hint or ()) # type: Hint - if event_id is None: - event["event_id"] = event_id = uuid.uuid4().hex if not self._should_capture(event, hint, scope): return None profile = event.pop("profile", None) + event_id = event.get("event_id") + if event_id is None: + event["event_id"] = event_id = uuid.uuid4().hex event_opt = self._prepare_event(event, hint, scope) if event_opt is None: return None @@ -812,15 +810,16 @@ def capture_event( for attachment in attachments or (): envelope.add_item(attachment.to_envelope_item()) + return_value = None if self.spotlight: self.spotlight.capture_envelope(envelope) + return_value = event_id - if self.transport is None: - return None - - self.transport.capture_envelope(envelope) + if self.transport is not None: + self.transport.capture_envelope(envelope) + return_value = event_id - return event_id + return return_value def capture_session( self, session # type: Session diff --git a/sentry_sdk/spotlight.py b/sentry_sdk/spotlight.py index 3e8072b5d8..0d8d41b8a7 100644 --- a/sentry_sdk/spotlight.py +++ b/sentry_sdk/spotlight.py @@ -79,14 +79,20 @@ def process_exception(self, _request, exception): spotlight_url = spotlight_client.url.rsplit("/", 1)[0] try: - spotlight = ( - urllib.request.urlopen(spotlight_url).read().decode("utf-8") - ).replace("", f'') + spotlight = urllib.request.urlopen(spotlight_url).read().decode("utf-8") except urllib.error.URLError: return None else: - sentry_sdk.api.capture_exception(exception) - return HttpResponseServerError(spotlight) + event_id = sentry_sdk.api.capture_exception(exception) + return HttpResponseServerError( + spotlight.replace( + "", + ( + f'' + f'' + ), + ) + ) except ImportError: settings = None From 5817495d36baa90a4a3fad14ddade9ef7ff5b256 Mon Sep 17 00:00:00 2001 From: Burak Yigit Kaya Date: Fri, 4 Oct 2024 22:34:44 +0100 Subject: [PATCH 2/2] fix lint error --- sentry_sdk/spotlight.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sentry_sdk/spotlight.py b/sentry_sdk/spotlight.py index 0d8d41b8a7..e21bf56545 100644 --- a/sentry_sdk/spotlight.py +++ b/sentry_sdk/spotlight.py @@ -89,7 +89,9 @@ def process_exception(self, _request, exception): "", ( f'' - f'' + ''.format( + event_id=event_id + ) ), ) )