1818redis_url = ENV . fetch ( "REDIS_URL" , "redis://localhost:6379" )
1919Resque . redis = redis_url if defined? ( Resque )
2020
21+ # Emits a Sentry log from above Sentry::Rails::CaptureExceptions - the same window
22+ # Rails::Rack::Logger writes its "Started GET ..." line from, and the window where a
23+ # request used to pick up an unrelated or stale trace_id. Scoped to one path so the
24+ # other e2e scenarios keep a quiet log stream.
25+ class EarlyRequestLogMiddleware
26+ PATH = "/trace_context"
27+
28+ def initialize ( app )
29+ @app = app
30+ end
31+
32+ def call ( env )
33+ Sentry . logger . info ( "early middleware log" , source : "middleware" ) if env [ "PATH_INFO" ] == PATH
34+
35+ @app . call ( env )
36+ end
37+ end
38+
2139class RailsMiniApp < Rails ::Application
2240 config . hosts = nil
2341 config . secret_key_base = "test_secret_key_base_for_rails_mini_app"
@@ -49,6 +67,8 @@ class RailsMiniApp < Rails::Application
4967 end
5068
5169 config . active_job . queue_adapter = SUPPORTED_ACTIVE_JOB_ADAPTERS [ adapter_name ]
70+
71+ config . middleware . insert_before Rails ::Rack ::Logger , EarlyRequestLogMiddleware
5272 config . x . active_job_adapter_name = adapter_name
5373
5474 def debug_log_path
@@ -75,6 +95,7 @@ def debug_log_path
7595 config . background_worker_threads = 0
7696
7797 config . enable_logs = true
98+ config . max_log_events = 1
7899 config . structured_logging . logger_class = Sentry ::DebugStructuredLogger
79100 config . structured_logging . file_path = debug_log_path . join ( "sentry_e2e_tests.log" )
80101
@@ -239,6 +260,24 @@ def set_cors_headers
239260 end
240261end
241262
263+ class TraceContextController < ActionController ::Base
264+ before_action :set_cors_headers
265+
266+ def show
267+ Sentry . logger . info ( "controller log" , source : "controller" )
268+
269+ render json : { trace : Sentry . get_current_scope . get_trace_context }
270+ end
271+
272+ private
273+
274+ def set_cors_headers
275+ response . headers [ "Access-Control-Allow-Origin" ] = "*"
276+ response . headers [ "Access-Control-Allow-Methods" ] = "GET, POST, PUT, DELETE, OPTIONS"
277+ response . headers [ "Access-Control-Allow-Headers" ] = "Content-Type, Authorization, sentry-trace, baggage"
278+ end
279+ end
280+
242281class JobsController < ActionController ::Base
243282 before_action :set_cors_headers
244283
@@ -360,6 +399,7 @@ def set_cors_headers
360399 get '/health' , to : 'events#health'
361400 get '/error' , to : 'error#error'
362401 get '/trace_headers' , to : 'events#trace_headers'
402+ get '/trace_context' , to : 'trace_context#show'
363403 get '/logged_events' , to : 'events#logged_events'
364404 post '/clear_logged_events' , to : 'events#clear_logged_events'
365405
0 commit comments