Skip to content

Commit 0b767ae

Browse files
committed
Refactor error tracker handling, move more ENV vars to settings
1 parent 53ac7b3 commit 0b767ae

File tree

11 files changed

+111
-82
lines changed

11 files changed

+111
-82
lines changed

app/controllers/application_controller.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ class ApplicationController < ActionController::Base
1313
skip_forgery_protection if: :saml_callback_path? # HACK: https://github.com/heartcombo/devise/issues/5210
1414

1515
# before_action :authenticate
16-
before_action :set_sentry_request_context
16+
before_action :set_error_tracker_request_context
1717
before_action :store_employee_location!, if: :storable_location?
1818
before_action :authenticate_employee!
19-
before_action :set_sentry_user_context
19+
before_action :set_error_tracker_user_context
2020
before_action :set_paper_trail_whodunnit
2121
check_authorization unless: :devise_controller?
2222

@@ -71,22 +71,22 @@ def not_found
7171
raise ActionController::RoutingError, 'Not Found'
7272
end
7373

74-
def set_sentry_request_context
75-
commit = ENV.fetch('OPENSHIFT_BUILD_COMMIT', nil)
76-
Sentry.set_tags(commit: ENV.fetch('OPENSHIFT_BUILD_COMMIT', nil)) if commit && ENV['GLITCHTIP_DSN']
74+
def set_error_tracker_request_context
75+
commit = Settings.puzzletime.build.commit
76+
project = Settings.puzzletime.run.project
77+
customer = Settings.puzzletime.run.customer
7778

78-
if (project = ENV.fetch('OPENSHIFT_BUILD_NAMESPACE', nil))
79-
Sentry.set_tags(project: project)
80-
Sentry.set_tags(customer: project.split('-')[0])
81-
end
79+
ErrorTracker.set_tags(commit:) if commit
80+
ErrorTracker.set_tags(project:) if project
81+
ErrorTracker.set_tags(customer:) if customer
8282
end
8383

84-
def set_sentry_user_context
85-
return unless ENV['GLITCHTIP_DSN']
86-
87-
Sentry.set_user(id: current_user.try(:id),
88-
username: current_user.try(:shortname),
89-
email: current_user.email)
84+
def set_error_tracker_user_context
85+
ErrorTracker.set_user(
86+
id: current_user.try(:id),
87+
username: current_user.try(:shortname),
88+
email: current_user.email
89+
)
9090
end
9191

9292
def saml_callback_path?

app/domain/crm/base.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,5 @@ def sync_additional_order(additional); end
5858
def restrict_local?
5959
false
6060
end
61-
62-
def sentry? = ENV['GLITCHTIP_DSN'].present?
63-
def airbrake? = ENV['RAILS_AIRBRAKE_HOST'].present?
6461
end
6562
end

app/domain/crm/highrise.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ def notify_sync_error(error, synced_entity, invalid_record = nil)
173173
parameters = record_to_params(synced_entity, 'synced_entity').tap do |params|
174174
params.merge!(record_to_params(invalid_record, 'invalid_record')) if invalid_record.present?
175175
end
176-
Airbrake.notify(error, parameters) if airbrake?
177-
Sentry.capture_exception(error, extra: parameters) if sentry?
176+
177+
ErrorTracker.report_exception(error, parameters)
178178
end
179179

180180
def record_to_params(record, prefix = 'record')

app/domain/crm/odoo.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,7 @@ def notify_sync_error(error, synced_entity, invalid_record = nil)
239239
#{error.backtrace.join("\n ")}
240240
ERROR
241241

242-
Airbrake.notify(error, parameters) if airbrake?
243-
Sentry.capture_exception(error, extra: parameters) if sentry?
242+
ErrorTracker.report_exception(error, parameters)
244243
end
245244

246245
def false_to_nil(hash)

app/domain/error_tracker.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# frozen_string_literal: true
2+
3+
class ErrorTracker
4+
class << self
5+
def type
6+
Settings.error_tracker.type.to_sym
7+
end
8+
9+
def report_message(message)
10+
Sentry.capture_message(message) if sentry_like?
11+
Airbrake.notify(message) if airbrake_like?
12+
end
13+
14+
def report_exception(error, payload)
15+
Sentry.capture_exception(error, extra: payload) if sentry_like?
16+
Airbrake.notify(error, payload) if airbrake_like?
17+
end
18+
19+
def set_tags(**kwargs)
20+
Sentry.set_tags(**kwargs) if sentry_like?
21+
Airbrake.merge_context(tags: kwargs) if airbrake?
22+
end
23+
24+
def set_extras(**kwargs)
25+
Sentry.set_extras(**kwargs) if sentry_like?
26+
Airbrake.merge_context(extra: kwargs) if airbrake?
27+
end
28+
29+
def set_user(**kwargs)
30+
Sentry.set_user(**kwargs) if sentry_like?
31+
Airbrake.merge_context(user: kwargs) if airbrake?
32+
end
33+
34+
def set_contexts(**)
35+
Sentry.set_contexts(**) if sentry_like?
36+
Airbrake.merge_context(**) if airbrake?
37+
end
38+
39+
def airbrake_like?
40+
%i[airbrake errbit].include? type
41+
end
42+
43+
def sentry_like?
44+
%i[sentry glitchtip].include? type
45+
end
46+
end
47+
end

