@@ -27,6 +27,32 @@ class RailsMiniApp < Rails::Application
2727 config . api_only = true
2828 config . force_ssl = false
2929
30+ # Validate DSN before starting
31+ initializer :validate_dsn , before : :configure_sentry do
32+ dsn_string = ENV [ "SENTRY_DSN" ]
33+
34+ if dsn_string . nil? || dsn_string . empty?
35+ puts "ERROR: SENTRY_DSN environment variable is required but not set"
36+ exit ( 1 )
37+ end
38+
39+ begin
40+ dsn = Sentry ::DSN . new ( dsn_string )
41+ unless dsn . valid?
42+ puts "ERROR: Invalid SENTRY_DSN: #{ dsn_string } "
43+ puts "DSN must include host, path, public_key, and project_id"
44+ exit ( 1 )
45+ end
46+ puts "✅ SENTRY_DSN validation passed: #{ dsn_string } "
47+ rescue URI ::InvalidURIError => e
48+ puts "ERROR: Invalid SENTRY_DSN format: #{ e . message } "
49+ exit ( 1 )
50+ rescue => e
51+ puts "ERROR: Failed to parse SENTRY_DSN: #{ e . message } "
52+ exit ( 1 )
53+ end
54+ end
55+
3056 # Configure Sentry
3157 initializer :configure_sentry do
3258 Sentry . init do |config |
@@ -40,7 +66,7 @@ class RailsMiniApp < Rails::Application
4066 config . release = "sentry-ruby-rails-mini-#{ Time . now . utc } "
4167
4268 config . transport . transport_class = Sentry ::DebugTransport
43- config . sdk_debug_transport_log_file = "/workspace/sentry/ log/ sentry_debug_events.log"
69+ config . sdk_debug_transport_log_file = File . join ( Dir . pwd , " log" , " sentry_debug_events.log")
4470 config . background_worker_threads = 0
4571 end
4672 end
@@ -66,30 +92,41 @@ def set_cors_headers
6692class EventsController < ActionController ::Base
6793 before_action :set_cors_headers
6894
69-
95+ def health
96+ render json : {
97+ status : "ok" ,
98+ timestamp : Time . now . utc . iso8601 ,
99+ sentry_initialized : Sentry . initialized? ,
100+ log_file_writable : check_log_file_writable
101+ }
102+ end
70103
71104 def trace_headers
72- # Return current trace propagation headers
73105 headers = Sentry . get_trace_propagation_headers || { }
74106 render json : { headers : headers }
75107 end
76108
77-
78-
79109 private
80110
111+ def check_log_file_writable
112+ log_file_path = File . join ( Dir . pwd , "log" , "sentry_debug_events.log" )
113+ File . writable? ( File . dirname ( log_file_path ) ) &&
114+ ( !File . exist? ( log_file_path ) || File . writable? ( log_file_path ) )
115+ rescue
116+ false
117+ end
118+
81119 def set_cors_headers
82120 response . headers [ 'Access-Control-Allow-Origin' ] = '*'
83121 response . headers [ 'Access-Control-Allow-Methods' ] = 'GET, POST, PUT, DELETE, OPTIONS'
84122 response . headers [ 'Access-Control-Allow-Headers' ] = 'Content-Type, Authorization, sentry-trace, baggage'
85123 end
86124end
87125
88- # Initialize the Rails app first
89126RailsMiniApp . initialize!
90127
91- # Configure routes after initialization
92128RailsMiniApp . routes . draw do
129+ get '/health' , to : 'events#health'
93130 get '/error' , to : 'error#error'
94131 get '/trace_headers' , to : 'events#trace_headers'
95132
0 commit comments