newrelic: refactor error logging.
* Factor out New Relic logging to DanbooruLogger class. * Log all exceptions to New Relic, not just statement timeouts.
This commit is contained in:
@@ -77,30 +77,28 @@ class ApplicationController < ActionController::Base
|
||||
|
||||
case exception
|
||||
when ActiveRecord::QueryCanceled
|
||||
if Rails.env.production?
|
||||
NewRelic::Agent.notice_error(exception, :uri => request.original_url, :referer => request.referer, :request_params => params, :custom_params => {:user_id => CurrentUser.user.id, :user_ip_addr => CurrentUser.ip_addr})
|
||||
end
|
||||
|
||||
render_error_page(500, "The database timed out running your query.")
|
||||
render_error_page(500, exception, message: "The database timed out running your query.")
|
||||
when ActiveRecord::RecordNotFound
|
||||
render_error_page(404, "That record was not found")
|
||||
render_error_page(404, exception, message: "That record was not found", expected: true)
|
||||
when ActionController::UnknownFormat
|
||||
@error_message = "#{request.format.to_s} is not a supported format for this page."
|
||||
render "static/error.html", status: 406
|
||||
when Danbooru::Paginator::PaginationError
|
||||
render_error_page(410, @exception.message)
|
||||
render_error_page(410, exception, expected: true)
|
||||
when NotImplementedError
|
||||
render_error_page(501, "This feature isn't available: #{@exception.message}")
|
||||
render_error_page(501, exception, message: "This feature isn't available: #{exception.message}")
|
||||
when PG::ConnectionBad
|
||||
render_error_page(503, "The database is unavailable. Try again later.")
|
||||
render_error_page(503, exception, message: "The database is unavailable. Try again later.")
|
||||
else
|
||||
render_error_page(500, @exception.message)
|
||||
render_error_page(500, exception)
|
||||
end
|
||||
end
|
||||
|
||||
def render_error_page(status, message)
|
||||
def render_error_page(status, exception, message: exception.message, expected: false)
|
||||
@error_message = message
|
||||
|
||||
DanbooruLogger.log(exception, expected: expected)
|
||||
|
||||
if request.format.symbol.in?(%i[html json xml js atom])
|
||||
render template: "static/error", status: status
|
||||
else
|
||||
|
||||
12
app/logical/danbooru_logger.rb
Normal file
12
app/logical/danbooru_logger.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
class DanbooruLogger
|
||||
def self.log(exception, expected: false, **params)
|
||||
if !expected
|
||||
backtrace = Rails.backtrace_cleaner.clean(exception.backtrace).join("\n")
|
||||
Rails.logger.error("#{exception.class}: #{exception.message}\n#{backtrace}")
|
||||
end
|
||||
|
||||
if defined?(::NewRelic)
|
||||
::NewRelic::Agent.notice_error(exception, expected: expected, custom_params: params)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -41,13 +41,7 @@ module Maintenance
|
||||
end
|
||||
|
||||
def rescue_exception(exception)
|
||||
backtrace = Rails.backtrace_cleaner.clean(exception.backtrace).join("\n")
|
||||
Rails.logger.error("#{exception.class}: #{exception.message}\n#{backtrace}")
|
||||
|
||||
if defined?(NewRelic::Agent)
|
||||
NewRelic::Agent.notice_error(exception, custom_params: { backtrace: backtrace })
|
||||
end
|
||||
|
||||
DanbooruLogger.log(exception)
|
||||
raise exception
|
||||
end
|
||||
end
|
||||
|
||||
@@ -72,7 +72,7 @@ module Moderator
|
||||
user.update(blacklisted_tags: repl.join("\n"))
|
||||
end
|
||||
rescue Exception => e
|
||||
NewRelic::Agent.notice_error(e)
|
||||
DanbooruLogger.log(e)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -56,10 +56,7 @@ class PopularSearchService
|
||||
data
|
||||
|
||||
rescue => e
|
||||
Rails.logger.error(e.to_s)
|
||||
if defined?(NewRelic)
|
||||
NewRelic::Agent.notice_error(e)
|
||||
end
|
||||
DanbooruLogger.log(e)
|
||||
return []
|
||||
end
|
||||
end
|
||||
|
||||
@@ -230,9 +230,7 @@ class ApplicationRecord < ActiveRecord::Base
|
||||
connection.execute("SET STATEMENT_TIMEOUT = #{n}") unless Rails.env == "test"
|
||||
yield
|
||||
rescue ::ActiveRecord::StatementInvalid => x
|
||||
if Rails.env.production?
|
||||
NewRelic::Agent.notice_error(x, :custom_params => new_relic_params.merge(:user_id => CurrentUser.id, :user_ip_addr => CurrentUser.ip_addr))
|
||||
end
|
||||
DanbooruLogger.log(x, expected: true)
|
||||
return default_value
|
||||
ensure
|
||||
connection.execute("SET STATEMENT_TIMEOUT = #{CurrentUser.user.try(:statement_timeout) || 3_000}") unless Rails.env == "test"
|
||||
|
||||
@@ -98,11 +98,6 @@ class Tag < ApplicationRecord
|
||||
end
|
||||
|
||||
def increment_post_counts(tag_names)
|
||||
if Rails.env.production? && tag_names.include?("breasts")
|
||||
trace = Kernel.caller.grep(/danbooru/).reject {|x| x =~ /bundle/}.map {|x| x.sub(/\/var\/www\/danbooru2\/releases\/\d+\//, "")}.join("\n").slice(0, 4095)
|
||||
::NewRelic::Agent.record_custom_event("increment_post_counts", user_id: CurrentUser.id, pid: Process.pid, stacktrace: trace, hash: Cache.hash(tag_names))
|
||||
end
|
||||
|
||||
Tag.where(:name => tag_names).update_all("post_count = post_count + 1")
|
||||
end
|
||||
|
||||
|
||||
@@ -99,9 +99,7 @@ class TagAlias < TagRelationship
|
||||
update(status: "error: #{e}")
|
||||
end
|
||||
|
||||
if Rails.env.production?
|
||||
NewRelic::Agent.notice_error(e, :custom_params => {:tag_alias_id => id, :antecedent_name => antecedent_name, :consequent_name => consequent_name})
|
||||
end
|
||||
DanbooruLogger.log(e, tag_alias_id: id, antecedent_name: antecedent_name, consequent_name: consequent_name)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -145,9 +145,7 @@ class TagImplication < TagRelationship
|
||||
forum_updater.update(failure_message(e), "FAILED") if update_topic
|
||||
update(status: "error: #{e}")
|
||||
|
||||
if Rails.env.production?
|
||||
NewRelic::Agent.notice_error(e, :custom_params => {:tag_implication_id => id, :antecedent_name => antecedent_name, :consequent_name => consequent_name})
|
||||
end
|
||||
DanbooruLogger.log(e, tag_implication_id: id, antecedent_name: antecedent_name, consequent_name: consequent_name)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user