Skip to content

Commit

Permalink
Move logging to separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
TaiWilkin committed Mar 26, 2024
1 parent 3813e17 commit 7409019
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
34 changes: 9 additions & 25 deletions app/controllers/v0/pension_claims_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# frozen_string_literal: true

require 'pension_21p527ez/tag_sentry'
require 'pension_21p527ez/monitor'

module V0
class PensionClaimsController < ClaimsBaseController
service_tag 'pension-application'

MONITOR = ::Pension21p527ez::Monitor.new

def short_name
'pension_claim'
end
Expand All @@ -25,42 +28,38 @@ def show
end
render(json: response)
rescue ActiveRecord::RecordNotFound => e
Rails.logger.error('21P-527EZ submission not found',
{ confirmation_number: params[:id], user_uuid: current_user&.uuid, errors: e.message })
MONITOR.track_show404(params[:id], current_user, e)
render(json: { error: e.to_s }, status: :not_found)
rescue => e
Rails.logger.error('Fetch 21P-527EZ submission failed',
{ confirmation_number: params[:id], user_uuid: current_user&.uuid, errors: e.message })
MONITOR.track_show_error(params[:id], current_user, e)
raise e
end

# Creates and validates an instance of the class, removing any copies of
# the form that had been previously saved by the user.
def create
StatsD.increment("#{stats_key}.attempt")
Pension21p527ez::TagSentry.tag_sentry

claim = claim_class.new(form: filtered_params[:form])
user_uuid = current_user&.uuid
Rails.logger.info("Begin #{claim.class::FORM} Submission", { guid: claim.guid, user_uuid: })
MONITOR.track_create_attempt(claim, current_user)

in_progress_form = current_user ? InProgressForm.form_for_user(claim.form_id, current_user) : nil
claim.itf_datetime = in_progress_form.created_at if in_progress_form

unless claim.save
track_create_error(in_progress_form, claim)
MONITOR.track_create_error(in_progress_form, claim, current_user)
log_validation_error_to_metadata(in_progress_form, claim)
raise Common::Exceptions::ValidationErrors, claim.errors
end

claim.upload_to_lighthouse

track_create_success(in_progress_form, claim)
MONITOR.track_create_success(in_progress_form, claim, current_user)

clear_saved_form(claim.form_id)
render(json: claim)
rescue => e
track_create_error(in_progress_form, claim, e)
MONITOR.track_create_error(in_progress_form, claim, current_user, e)
raise e
end

Expand Down Expand Up @@ -90,20 +89,5 @@ def format_show_response(claim, state, form_submission, submission_attempt)
}
}
end

def track_create_error(in_progress_form, claim, e = nil)
Rails.logger.error('21P-527EZ submission to Sidekiq failed',
{ confirmation_number: claim&.confirmation_number, user_uuid: current_user&.uuid,
in_progress_form_id: in_progress_form&.id, errors: claim&.errors&.errors,
message: e&.message })
StatsD.increment("#{stats_key}.failure")
end

def track_create_success(in_progress_form, claim)
Rails.logger.info('21P-527EZ submission to Sidekiq success',
{ confirmation_number: claim&.confirmation_number, user_uuid: current_user&.uuid,
in_progress_form_id: in_progress_form&.id })
StatsD.increment("#{stats_key}.success")
end
end
end
38 changes: 38 additions & 0 deletions lib/pension_21p527ez/monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

module Pension21p527ez
class Monitor
STATS_KEY = 'api.pension_claim'

def track_show404(confirmation_number, current_user, e)
Rails.logger.error('21P-527EZ submission not found',
{ confirmation_number:, user_uuid: current_user&.uuid, message: e&.message })
end

def track_show_error(confirmation_number, current_user, e)
Rails.logger.error('21P-527EZ fetching submission failed',
{ confirmation_number:, user_uuid: current_user&.uuid, message: e&.message })
end

def track_create_attempt(claim, current_user)
StatsD.increment("#{STATS_KEY}.attempt")
Rails.logger.info('21P-527EZ submission to Sidekiq begun',
{ confirmation_number: claim&.confirmation_number, user_uuid: current_user&.uuid })
end

def track_create_error(in_progress_form, claim, current_user, e = nil)
StatsD.increment("#{STATS_KEY}.failure")
Rails.logger.error('21P-527EZ submission to Sidekiq failed',
{ confirmation_number: claim&.confirmation_number, user_uuid: current_user&.uuid,
in_progress_form_id: in_progress_form&.id, errors: claim&.errors&.errors,
message: e&.message })
end

def track_create_success(in_progress_form, claim, current_user)
StatsD.increment("#{STATS_KEY}.success")
Rails.logger.info('21P-527EZ submission to Sidekiq success',
{ confirmation_number: claim&.confirmation_number, user_uuid: current_user&.uuid,
in_progress_form_id: in_progress_form&.id })
end
end
end
4 changes: 2 additions & 2 deletions spec/requests/pension_claims_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
end

it 'logs the attempted submission' do
expect(Rails.logger).to receive(:info).with('Begin 21P-527EZ Submission', be_a(Hash))
expect(Rails.logger).to receive(:info).with('21P-527EZ submission to Sidekiq begun', be_a(Hash))
expect(Rails.logger).to receive(:error).with('Validation error.')
subject
end
Expand All @@ -68,7 +68,7 @@
end

it 'logs the successful submission' do
expect(Rails.logger).to receive(:info).with('Begin 21P-527EZ Submission', be_a(Hash))
expect(Rails.logger).to receive(:info).with('21P-527EZ submission to Sidekiq begun', be_a(Hash))
expect(Rails.logger).to receive(:info).with('21P-527EZ submission to Sidekiq success', be_a(Hash))
subject
end
Expand Down

0 comments on commit 7409019

Please sign in to comment.