15219: Logs exceptions with backtraces as part of the JSON log.
authorLucas Di Pentima <ldipentima@veritasgenetics.com>
Fri, 12 Jul 2019 15:27:05 +0000 (12:27 -0300)
committerLucas Di Pentima <ldipentima@veritasgenetics.com>
Fri, 12 Jul 2019 15:27:05 +0000 (12:27 -0300)
Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <ldipentima@veritasgenetics.com>

services/api/app/controllers/application_controller.rb
services/api/config/initializers/lograge.rb

index d5bc3f35d7e04fc47f9795f8a3643f4aef8bcf57..b23515dda4528b1e7f44433a8fcfa006d7ee3486 100644 (file)
@@ -140,8 +140,13 @@ class ApplicationController < ActionController::Base
 
   def render_error(e)
     logger.error e.inspect
-    if !e.is_a? RequestError and (e.respond_to? :backtrace and e.backtrace)
-      logger.error e.backtrace.collect { |x| x + "\n" }.join('')
+    if e.respond_to? :backtrace and e.backtrace
+      # This will be cleared by lograge after adding it to the log.
+      # Usually lograge would get the exceptions, but in our case we're catching
+      # all of them with exception handlers that cannot re-raise them because they
+      # don't get propagated.
+      Thread.current[:exception] = e.inspect
+      Thread.current[:backtrace] = e.backtrace.collect { |x| x + "\n" }.join('')
     end
     if (@object.respond_to? :errors and
         @object.errors.andand.full_messages.andand.any?)
index 07dba3aef4ff9bb64db8c1236a73c0c97c15a794..9b422462b193a400a58375bc31cf76486132d023 100644 (file)
@@ -13,6 +13,21 @@ Server::Application.configure do
       client_ipaddr: event.payload[:client_ipaddr],
       client_auth: event.payload[:client_auth],
     }
+
+    # Lograge adds exceptions not being rescued to event.payload, but we're
+    # catching all errors on ApplicationController so we look for backtraces
+    # elsewhere.
+    if !Thread.current[:backtrace].nil?
+      payload.merge!(
+        {
+          exception: Thread.current[:exception],
+          exception_backtrace: Thread.current[:backtrace],
+        }
+      )
+      Thread.current[:exception] = nil
+      Thread.current[:backtrace] = nil
+    end
+
     exceptions = %w(controller action format id)
     params = event.payload[:params].except(*exceptions)