Commit dc99ae29 authored by Quang-Minh Nguyen's avatar Quang-Minh Nguyen

Create a dedicated logger for Gitlab::ErrorTracking

Issue https://gitlab.com/gitlab-com/gl-infra/scalability/-/issues/846
parent 44de5652
......@@ -101,37 +101,19 @@ module Gitlab
end
def process_exception(exception, sentry: false, logging: true, extra:)
exception.try(:sentry_extra_data)&.tap do |data|
extra = extra.merge(data) if data.is_a?(Hash)
end
extra = sanitize_request_parameters(extra)
extra = build_exception_extra(exception, extra)
if sentry && Raven.configuration.server
Raven.capture_exception(exception, extra: extra)
end
if logging
# TODO: this logic could migrate into `Gitlab::ExceptionLogFormatter`
# and we could also flatten deep nested hashes if required for search
# (e.g. if `extra` includes hash of hashes).
# In the current implementation, we don't flatten multi-level folded hashes.
log_hash = {}
Raven.context.tags.each { |name, value| log_hash["tags.#{name}"] = value }
Raven.context.user.each { |name, value| log_hash["user.#{name}"] = value }
Raven.context.extra.merge(extra).each { |name, value| log_hash["extra.#{name}"] = value }
Gitlab::ExceptionLogFormatter.format!(exception, log_hash)
Gitlab::ErrorTracking::LogFormatter.format!(exception, extra, log_hash)
Gitlab::ErrorTracking::Logger.error(log_hash)
end
end
def sanitize_request_parameters(parameters)
filter = ActiveSupport::ParameterFilter.new(::Rails.application.config.filter_parameters)
filter.filter(parameters)
end
def sentry_dsn
return unless Rails.env.production? || Rails.env.development?
return unless Gitlab.config.sentry.enabled
......@@ -159,6 +141,21 @@ module Gitlab
inject_context_for_exception(event, ex.cause) if ex.cause.present?
end
end
def build_exception_extra(exception, extra)
inline_extra = exception.try(:sentry_extra_data)
if inline_extra.present? && inline_extra.is_a?(Hash)
extra = extra.merge(inline_extra)
end
extra = sanitize_request_parameters(extra)
extra
end
def sanitize_request_parameters(parameters)
filter = ActiveSupport::ParameterFilter.new(::Rails.application.config.filter_parameters)
filter.filter(parameters)
end
end
end
end
# frozen_string_literal: true
module Gitlab
module ErrorTracking
class LogFormatter
def self.format!(exception, extra, payload)
Gitlab::ExceptionLogFormatter.format!(exception, payload)
append_context_to_log!(payload)
append_extra_to_log!(extra, payload)
end
def self.append_context_to_log!(payload)
Raven.context.tags.each do |key, value|
payload["tags.#{key}"] = value
end
Raven.context.user.each do |key, value|
payload["user.#{key}"] = value
end
current_context = ::Gitlab::ApplicationContext.current
payload.merge!(
'user.username' => current_context['meta.user'],
'tags.program' => Gitlab.process_name,
'tags.locale' => I18n.locale,
'tags.feature_category' => current_context['meta.feature_category']
)
payload
end
def self.append_extra_to_log!(extra, payload)
extra = Raven.context.extra.merge(extra).except(:server)
sidekiq_extra = extra[:sidekiq]
if sidekiq_extra.is_a?(Hash) && sidekiq_extra.key?('args')
sidekiq_extra = sidekiq_extra.dup
sidekiq_extra['args'] = Gitlab::ErrorTracking::Processor::SidekiqProcessor.loggable_arguments(
value['args'], value['class']
)
end
extra[:sidekiq] = sidekiq_extra if sidekiq_extra
extra.each do |key, value|
payload["extra.#{key}"] = value
end
payload
end
end
end
end
......@@ -12,16 +12,6 @@ module Gitlab
'exception.message' => exception.message
)
payload.delete('extra.server')
payload['extra.sidekiq'].tap do |value|
if value.is_a?(Hash) && value.key?('args')
value = value.dup
payload['extra.sidekiq']['args'] = Gitlab::ErrorTracking::Processor::SidekiqProcessor
.loggable_arguments(value['args'], value['class'])
end
end
if exception.backtrace
payload['exception.backtrace'] = Rails.backtrace_cleaner.clean(exception.backtrace)
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment