Skip to content

Commit aa70bd2

Browse files
committed
Add Sentry for error tracking
1 parent cd001d8 commit aa70bd2

6 files changed

Lines changed: 46 additions & 0 deletions

File tree

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ gem 'reverse_markdown'
5050
gem 'rss'
5151
gem 'sass-rails'
5252
gem 'sassc-rails'
53+
gem 'sentry-rails'
54+
gem 'sentry-ruby'
55+
gem 'sentry-sidekiq'
5356
gem 'sidekiq'
5457
gem 'sidekiq-status'
5558
gem 'simple_form'

Gemfile.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,14 @@ GEM
614614
sprockets-rails
615615
tilt
616616
scanf (1.0.0)
617+
sentry-rails (5.11.0)
618+
railties (>= 5.0)
619+
sentry-ruby (~> 5.11.0)
620+
sentry-ruby (5.11.0)
621+
concurrent-ruby (~> 1.0, >= 1.0.2)
622+
sentry-sidekiq (5.11.0)
623+
sentry-ruby (~> 5.11.0)
624+
sidekiq (>= 3.0)
617625
shacl (0.3.0)
618626
json-ld (~> 3.2)
619627
rdf (~> 3.2, >= 3.2.8)
@@ -817,6 +825,9 @@ DEPENDENCIES
817825
rubocop
818826
sass-rails
819827
sassc-rails
828+
sentry-rails
829+
sentry-ruby
830+
sentry-sidekiq
820831
sidekiq
821832
sidekiq-status
822833
simple_form

app/controllers/application_controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ def user_not_authorized(exception)
107107

108108
def set_current_user
109109
User.current_user = current_user
110+
if TeSS::Config.sentry_enabled?
111+
Sentry.set_user(current_user ? { id: current_user.id, username: current_user.username } : {})
112+
end
110113
end
111114

112115
protected

config/application.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ def analytics_enabled
9494
def map_enabled
9595
!feature['disabled'].include?('events_map') && Rails.application.secrets.google_maps_api_key.present?
9696
end
97+
98+
def _sentry_dsn
99+
ENV.fetch('SENTRY_DSN') { sentry_dsn }
100+
end
101+
102+
def sentry_enabled?
103+
_sentry_dsn.present? && Rails.env.production?
104+
end
97105
end
98106

99107
Config = TessConfig.new(tess_config)

config/initializers/sentry.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Rails.configuration.after_initialize do
2+
if TeSS::Config.sentry_enabled?
3+
Sentry.init do |config|
4+
config.dsn = TeSS::Config._sentry_dsn
5+
config.breadcrumbs_logger = [:active_support_logger, :http_logger]
6+
config.excluded_exceptions += ['ActionController::RoutingError', 'ActiveRecord::RecordNotFound']
7+
filter = ActiveSupport::ParameterFilter.new(Rails.application.config.filter_parameters)
8+
config.before_send = lambda do |event, hint|
9+
filter.filter(event.to_hash)
10+
end
11+
config.release = `git rev-parse --short HEAD`&.chomp("\n")
12+
end
13+
Sentry.configure_scope do |scope|
14+
scope.set_context('app', {
15+
app_name: TeSS::Config.site['title_short'],
16+
app_version: APP_VERSION
17+
})
18+
end
19+
end
20+
end

config/tess.example.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ default: &default
1515
require_cookie_consent: true
1616
blocked_domains:
1717
- !ruby/regexp '/bad-domain\.example/'
18+
sentry_dsn:
1819
site:
1920
title: 'TeSS (Training eSupport System)'
2021
title_short: TeSS

0 commit comments

Comments
 (0)