From 2ff3444568ebb86278c03803ff16f217bcca1ccb Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 8 Dec 2017 08:30:48 +0800 Subject: [PATCH] Rescue from errors when trying to append custom payloads to lograge output. --- config/initializers/100-lograge.rb | 67 ++++++++++++++++-------------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/config/initializers/100-lograge.rb b/config/initializers/100-lograge.rb index 91a3ff430de..36416683ae9 100644 --- a/config/initializers/100-lograge.rb +++ b/config/initializers/100-lograge.rb @@ -8,46 +8,51 @@ if (Rails.env.production? && SiteSetting.logging_provider == 'lograge') || ENV[" Rails.application.configure do config.lograge.enabled = true - # Seeing RuntimeError (Missing rack.input) - # for: `/topics/timings` - # - # - # config.lograge.custom_payload do |controller| - # username = controller.try(:current_user)&.username rescue nil - # { - # ip: controller.request.remote_ip, - # username: username, - # } - # end + config.lograge.custom_payload do |controller| + begin + { + ip: controller.request.remote_ip, + username: controller.current_user&.username, + } + rescue => e + Rails.logger.warn("Failed to append custom payload: #{e.message}\n#{e.backtrace.join("\n")}") + {} + end + end config.lograge.custom_options = lambda do |event| - exceptions = %w(controller action format id) + begin + exceptions = %w(controller action format id) - params = event.payload[:params].except(*exceptions) - params[:files].map!(&:headers) if params[:files] + params = event.payload[:params].except(*exceptions) + params[:files].map!(&:headers) if params[:files] - output = { - params: params.to_query, - database: RailsMultisite::ConnectionManagement.current_db, - } + output = { + params: params.to_query, + database: RailsMultisite::ConnectionManagement.current_db, + } - if data = Thread.current[:_method_profiler] - sql = data[:sql] + if data = Thread.current[:_method_profiler] + sql = data[:sql] - if sql - output[:db] = sql[:duration] * 1000 - output[:db_calls] = sql[:calls] + if sql + output[:db] = sql[:duration] * 1000 + output[:db_calls] = sql[:calls] + end + + redis = data[:redis] + + if redis + output[:redis] = redis[:duration] * 1000 + output[:redis_calls] = redis[:calls] + end end - redis = data[:redis] - - if redis - output[:redis] = redis[:duration] * 1000 - output[:redis_calls] = redis[:calls] - end + output + rescue => e + Rails.logger.warn("Failed to append custom options: #{e.message}\n#{e.backtrace.join("\n")}") + {} end - - output end if ENV["LOGSTASH_URI"]