From a504f83d9c55db12e128b77f4ac7a18c8ba52ed0 Mon Sep 17 00:00:00 2001 From: Nichelous Herndon Date: Mon, 30 Dec 2024 15:01:21 -0600 Subject: [PATCH] trying out datadog logging and tests --- Gemfile.lock | 16 ------- .../virtual_agent_appeal_controller.rb | 10 +++- .../virtual_agent_claim_status_controller.rb | 10 ++++ .../virtual_agent_speech_token_controller.rb | 5 ++ .../v0/virtual_agent_token_controller.rb | 5 ++ .../v0/virtual_agent_token_msft_controller.rb | 5 ++ .../v0/virtual_agent_token_nlu_controller.rb | 5 ++ .../virtual_agent_appeal_controller_spec.rb | 48 +++++++++++++++++++ 8 files changed, 87 insertions(+), 17 deletions(-) create mode 100644 spec/controllers/v0/virtual_agent/virtual_agent_appeal_controller_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index 1e0c1808816..2deaae5e22b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -139,18 +139,6 @@ PATH veteran_confirmation (0.0.1) vye (0.1.0) -GEM - remote: https://enterprise.contribsys.com/ - specs: - sidekiq-ent (7.2.4) - einhorn (~> 1.0) - gserver - sidekiq (>= 7.2.0, < 8) - sidekiq-pro (>= 7.2.0, < 8) - sidekiq-pro (7.2.1) - base64 - sidekiq (>= 7.2.0, < 8) - GEM remote: https://rubygems.org/ specs: @@ -426,7 +414,6 @@ GEM dry-initializer (~> 3.0) dry-schema (>= 1.12, < 2) zeitwerk (~> 2.6) - einhorn (1.0.0) erubi (1.13.0) et-orbi (1.2.11) tzinfo @@ -550,7 +537,6 @@ GEM multi_json (~> 1.11) os (>= 0.9, < 2.0) signet (>= 0.16, < 2.a) - gserver (0.0.1) guard (2.18.1) formatador (>= 0.2.4) listen (>= 2.7, < 4.0) @@ -1313,8 +1299,6 @@ DEPENDENCIES shoulda-matchers shrine sidekiq (~> 7.2.0) - sidekiq-ent! - sidekiq-pro! sign_in_service simple_forms_api! simplecov diff --git a/app/controllers/v0/virtual_agent/virtual_agent_appeal_controller.rb b/app/controllers/v0/virtual_agent/virtual_agent_appeal_controller.rb index 5f613a5a158..a983bea420f 100644 --- a/app/controllers/v0/virtual_agent/virtual_agent_appeal_controller.rb +++ b/app/controllers/v0/virtual_agent/virtual_agent_appeal_controller.rb @@ -165,6 +165,10 @@ def get_status_type_text(appeal_status, aoj) appeal_status_description = 'Unknown Status' unknown_status_error = StandardError.new("Unknown status: #{appeal_status} with AOJ: #{aoj}") log_exception_to_sentry(unknown_status_error, { appeal_status => appeal_status, aoj => aoj }) + # also log to datadog + ::Rails.logger.error( + message: unknown_status_error.message + ) end if appeal_status_description.include? '{aoj_desc}' @@ -191,8 +195,12 @@ def set_user_credentials def service_exception_handler(exception) context = 'An error occurred while attempting to retrieve the appeal(s)' + # also log to datadog + datadog_message = { message: "#{context}: #{exception.message}" } + datadog_message[:backtrace] = exception.backtrace if exception.backtrace + ::Rails.logger.error(datadog_message) log_exception_to_sentry(exception, 'context' => context) - render nothing: true, status: :internal_server_error + head :internal_server_error end end end diff --git a/app/controllers/v0/virtual_agent/virtual_agent_claim_status_controller.rb b/app/controllers/v0/virtual_agent/virtual_agent_claim_status_controller.rb index 2f7b0477ab0..c9de6752c0a 100644 --- a/app/controllers/v0/virtual_agent/virtual_agent_claim_status_controller.rb +++ b/app/controllers/v0/virtual_agent/virtual_agent_claim_status_controller.rb @@ -80,11 +80,21 @@ def service_exception_handler(exception) context = 'An error occurred while attempting to retrieve the claim(s).' log_exception_to_sentry(exception, 'context' => context) render nothing: true, status: :service_unavailable + # also log to datadog + ::Rails.logger.error( + message: context + ": #{exception.message}", + backtrace: exception.backtrace + ) end def report_exception_handler(exception) context = 'An error occurred while attempting to report the claim(s).' log_exception_to_sentry(exception, 'context' => context) + # also log to datadog + ::Rails.logger.error( + message: context + ": #{exception.message}", + backtrace: exception.backtrace + ) end class ServiceException < RuntimeError; end diff --git a/app/controllers/v0/virtual_agent_speech_token_controller.rb b/app/controllers/v0/virtual_agent_speech_token_controller.rb index 132ee27bb34..e6f6075b9ca 100644 --- a/app/controllers/v0/virtual_agent_speech_token_controller.rb +++ b/app/controllers/v0/virtual_agent_speech_token_controller.rb @@ -38,6 +38,11 @@ def service_exception_handler(exception) context = 'An error occurred with the Microsoft service that issues chatbot tokens' log_exception_to_sentry(exception, 'context' => context) render nothing: true, status: :service_unavailable + # also log to datadog + ::Rails.logger.error( + message: context + ": #{exception.message}", + backtrace: exception.backtrace + ) end class ServiceException < RuntimeError; end diff --git a/app/controllers/v0/virtual_agent_token_controller.rb b/app/controllers/v0/virtual_agent_token_controller.rb index 0529f14e9a8..26453f0a790 100644 --- a/app/controllers/v0/virtual_agent_token_controller.rb +++ b/app/controllers/v0/virtual_agent_token_controller.rb @@ -63,6 +63,11 @@ def service_exception_handler(exception) context = 'An error occurred with the Microsoft service that issues chatbot tokens' log_exception_to_sentry(exception, 'context' => context) render nothing: true, status: :service_unavailable + # also log to datadog + ::Rails.logger.error( + message: context + ": #{exception.message}", + backtrace: exception.backtrace + ) end class ServiceException < RuntimeError; end diff --git a/app/controllers/v0/virtual_agent_token_msft_controller.rb b/app/controllers/v0/virtual_agent_token_msft_controller.rb index e52440b51d2..1c70c5b2805 100644 --- a/app/controllers/v0/virtual_agent_token_msft_controller.rb +++ b/app/controllers/v0/virtual_agent_token_msft_controller.rb @@ -57,6 +57,11 @@ def service_exception_handler(exception) context = 'An error occurred with the Microsoft service that issues chatbot tokens' log_exception_to_sentry(exception, 'context' => context) render nothing: true, status: :service_unavailable + # also log to datadog + ::Rails.logger.error( + message: context + ": #{exception.message}", + backtrace: exception.backtrace + ) end class ServiceException < RuntimeError; end diff --git a/app/controllers/v0/virtual_agent_token_nlu_controller.rb b/app/controllers/v0/virtual_agent_token_nlu_controller.rb index 93d6aa2fc28..8fd780219fe 100644 --- a/app/controllers/v0/virtual_agent_token_nlu_controller.rb +++ b/app/controllers/v0/virtual_agent_token_nlu_controller.rb @@ -57,6 +57,11 @@ def service_exception_handler(exception) context = 'An error occurred with the Microsoft service that issues chatbot tokens' log_exception_to_sentry(exception, 'context' => context) render nothing: true, status: :service_unavailable + # also log to datadog + ::Rails.logger.error( + message: context + ": #{exception.message}", + backtrace: exception.backtrace + ) end class ServiceException < RuntimeError; end diff --git a/spec/controllers/v0/virtual_agent/virtual_agent_appeal_controller_spec.rb b/spec/controllers/v0/virtual_agent/virtual_agent_appeal_controller_spec.rb new file mode 100644 index 00000000000..216aca9ffe6 --- /dev/null +++ b/spec/controllers/v0/virtual_agent/virtual_agent_appeal_controller_spec.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe V0::VirtualAgent::VirtualAgentAppealController, type: :controller do + # let(:exception) { StandardError.new('Test error') } + # let(:context) { 'An error occurred while attempting to retrieve the appeal(s)' } + + describe '#service_exception_handler' do + let(:exception) { StandardError.new('An error occurred') } + + it 'calls service_exception_handler with the exception' do + allow(Rails.logger).to receive(:error) + expect(controller).to receive(:service_exception_handler).with(exception) + expect(Rails.logger).to have_received(:error) + controller.send(:service_exception_handler, exception) + end + end + + # before do + # # Simulate a request to ensure the response object is available + # # allow(controller).to receive(:head).with(:internal_server_error) + + # get :index + # rescue + # nil + # end + + # describe '#service_exception_handler' do + # it 'logs the error to Rails logger' do + # # expect(Rails.logger).to receive(:error).with( + # # exception.message + # # ) + + # controller.send(:service_exception_handler, exception) + + # expect(Rails.logger).to receive(:error).with( + # hash_including(message: a_string_including('Test error')) + # ) + # end + + # # it 'renders internal server error' do + # # expect(controller).to receive(:head).with(:internal_server_error) + + # # controller.send(:service_exception_handler, exception) + # # end + # end +end