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

Add tests for Gitlab::ErrorTracking::LogFormatter

Issue https://gitlab.com/gitlab-com/gl-infra/scalability/-/issues/846
parent 7764adfc
......@@ -61,6 +61,9 @@ module Gitlab
end
def current_context
# In case Gitlab::ErrorTracking is used when the app starts
return {} unless defined?(::Gitlab::ApplicationContext)
::Gitlab::ApplicationContext.current.to_h
end
end
......
......@@ -3,6 +3,9 @@
module Gitlab
module ErrorTracking
class LogFormatter
# Note: all the accesses to Raven's contexts here are to keep the
# backward-compatibility to Sentry's built-in integrations. In future,
# they can be removed.
def self.format!(payload, exception, context_payload)
Gitlab::ExceptionLogFormatter.format!(exception, payload)
append_user_to_log!(payload, context_payload)
......@@ -29,6 +32,7 @@ module Gitlab
extra = extra.except(:server)
sidekiq_extra = extra[:sidekiq]
# The extra value for sidekiq is a hash whose keys are strings.
if sidekiq_extra.is_a?(Hash) && sidekiq_extra.key?('args')
sidekiq_extra = sidekiq_extra.dup
sidekiq_extra['args'] = Gitlab::ErrorTracking::Processor::SidekiqProcessor.loggable_arguments(
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::ErrorTracking::LogFormatter do
let(:exception) { StandardError.new('boom') }
let(:context_payload) do
{
user: {
ip_address: '127.0.0.1',
username: 'root'
},
tags: {
locale: 'en',
feature_category: 'category_a'
},
extra: {
some_other_info: 'other_info',
sidekiq: {
'class' => 'HelloWorker',
'args' => ['senstive string', 1, 2],
'another_field' => 'field'
}
}
}
end
before do
Raven.context.user[:user_flag] = 'flag'
Raven.context.tags[:shard] = 'catchall'
Raven.context.extra[:some_info] = 'info'
allow(exception).to receive(:backtrace).and_return(
[
'lib/gitlab/file_a.rb:1',
'lib/gitlab/file_b.rb:2'
]
)
end
after do
::Raven::Context.clear!
end
it 'appends error-related log fields' do
payload = {}
described_class.format!(payload, exception, context_payload)
expect(payload).to eql(
'exception.class' => 'StandardError',
'exception.message' => 'boom',
'exception.backtrace' => [
'lib/gitlab/file_a.rb:1',
'lib/gitlab/file_b.rb:2'
],
'user.ip_address' => '127.0.0.1',
'user.username' => 'root',
'user.user_flag' => 'flag',
'tags.locale' => 'en',
'tags.feature_category' => 'category_a',
'tags.shard' => 'catchall',
'extra.some_other_info' => 'other_info',
'extra.some_info' => 'info',
"extra.sidekiq" => {
"another_field" => "field",
"args" => ["[FILTERED]", "1", "2"],
"class" => "HelloWorker"
}
)
end
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