Commit 6e0b367c authored by Quang-Minh Nguyen's avatar Quang-Minh Nguyen

Write tests for ApplicationContextProcessor

Issue https://gitlab.com/gitlab-com/gl-infra/scalability/-/issues/846
parent 97a94ae7
......@@ -82,14 +82,6 @@ module Gitlab
user&.username
end
def user_id
user&.id
end
def user_email
user&.email
end
def root_namespace_path
if namespace
namespace.full_path_components.first
......
# frozen_string_literal: true
require 'spec_helper'
require 'rspec-parameterized'
RSpec.describe Gitlab::ErrorTracking::Processor::ApplicationContextProcessor do
subject(:processor) { described_class.new }
before do
allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return('cid')
allow(I18n).to receive(:locale).and_return('en')
end
context 'user metadata' do
let(:user) { create(:user) }
context 'when event user metadata was not set' do
it 'appends username to the event metadata' do
event = {}
Gitlab::ApplicationContext.with_context(user: user) do
processor.process(event)
end
expect(event[:user]).to eql(
username: user.username
)
end
end
context 'when event user metadata was already set' do
it 'appends username to the event metadata' do
event = {
user: {
ip_address: '127.0.0.1'
}
}
Gitlab::ApplicationContext.with_context(user: user) do
processor.process(event)
end
expect(event[:user]).to eql(
ip_address: '127.0.0.1',
username: user.username
)
end
end
end
context 'tags metadata' do
context 'when the GITLAB_SENTRY_EXTRA_TAGS env is not set' do
before do
stub_env('GITLAB_SENTRY_EXTRA_TAGS', nil)
end
it 'does not log into AppLogger' do
expect(Gitlab::AppLogger).not_to receive(:debug)
end
it 'does not send any extra tags' do
event = {}
Gitlab::ApplicationContext.with_context(feature_category: 'feature_a') do
processor.process(event)
end
expect(event[:tags]).to eql(
correlation_id: 'cid',
locale: 'en',
program: 'test',
feature_category: 'feature_a'
)
end
end
context 'when the GITLAB_SENTRY_EXTRA_TAGS env is a JSON hash' do
it 'includes those tags in all events' do
stub_env('GITLAB_SENTRY_EXTRA_TAGS', { foo: 'bar', baz: 'quux' }.to_json)
event = {}
Gitlab::ApplicationContext.with_context(feature_category: 'feature_a') do
processor.process(event)
end
expect(event[:tags]).to eql(
correlation_id: 'cid',
locale: 'en',
program: 'test',
feature_category: 'feature_a',
'foo' => 'bar',
'baz' => 'quux'
)
end
it 'does not log into AppLogger' do
expect(Gitlab::AppLogger).not_to receive(:debug)
end
end
context 'when the GITLAB_SENTRY_EXTRA_TAGS env is not a JSON hash' do
using RSpec::Parameterized::TableSyntax
where(:env_var, :error) do
{ foo: 'bar', baz: 'quux' }.inspect | 'JSON::ParserError'
[].to_json | 'NoMethodError'
[%w[foo bar]].to_json | 'NoMethodError'
%w[foo bar].to_json | 'NoMethodError'
'"string"' | 'NoMethodError'
end
with_them do
before do
stub_env('GITLAB_SENTRY_EXTRA_TAGS', env_var)
end
it 'logs into AppLogger' do
expect(Gitlab::AppLogger).to receive(:debug).with(a_string_matching(error))
processor.process({})
end
it 'does not include any extra tags' do
event = {}
Gitlab::ApplicationContext.with_context(feature_category: 'feature_a') do
processor.process(event)
end
expect(event[:tags]).to eql(
correlation_id: 'cid',
locale: 'en',
program: 'test',
feature_category: 'feature_a'
)
end
end
end
end
end
......@@ -31,96 +31,6 @@ RSpec.describe Gitlab::ErrorTracking do
end
end
describe '.configure' do
context 'default tags from GITLAB_SENTRY_EXTRA_TAGS' do
context 'when the value is a JSON hash' do
it 'includes those tags in all events' do
stub_env('GITLAB_SENTRY_EXTRA_TAGS', { foo: 'bar', baz: 'quux' }.to_json)
described_class.configure do |config|
config.encoding = 'json'
end
described_class.track_exception(StandardError.new)
expect(sentry_event['tags'].except('correlation_id', 'locale', 'program'))
.to eq('foo' => 'bar', 'baz' => 'quux')
end
end
context 'when the value is not set' do
before do
stub_env('GITLAB_SENTRY_EXTRA_TAGS', nil)
end
it 'does not log an error' do
expect(Gitlab::AppLogger).not_to receive(:debug)
described_class.configure do |config|
config.encoding = 'json'
end
end
it 'does not send any extra tags' do
described_class.configure do |config|
config.encoding = 'json'
end
described_class.track_exception(StandardError.new)
expect(sentry_event['tags'].keys).to contain_exactly('correlation_id', 'locale', 'program')
end
end
context 'when the value is not a JSON hash' do
using RSpec::Parameterized::TableSyntax
where(:env_var, :error) do
{ foo: 'bar', baz: 'quux' }.inspect | 'JSON::ParserError'
[].to_json | 'NoMethodError'
[%w[foo bar]].to_json | 'NoMethodError'
%w[foo bar].to_json | 'NoMethodError'
'"string"' | 'NoMethodError'
end
with_them do
before do
stub_env('GITLAB_SENTRY_EXTRA_TAGS', env_var)
end
it 'does not include any extra tags' do
described_class.configure do |config|
config.encoding = 'json'
end
described_class.track_exception(StandardError.new)
expect(sentry_event['tags'].except('correlation_id', 'locale', 'program'))
.to be_empty
end
it 'logs the error class' do
expect(Gitlab::AppLogger).to receive(:debug).with(a_string_matching(error))
described_class.configure do |config|
config.encoding = 'json'
end
end
end
end
end
end
describe '.with_context' do
it 'adds the expected tags' do
described_class.with_context {}
expect(Raven.tags_context[:locale].to_s).to eq(I18n.locale.to_s)
expect(Raven.tags_context[Labkit::Correlation::CorrelationId::LOG_KEY.to_sym].to_s)
.to eq('cid')
end
end
describe '.track_and_raise_for_dev_exception' do
context 'when exceptions for dev should be raised' do
before do
......
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