diff --git a/Gemfile b/Gemfile index 9563f44..ec00c16 100644 --- a/Gemfile +++ b/Gemfile @@ -57,6 +57,10 @@ group :production do gem "figaro", "~> 1.2" gem "sidekiq", "~> 6.0" gem "whenever", require: false + + gem "sentry-rails" + gem "sentry-ruby" + gem "sentry-sidekiq" end group :test do diff --git a/Gemfile.lock b/Gemfile.lock index bfdcf49..2e7b3cb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -791,6 +791,14 @@ GEM rexml (~> 3.2, >= 3.2.5) rubyzip (>= 1.2.2) semantic_range (3.0.0) + sentry-rails (5.16.1) + railties (>= 5.0) + sentry-ruby (~> 5.16.1) + sentry-ruby (5.16.1) + concurrent-ruby (~> 1.0, >= 1.0.2) + sentry-sidekiq (5.16.1) + sentry-ruby (~> 5.16.1) + sidekiq (>= 3.0) seven_zip_ruby (1.3.0) sidekiq (6.5.9) connection_pool (>= 2.2.5, < 3) @@ -927,6 +935,9 @@ DEPENDENCIES rubocop-faker rubocop-rails rubocop-rspec + sentry-rails + sentry-ruby + sentry-sidekiq sidekiq (~> 6.0) spring (~> 2.0) spring-watcher-listen (~> 2.0) diff --git a/app/controllers/decidim_controller.rb b/app/controllers/decidim_controller.rb index 85919c0..1cefe1c 100644 --- a/app/controllers/decidim_controller.rb +++ b/app/controllers/decidim_controller.rb @@ -4,4 +4,12 @@ # entry point, but you can change what controller it inherits from # so you can customize some methods. class DecidimController < ApplicationController + before_action :set_sentry_context + + def set_sentry_context + return unless Rails.application.secrets.sentry_enabled? + + Sentry.set_user({ id: try(:current_user).try(:id) }.merge(session)) + Sentry.set_extras(params: params.to_unsafe_h, url: request.url) + end end diff --git a/config/initializers/sentry.rb b/config/initializers/sentry.rb new file mode 100644 index 0000000..1d9c440 --- /dev/null +++ b/config/initializers/sentry.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +if Rails.application.secrets.sentry_enabled + Sentry.init do |config| + config.dsn = ENV.fetch("SENTRY_DSN") + config.breadcrumbs_logger = [:active_support_logger, :http_logger] + + # Set traces_sample_rate to 1.0 to capture 100% + # of transactions for performance monitoring. + # We recommend adjusting this value in production. + config.traces_sample_rate = ENV.fetch("SENTRY_TRACES_SAMPLE_RATE", "0.5").to_f + end +end diff --git a/config/secrets.yml b/config/secrets.yml index 02e5254..251abf0 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -158,6 +158,7 @@ default: &default development: <<: *default + sentry_enabled: false secret_key_base: <%= ENV["SECRET_KEY_BASE"].presence || "417e4b74122188de90a9ec2551d2d2c52eb9d18f09684605b876d7772b3295b58f810c1de2b3131c0689d38454aac60c765cb4c1c1b9fa89fb9e028f460726f1" %> omniauth: developer: @@ -189,6 +190,7 @@ test: # instead read values from the environment. production: <<: *default + sentry_enabled: true secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> smtp_username: <%= ENV["SMTP_USERNAME"] %> smtp_password: <%= ENV["SMTP_PASSWORD"] %>