From c407d8cb8b932e39806046892fe73be90d435b41 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Fri, 14 Apr 2023 12:50:50 +0200 Subject: [PATCH] Capture context for Sentry errors during WebSocket connection Special handling is needed to ensure we have the transaction ID and Rack ENV. Otherwise the error cannot be matched to a transaction in Sentry. This special behavior is needed because of the underlying EventMachine, which executes code in a different thread/context. --- lib/runner/connection.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/runner/connection.rb b/lib/runner/connection.rb index df8f687f..ad8256d2 100644 --- a/lib/runner/connection.rb +++ b/lib/runner/connection.rb @@ -42,6 +42,9 @@ class Runner::Connection I18n.with_locale(@locale) do clone_sentry_hub_from_span(sentry_span) __send__(:"on_#{event_type}", event, sentry_span) + rescue StandardError => e + Sentry.capture_exception(e) + raise e end end end @@ -241,7 +244,7 @@ class Runner::Connection # and adapted to the Websocket protocol running with EventMachine. def clone_sentry_hub_from_span(sentry_span) - Thread.current.thread_variable_set(Sentry::THREAD_LOCAL, sentry_span.transaction.hub) if sentry_span + Thread.current.thread_variable_set(Sentry::THREAD_LOCAL, sentry_span.transaction.hub.clone) if sentry_span end def sentry_trace_header(sentry_span)