debian-mirror-gitlab/lib/gitlab/exception_log_formatter.rb

48 lines
1.2 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
module Gitlab
module ExceptionLogFormatter
2022-01-26 12:08:38 +05:30
class << self
def format!(exception, payload)
return unless exception
2019-12-26 22:10:19 +05:30
2022-01-26 12:08:38 +05:30
# Elasticsearch/Fluentd don't handle nested structures well.
# Use periods to flatten the fields.
payload.merge!(
'exception.class' => exception.class.name,
2022-04-01 21:47:47 +05:30
'exception.message' => sanitize_message(exception)
2022-01-26 12:08:38 +05:30
)
2019-12-26 22:10:19 +05:30
2022-01-26 12:08:38 +05:30
if exception.backtrace
payload['exception.backtrace'] = Rails.backtrace_cleaner.clean(exception.backtrace)
end
if sql = find_sql(exception)
payload['exception.sql'] = sql
end
end
def find_sql(exception)
if exception.is_a?(ActiveRecord::StatementInvalid)
# StatementInvalid may be caused by a statement timeout or a bad query
normalize_query(exception.sql.to_s)
elsif exception.cause.present?
find_sql(exception.cause)
end
end
private
def normalize_query(sql)
PgQuery.normalize(sql)
rescue PgQuery::ParseError
sql
2019-12-26 22:10:19 +05:30
end
2022-04-01 21:47:47 +05:30
def sanitize_message(exception)
Gitlab::Sanitizers::ExceptionMessage.clean(exception.class.name, exception.message)
end
2019-12-26 22:10:19 +05:30
end
end
end