Compare commits

...
Author SHA1 Message Date
Muhsin KelothandGitHub 9b32748c8b Merge branch 'develop' into fix/error_reporting 2025-09-25 13:46:34 +05:30
Muhsin KelothandGitHub 0403361a36 Merge branch 'develop' into fix/error_reporting 2025-03-26 09:30:29 +05:30
Vishnu Narayanan 9bd93f970c fix: specs 2025-03-25 17:19:00 +05:30
Vishnu Narayanan 2e6142c4c3 fix: refactor 2025-03-25 01:13:17 +05:30
Vishnu Narayanan f32daa96ab fix: improve apm error reporting
Previously, errors were only being logged with logger.info and rendered
as JSON responses, but weren't being properly reported to APM services.
This meant that critical errors like 500s were only visible in application
logs but not in our monitoring systems, making it harder to track and debug
issues in production.

This commit ensures that all errors are properly reported to configured
APM services by:
1. Adding explicit APM error reporting alongside logging
2. Using proper APM reporting methods for each service
3. Maintaining consistent error handling across all APM integrations

These changes ensure that errors are properly captured in our monitoring
systems,improving our ability to track and debug production issues.
2025-03-25 00:36:40 +05:30
2 changed files with 28 additions and 6 deletions
@@ -23,24 +23,29 @@ module RequestExceptionHandler
Current.reset Current.reset
end end
def render_error(message, status, error = nil)
log_handled_error(error) if error
render json: { error: message }, status: status
end
def render_unauthorized(message) def render_unauthorized(message)
render json: { error: message }, status: :unauthorized render_error(message, :unauthorized)
end end
def render_not_found_error(message) def render_not_found_error(message)
render json: { error: message }, status: :not_found render_error(message, :not_found)
end end
def render_could_not_create_error(message) def render_could_not_create_error(message)
render json: { error: message }, status: :unprocessable_entity render_error(message, :unprocessable_entity)
end end
def render_payment_required(message) def render_payment_required(message)
render json: { error: message }, status: :payment_required render_error(message, :payment_required)
end end
def render_internal_server_error(message) def render_internal_server_error(message)
render json: { error: message }, status: :internal_server_error render_error(message, :internal_server_error)
end end
def render_record_invalid(exception) def render_record_invalid(exception)
@@ -57,6 +62,23 @@ module RequestExceptionHandler
end end
def log_handled_error(exception) def log_handled_error(exception)
return unless exception
logger.info("Handled error: #{exception.inspect}") logger.info("Handled error: #{exception.inspect}")
report_to_apms(exception)
end
def report_to_apms(exception)
apm_reporters = {
'NewRelic::Agent' => -> { ::NewRelic::Agent.notice_error(exception) },
'Datadog::Tracing' => -> { ::Datadog::Tracing.active_trace&.set_error(exception) },
'ElasticAPM' => -> { ::ElasticAPM.report(exception) },
'ScoutApm::Error' => -> { ::ScoutApm::Error.capture(exception) },
'Sentry' => -> { ::Sentry.capture_exception(exception) }
}
apm_reporters.each do |module_name, reporter|
reporter.call if Object.const_defined?(module_name)
end
end end
end end
@@ -440,7 +440,7 @@ RSpec.describe 'Contacts API', type: :request do
as: :json as: :json
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:unprocessable_entity)
expect(response.body).to include('Invalid value. The values provided for country_code are invalid"') expect(response.body).to include('Invalid value. The values provided for country_code are invalid')
end end
end end
end end