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:
evazion
2019-08-08 22:16:39 -05:00
parent 35dfc704bc
commit 9a6add9730
9 changed files with 27 additions and 37 deletions

View File

@@ -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

View 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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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"

View File

@@ -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

View File

@@ -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

View File

@@ -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