app/domain/invoicing/small_invoice/client_sync.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,11 @@ def notify_sync_error(error, client = nil)
3333
parameters = client.present? ? record_to_params(client) : {}
3434
parameters[:code] = error.code if error.respond_to?(:code)
3535
parameters[:data] = error.data if error.respond_to?(:data)
36-
Airbrake.notify(error, parameters) if airbrake?
37-
Sentry.capture_exception(error, extra: parameters) if sentry?
38-
end
39-
40-
private
4136

42-
def airbrake?
43-
ENV['RAILS_AIRBRAKE_HOST'].present?
37+
ErrorTracker.report_exception(error, parameters)
4438
end
4539

46-
def sentry?
47-
ENV['GLITCHTIP_DSN'].present?
48-
end
40+
private
4941

5042
def record_to_params(record, prefix = 'client')
5143
{

app/domain/invoicing/small_invoice/invoice_sync.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,8 @@ def notify_sync_error(error, invoice)
4848
parameters = record_to_params(invoice)
4949
parameters[:code] = error.code if error.respond_to?(:code)
5050
parameters[:data] = error.data if error.respond_to?(:data)
51-
Airbrake.notify(error, parameters) if airbrake?
52-
Sentry.capture_exception(error, extra: parameters) if sentry?
53-
end
54-
55-
def airbrake?
56-
ENV['RAILS_AIRBRAKE_HOST'].present?
57-
end
5851

59-
def sentry?
60-
ENV['GLITCHTIP_DSN'].present?
52+
ErrorTracker.report_exception(error, parameters)
6153
end
6254

6355
def record_to_params(record, prefix = 'invoice')

app/jobs/application_job.rb

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,13 @@ class ApplicationJob < ActiveJob::Base
1010
payload = { cgi_data: ENV.to_hash }
1111
payload[:code] = error.code if error.respond_to?(:code)
1212
payload[:data] = error.data if error.respond_to?(:data)
13-
Airbrake.notify(error, payload) if airbrake?
14-
Sentry.capture_exception(error, extra: payload) if sentry?
13+
14+
ErrorTracker.report_exception(error, payload)
1515
end
1616

1717
# Called once by active job before perform_now, after delayed job callbacks
1818
def deserialize(job_data)
1919
super
20-
init_sentry_job_context(job_data) if sentry?
21-
end
22-
23-
def airbrake?
24-
ENV['RAILS_AIRBRAKE_HOST'].present?
25-
end
26-
27-
def sentry?
28-
ENV['GLITCHTIP_DSN'].present?
29-
end
30-
31-
def init_sentry_job_context(job_data)
32-
Sentry.set_extras(active_job: job_data)
20+
ErrorTracker.set_extras(active_job: job_data)
3321
end
3422
end

config/initializers/errbit.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@
55
# or later. See the COPYING file at the top-level directory or at
66
# https://github.com/puzzle/puzzletime.
77

8-
if ENV['RAILS_AIRBRAKE_HOST'] && ENV['RAILS_AIRBRAKE_API_KEY']
8+
require 'error_tracker'
9+
10+
if ErrorTracker.airbrake_like?
911
require 'airbrake'
12+
1013
Airbrake.configure do |config|
1114
config.environment = Rails.env
1215
config.ignore_environments = %i[development test]
1316
# if no host is given, ignore all environments
14-
config.ignore_environments << :production if ENV['RAILS_AIRBRAKE_HOST'].blank?
17+
config.ignore_environments << :production if Settings.error_tracker.airbrake.host.blank?
1518

1619
config.project_id = 1 # required, but any positive integer works
17-
config.project_key = ENV['RAILS_AIRBRAKE_API_KEY']
18-
config.host = ENV['RAILS_AIRBRAKE_HOST']
20+
config.project_key = Settings.error_tracker.airbrake.api_key
21+
config.host = Settings.error_tracker.airbrake.host
1922
config.blacklist_keys << 'pwd'
2023
config.blacklist_keys << 'RAILS_DB_PASSWORD'
2124
config.blacklist_keys << 'RAILS_AIRBRAKE_API_KEY'

config/initializers/sentry.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
# or later. See the COPYING file at the top-level directory or at
66
# https://github.com/puzzle/puzzletime.
77

8-
if ENV['GLITCHTIP_DSN']
8+
require 'error_tracker'
9+
10+
if ErrorTracker.sentry_like?
911
Sentry.init do |config|
10-
config.dsn = ENV['GLITCHTIP_DSN']
12+
config.dsn = Settings.error_tracker.sentry.dsn
1113

1214
# Additionally exclude the following exceptions:
1315
# config.excluded_exceptions += []
@@ -25,11 +27,7 @@
2527
# defaults to development
2628
# config.environment = Rails.env
2729

28-
config.release = if (commit = ENV.fetch('OPENSHIFT_BUILD_COMMIT', nil))
29-
"#{Puzzletime.version}_#{commit}"
30-
else
31-
Puzzletime.version
32-
end
30+
config.release = Settings.puzzletime.run.full_versioon
3331

3432
config.before_send = lambda do |event, _hint|
3533
# filter out parameters filtered by Rails

0 commit comments

Comments
 (0)