diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-12-20 16:37:47 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-12-20 16:37:47 +0300 |
commit | aee0a117a889461ce8ced6fcf73207fe017f1d99 (patch) | |
tree | 891d9ef189227a8445d83f35c1b0fc99573f4380 /lib/gitlab/exception_log_formatter.rb | |
parent | 8d46af3258650d305f53b819eabf7ab18d22f59e (diff) |
Add latest changes from gitlab-org/gitlab@14-6-stable-eev14.6.0-rc42
Diffstat (limited to 'lib/gitlab/exception_log_formatter.rb')
-rw-r--r-- | lib/gitlab/exception_log_formatter.rb | 43 |
1 files changed, 33 insertions, 10 deletions
diff --git a/lib/gitlab/exception_log_formatter.rb b/lib/gitlab/exception_log_formatter.rb index 9898651c9e3..315574fed31 100644 --- a/lib/gitlab/exception_log_formatter.rb +++ b/lib/gitlab/exception_log_formatter.rb @@ -2,18 +2,41 @@ module Gitlab module ExceptionLogFormatter - def self.format!(exception, payload) - return unless exception + class << self + def format!(exception, payload) + return unless exception - # Elasticsearch/Fluentd don't handle nested structures well. - # Use periods to flatten the fields. - payload.merge!( - 'exception.class' => exception.class.name, - 'exception.message' => exception.message - ) + # Elasticsearch/Fluentd don't handle nested structures well. + # Use periods to flatten the fields. + payload.merge!( + 'exception.class' => exception.class.name, + 'exception.message' => exception.message + ) - if exception.backtrace - payload['exception.backtrace'] = Rails.backtrace_cleaner.clean(exception.backtrace) + 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 end end end |