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
|
case exception
|
||||||
when ActiveRecord::QueryCanceled
|
when ActiveRecord::QueryCanceled
|
||||||
if Rails.env.production?
|
render_error_page(500, exception, message: "The database timed out running your query.")
|
||||||
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.")
|
|
||||||
when ActiveRecord::RecordNotFound
|
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
|
when ActionController::UnknownFormat
|
||||||
@error_message = "#{request.format.to_s} is not a supported format for this page."
|
@error_message = "#{request.format.to_s} is not a supported format for this page."
|
||||||
render "static/error.html", status: 406
|
render "static/error.html", status: 406
|
||||||
when Danbooru::Paginator::PaginationError
|
when Danbooru::Paginator::PaginationError
|
||||||
render_error_page(410, @exception.message)
|
render_error_page(410, exception, expected: true)
|
||||||
when NotImplementedError
|
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
|
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
|
else
|
||||||
render_error_page(500, @exception.message)
|
render_error_page(500, exception)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_error_page(status, message)
|
def render_error_page(status, exception, message: exception.message, expected: false)
|
||||||
@error_message = message
|
@error_message = message
|
||||||
|
|
||||||
|
DanbooruLogger.log(exception, expected: expected)
|
||||||
|
|
||||||
if request.format.symbol.in?(%i[html json xml js atom])
|
if request.format.symbol.in?(%i[html json xml js atom])
|
||||||
render template: "static/error", status: status
|
render template: "static/error", status: status
|
||||||
else
|
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
|
end
|
||||||
|
|
||||||
def rescue_exception(exception)
|
def rescue_exception(exception)
|
||||||
backtrace = Rails.backtrace_cleaner.clean(exception.backtrace).join("\n")
|
DanbooruLogger.log(exception)
|
||||||
Rails.logger.error("#{exception.class}: #{exception.message}\n#{backtrace}")
|
|
||||||
|
|
||||||
if defined?(NewRelic::Agent)
|
|
||||||
NewRelic::Agent.notice_error(exception, custom_params: { backtrace: backtrace })
|
|
||||||
end
|
|
||||||
|
|
||||||
raise exception
|
raise exception
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ module Moderator
|
|||||||
user.update(blacklisted_tags: repl.join("\n"))
|
user.update(blacklisted_tags: repl.join("\n"))
|
||||||
end
|
end
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
NewRelic::Agent.notice_error(e)
|
DanbooruLogger.log(e)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -56,10 +56,7 @@ class PopularSearchService
|
|||||||
data
|
data
|
||||||
|
|
||||||
rescue => e
|
rescue => e
|
||||||
Rails.logger.error(e.to_s)
|
DanbooruLogger.log(e)
|
||||||
if defined?(NewRelic)
|
|
||||||
NewRelic::Agent.notice_error(e)
|
|
||||||
end
|
|
||||||
return []
|
return []
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -230,9 +230,7 @@ class ApplicationRecord < ActiveRecord::Base
|
|||||||
connection.execute("SET STATEMENT_TIMEOUT = #{n}") unless Rails.env == "test"
|
connection.execute("SET STATEMENT_TIMEOUT = #{n}") unless Rails.env == "test"
|
||||||
yield
|
yield
|
||||||
rescue ::ActiveRecord::StatementInvalid => x
|
rescue ::ActiveRecord::StatementInvalid => x
|
||||||
if Rails.env.production?
|
DanbooruLogger.log(x, expected: true)
|
||||||
NewRelic::Agent.notice_error(x, :custom_params => new_relic_params.merge(:user_id => CurrentUser.id, :user_ip_addr => CurrentUser.ip_addr))
|
|
||||||
end
|
|
||||||
return default_value
|
return default_value
|
||||||
ensure
|
ensure
|
||||||
connection.execute("SET STATEMENT_TIMEOUT = #{CurrentUser.user.try(:statement_timeout) || 3_000}") unless Rails.env == "test"
|
connection.execute("SET STATEMENT_TIMEOUT = #{CurrentUser.user.try(:statement_timeout) || 3_000}") unless Rails.env == "test"
|
||||||
|
|||||||
@@ -98,11 +98,6 @@ class Tag < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def increment_post_counts(tag_names)
|
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")
|
Tag.where(:name => tag_names).update_all("post_count = post_count + 1")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -99,9 +99,7 @@ class TagAlias < TagRelationship
|
|||||||
update(status: "error: #{e}")
|
update(status: "error: #{e}")
|
||||||
end
|
end
|
||||||
|
|
||||||
if Rails.env.production?
|
DanbooruLogger.log(e, tag_alias_id: id, antecedent_name: antecedent_name, consequent_name: consequent_name)
|
||||||
NewRelic::Agent.notice_error(e, :custom_params => {:tag_alias_id => id, :antecedent_name => antecedent_name, :consequent_name => consequent_name})
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -145,9 +145,7 @@ class TagImplication < TagRelationship
|
|||||||
forum_updater.update(failure_message(e), "FAILED") if update_topic
|
forum_updater.update(failure_message(e), "FAILED") if update_topic
|
||||||
update(status: "error: #{e}")
|
update(status: "error: #{e}")
|
||||||
|
|
||||||
if Rails.env.production?
|
DanbooruLogger.log(e, tag_implication_id: id, antecedent_name: antecedent_name, consequent_name: consequent_name)
|
||||||
NewRelic::Agent.notice_error(e, :custom_params => {:tag_implication_id => id, :antecedent_name => antecedent_name, :consequent_name => consequent_name})
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user