|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Sentry configuration for Rails 8 |
| 4 | +# Docs: https://docs.sentry.io/platforms/ruby/guides/rails/ |
| 5 | + |
| 6 | +sentry_dsn = Rails.application.credentials.dig(:sentry, :dsn) |
| 7 | +sentry_enabled = Rails.application.credentials.dig(:sentry, :enabled) != false |
| 8 | + |
| 9 | +if sentry_dsn && !sentry_dsn.empty? && sentry_enabled |
| 10 | + Sentry.init do |config| |
| 11 | + config.dsn = sentry_dsn |
| 12 | + config.environment = Rails.env |
| 13 | + config.enabled_environments = %w[production test development] |
| 14 | + |
| 15 | + # Release tracking |
| 16 | + config.release = begin |
| 17 | + `git rev-parse HEAD 2>/dev/null`.strip.presence || "unknown" |
| 18 | + rescue => e |
| 19 | + Rails.logger.warn("Failed to get git revision: #{e.message}") |
| 20 | + "unknown" |
| 21 | + end |
| 22 | + |
| 23 | + # Breadcrumbs and logging |
| 24 | + config.breadcrumbs_logger = [:active_support_logger, :http_logger] |
| 25 | + config.sdk_logger = Rails.logger |
| 26 | + |
| 27 | + # PII and data collection |
| 28 | + config.send_default_pii = Rails.env.development? |
| 29 | + |
| 30 | + # Performance monitoring - environment specific |
| 31 | + traces_rate = Rails.application.credentials.dig(:sentry, :traces_sample_rate) || |
| 32 | + (Rails.env.production? ? 0.1 : 1.0) |
| 33 | + profiles_rate = Rails.application.credentials.dig(:sentry, :profiles_sample_rate) || |
| 34 | + (Rails.env.production? ? 0.1 : 1.0) |
| 35 | + |
| 36 | + config.traces_sample_rate = traces_rate |
| 37 | + config.profiles_sample_rate = profiles_rate |
| 38 | + |
| 39 | + # Enhanced performance sampling |
| 40 | + config.traces_sampler = lambda do |sampling_context| |
| 41 | + transaction_name = sampling_context[:transaction_context][:name] |
| 42 | + case transaction_name |
| 43 | + when /health|heartbeat|ping/ |
| 44 | + 0.0 # Skip health checks |
| 45 | + when /users\/(sign_in|sign_up|password)/ |
| 46 | + 1.0 # Always trace authentication endpoints |
| 47 | + when /projects|chapters|countries/ |
| 48 | + 0.8 # High sampling for core content |
| 49 | + when /admin/ |
| 50 | + 0.5 # Sample admin pages at 50% |
| 51 | + else |
| 52 | + traces_rate |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + # Rails 8 specific instrumentation |
| 57 | + config.rails.report_rescued_exceptions = true |
| 58 | + config.instrumenter = :active_support |
| 59 | + |
| 60 | + # Filter noise - common Rails exceptions |
| 61 | + config.excluded_exceptions += %w[ |
| 62 | + ActionController::RoutingError |
| 63 | + ActiveRecord::RecordNotFound |
| 64 | + ActionController::InvalidAuthenticityToken |
| 65 | + ActionController::UnknownFormat |
| 66 | + ActionDispatch::Http::MimeNegotiation::InvalidType |
| 67 | + Rack::QueryParser::ParameterTypeError |
| 68 | + Rack::QueryParser::InvalidParameterError |
| 69 | + ] |
| 70 | + |
| 71 | + # Transaction filtering |
| 72 | + config.before_send_transaction = lambda do |event, hint| |
| 73 | + # Skip asset requests and health checks |
| 74 | + return nil if event.transaction&.match?(/\.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$/) |
| 75 | + return nil if event.transaction&.match?(/health|assets|favicon|robots\.txt/) |
| 76 | + event |
| 77 | + end |
| 78 | + |
| 79 | + # Enhanced error context and filtering |
| 80 | + config.before_send = lambda do |event, hint| |
| 81 | + # Skip letter opener and development tools |
| 82 | + return nil if event.request&.url&.match?(/letter_opener|web-console|__better_errors/) |
| 83 | + |
| 84 | + # Add user context (non-PII) |
| 85 | + if defined?(Current) && Current.respond_to?(:user) && Current.user |
| 86 | + event.user = { |
| 87 | + id: Current.user.id, |
| 88 | + role: Current.user.role, |
| 89 | + created_at: Current.user.created_at |
| 90 | + } |
| 91 | + end |
| 92 | + |
| 93 | + # Add request context |
| 94 | + if event.request |
| 95 | + event.tags.merge!({ |
| 96 | + request_id: event.request.env['action_dispatch.request_id'], |
| 97 | + user_agent: event.request.env['HTTP_USER_AGENT']&.truncate(100), |
| 98 | + referer: event.request.env['HTTP_REFERER']&.truncate(200) |
| 99 | + }) |
| 100 | + end |
| 101 | + |
| 102 | + # Add Rails context |
| 103 | + event.tags.merge!({ |
| 104 | + rails_version: Rails.version, |
| 105 | + ruby_version: RUBY_VERSION, |
| 106 | + environment: Rails.env |
| 107 | + }) |
| 108 | + |
| 109 | + # Scrub sensitive data |
| 110 | + if event.request&.data.is_a?(String) |
| 111 | + event.request.data = event.request.data |
| 112 | + .gsub(/password=[^&]+/i, 'password=[FILTERED]') |
| 113 | + .gsub(/token=[^&]+/i, 'token=[FILTERED]') |
| 114 | + .gsub(/api_key=[^&]+/i, 'api_key=[FILTERED]') |
| 115 | + .gsub(/secret=[^&]+/i, 'secret=[FILTERED]') |
| 116 | + end |
| 117 | + |
| 118 | + event |
| 119 | + end |
| 120 | + |
| 121 | + # Test environment configuration |
| 122 | + if Rails.env.test? |
| 123 | + config.transport.transport_class = Sentry::DummyTransport |
| 124 | + config.background_worker_threads = 0 |
| 125 | + end |
| 126 | + |
| 127 | + # Production optimizations |
| 128 | + if Rails.env.production? |
| 129 | + config.background_worker_threads = 5 |
| 130 | + config.send_client_reports = true |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + Rails.logger.info "Sentry initialized for #{Rails.env} environment" |
| 135 | +else |
| 136 | + Rails.logger.warn "Sentry not initialized - DSN missing or disabled" |
| 137 | +end |
0 commit comments