2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-20 02:53:57 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-12 08:08:56 -04:00
|
|
|
require 'raven/transports/dummy'
|
|
|
|
|
2019-12-16 07:07:43 -05:00
|
|
|
describe Gitlab::ErrorTracking do
|
2019-12-13 07:07:41 -05:00
|
|
|
let(:exception) { RuntimeError.new('boom') }
|
|
|
|
let(:issue_url) { 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1' }
|
|
|
|
|
|
|
|
let(:expected_payload_includes) do
|
|
|
|
[
|
|
|
|
{ 'exception.class' => 'RuntimeError' },
|
|
|
|
{ 'exception.message' => 'boom' },
|
|
|
|
{ 'tags.correlation_id' => 'cid' },
|
|
|
|
{ 'extra.some_other_info' => 'info' },
|
|
|
|
{ 'extra.issue_url' => 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1' }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_sentry_settings
|
2017-07-20 02:53:57 -04:00
|
|
|
|
2019-12-13 07:07:41 -05:00
|
|
|
allow(described_class).to receive(:sentry_dsn).and_return(Gitlab.config.sentry.dsn)
|
|
|
|
allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return('cid')
|
|
|
|
|
2020-06-12 08:08:56 -04:00
|
|
|
described_class.configure do |config|
|
|
|
|
config.encoding = 'json'
|
|
|
|
end
|
2019-12-13 07:07:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.with_context' do
|
|
|
|
it 'adds the expected tags' do
|
|
|
|
described_class.with_context {}
|
2017-07-20 02:53:57 -04:00
|
|
|
|
2018-04-13 12:32:54 -04:00
|
|
|
expect(Raven.tags_context[:locale].to_s).to eq(I18n.locale.to_s)
|
2019-05-10 17:12:33 -04:00
|
|
|
expect(Raven.tags_context[Labkit::Correlation::CorrelationId::LOG_KEY.to_sym].to_s)
|
|
|
|
.to eq('cid')
|
2018-04-13 12:32:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-13 07:07:41 -05:00
|
|
|
describe '.track_and_raise_for_dev_exception' do
|
|
|
|
context 'when exceptions for dev should be raised' do
|
|
|
|
before do
|
|
|
|
expect(described_class).to receive(:should_raise_for_dev?).and_return(true)
|
|
|
|
end
|
2018-04-13 12:32:54 -04:00
|
|
|
|
2019-12-13 07:07:41 -05:00
|
|
|
it 'raises the exception' do
|
|
|
|
expect(Raven).to receive(:capture_exception)
|
2018-04-13 12:32:54 -04:00
|
|
|
|
2019-12-13 07:07:41 -05:00
|
|
|
expect { described_class.track_and_raise_for_dev_exception(exception) }
|
|
|
|
.to raise_error(RuntimeError)
|
|
|
|
end
|
2018-04-13 12:32:54 -04:00
|
|
|
end
|
|
|
|
|
2019-12-13 07:07:41 -05:00
|
|
|
context 'when exceptions for dev should not be raised' do
|
2018-04-13 12:32:54 -04:00
|
|
|
before do
|
2019-12-13 07:07:41 -05:00
|
|
|
expect(described_class).to receive(:should_raise_for_dev?).and_return(false)
|
2018-04-13 12:32:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs the exception with all attributes passed' do
|
|
|
|
expected_extras = {
|
|
|
|
some_other_info: 'info',
|
2019-09-18 10:02:45 -04:00
|
|
|
issue_url: 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1'
|
2018-04-13 12:32:54 -04:00
|
|
|
}
|
|
|
|
|
2018-12-05 15:54:40 -05:00
|
|
|
expected_tags = {
|
|
|
|
correlation_id: 'cid'
|
|
|
|
}
|
|
|
|
|
2018-04-13 12:32:54 -04:00
|
|
|
expect(Raven).to receive(:capture_exception)
|
2018-12-05 15:54:40 -05:00
|
|
|
.with(exception,
|
|
|
|
tags: a_hash_including(expected_tags),
|
|
|
|
extra: a_hash_including(expected_extras))
|
2018-04-13 12:32:54 -04:00
|
|
|
|
2019-12-13 07:07:41 -05:00
|
|
|
described_class.track_and_raise_for_dev_exception(
|
2018-04-13 12:32:54 -04:00
|
|
|
exception,
|
2019-12-13 07:07:41 -05:00
|
|
|
issue_url: issue_url,
|
|
|
|
some_other_info: 'info'
|
2018-04-13 12:32:54 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-12-16 07:07:43 -05:00
|
|
|
it 'calls Gitlab::ErrorTracking::Logger.error with formatted payload' do
|
|
|
|
expect(Gitlab::ErrorTracking::Logger).to receive(:error)
|
2019-12-13 07:07:41 -05:00
|
|
|
.with(a_hash_including(*expected_payload_includes))
|
2018-04-13 12:32:54 -04:00
|
|
|
|
2019-12-13 07:07:41 -05:00
|
|
|
described_class.track_and_raise_for_dev_exception(
|
|
|
|
exception,
|
|
|
|
issue_url: issue_url,
|
|
|
|
some_other_info: 'info'
|
|
|
|
)
|
2018-04-13 12:32:54 -04:00
|
|
|
end
|
2017-07-20 02:53:57 -04:00
|
|
|
end
|
|
|
|
end
|
2018-11-14 06:10:36 -05:00
|
|
|
|
2019-12-13 07:07:41 -05:00
|
|
|
describe '.track_and_raise_exception' do
|
|
|
|
it 'always raises the exception' do
|
|
|
|
expect(Raven).to receive(:capture_exception)
|
|
|
|
|
|
|
|
expect { described_class.track_and_raise_exception(exception) }
|
|
|
|
.to raise_error(RuntimeError)
|
|
|
|
end
|
2018-11-14 06:10:36 -05:00
|
|
|
|
2019-12-16 07:07:43 -05:00
|
|
|
it 'calls Gitlab::ErrorTracking::Logger.error with formatted payload' do
|
|
|
|
expect(Gitlab::ErrorTracking::Logger).to receive(:error)
|
2019-12-13 07:07:41 -05:00
|
|
|
.with(a_hash_including(*expected_payload_includes))
|
|
|
|
|
|
|
|
expect do
|
|
|
|
described_class.track_and_raise_exception(
|
|
|
|
exception,
|
|
|
|
issue_url: issue_url,
|
|
|
|
some_other_info: 'info'
|
|
|
|
)
|
|
|
|
end.to raise_error(RuntimeError)
|
2018-11-14 06:10:36 -05:00
|
|
|
end
|
2019-12-13 07:07:41 -05:00
|
|
|
end
|
2018-11-14 06:10:36 -05:00
|
|
|
|
2019-12-13 07:07:41 -05:00
|
|
|
describe '.track_exception' do
|
2018-11-14 06:10:36 -05:00
|
|
|
it 'calls Raven.capture_exception' do
|
|
|
|
expected_extras = {
|
|
|
|
some_other_info: 'info',
|
2019-08-19 19:06:21 -04:00
|
|
|
issue_url: issue_url
|
2018-11-14 06:10:36 -05:00
|
|
|
}
|
|
|
|
|
2018-12-05 15:54:40 -05:00
|
|
|
expected_tags = {
|
|
|
|
correlation_id: 'cid'
|
|
|
|
}
|
|
|
|
|
2018-11-14 06:10:36 -05:00
|
|
|
expect(Raven).to receive(:capture_exception)
|
2018-12-05 15:54:40 -05:00
|
|
|
.with(exception,
|
|
|
|
tags: a_hash_including(expected_tags),
|
|
|
|
extra: a_hash_including(expected_extras))
|
2018-11-14 06:10:36 -05:00
|
|
|
|
2019-12-13 07:07:41 -05:00
|
|
|
described_class.track_exception(
|
|
|
|
exception,
|
|
|
|
issue_url: issue_url,
|
|
|
|
some_other_info: 'info'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-12-16 07:07:43 -05:00
|
|
|
it 'calls Gitlab::ErrorTracking::Logger.error with formatted payload' do
|
|
|
|
expect(Gitlab::ErrorTracking::Logger).to receive(:error)
|
2019-12-13 07:07:41 -05:00
|
|
|
.with(a_hash_including(*expected_payload_includes))
|
|
|
|
|
|
|
|
described_class.track_exception(
|
2018-11-14 06:10:36 -05:00
|
|
|
exception,
|
2019-08-19 19:06:21 -04:00
|
|
|
issue_url: issue_url,
|
2019-12-13 07:07:41 -05:00
|
|
|
some_other_info: 'info'
|
2018-11-14 06:10:36 -05:00
|
|
|
)
|
|
|
|
end
|
2020-02-07 10:09:52 -05:00
|
|
|
|
|
|
|
context 'with filterable parameters' do
|
|
|
|
let(:extra) { { test: 1, my_token: 'test' } }
|
|
|
|
|
|
|
|
it 'filters parameters' do
|
|
|
|
expect(Gitlab::ErrorTracking::Logger).to receive(:error).with(
|
|
|
|
hash_including({ 'extra.test' => 1, 'extra.my_token' => '[FILTERED]' }))
|
|
|
|
|
|
|
|
described_class.track_exception(exception, extra)
|
|
|
|
end
|
|
|
|
end
|
2019-08-19 19:06:21 -04:00
|
|
|
|
|
|
|
context 'the exception implements :sentry_extra_data' do
|
|
|
|
let(:extra_info) { { event: 'explosion', size: :massive } }
|
2019-12-13 07:07:41 -05:00
|
|
|
let(:exception) { double(message: 'bang!', sentry_extra_data: extra_info, backtrace: caller) }
|
2019-08-19 19:06:21 -04:00
|
|
|
|
|
|
|
it 'includes the extra data from the exception in the tracking information' do
|
|
|
|
expect(Raven).to receive(:capture_exception)
|
|
|
|
.with(exception, a_hash_including(extra: a_hash_including(extra_info)))
|
|
|
|
|
2019-12-13 07:07:41 -05:00
|
|
|
described_class.track_exception(exception)
|
2019-08-19 19:06:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'the exception implements :sentry_extra_data, which returns nil' do
|
2019-12-13 07:07:41 -05:00
|
|
|
let(:exception) { double(message: 'bang!', sentry_extra_data: nil, backtrace: caller) }
|
2019-08-19 19:06:21 -04:00
|
|
|
|
|
|
|
it 'just includes the other extra info' do
|
|
|
|
extra_info = { issue_url: issue_url }
|
|
|
|
expect(Raven).to receive(:capture_exception)
|
|
|
|
.with(exception, a_hash_including(extra: a_hash_including(extra_info)))
|
|
|
|
|
2019-12-13 07:07:41 -05:00
|
|
|
described_class.track_exception(exception, extra_info)
|
2019-08-19 19:06:21 -04:00
|
|
|
end
|
|
|
|
end
|
2020-05-29 11:08:14 -04:00
|
|
|
|
|
|
|
context 'with sidekiq args' do
|
|
|
|
it 'ensures extra.sidekiq.args is a string' do
|
2020-06-12 08:08:56 -04:00
|
|
|
extra = { sidekiq: { 'class' => 'PostReceive', 'args' => [1, { 'id' => 2, 'name' => 'hello' }, 'some-value', 'another-value'] } }
|
|
|
|
|
|
|
|
expect(Gitlab::ErrorTracking::Logger).to receive(:error).with(
|
|
|
|
hash_including({ 'extra.sidekiq' => { 'class' => 'PostReceive', 'args' => ['1', '{"id"=>2, "name"=>"hello"}', 'some-value', 'another-value'] } }))
|
|
|
|
|
|
|
|
described_class.track_exception(exception, extra)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters sensitive arguments before sending' do
|
|
|
|
extra = { sidekiq: { 'class' => 'UnknownWorker', 'args' => ['sensitive string', 1, 2] } }
|
|
|
|
|
2020-05-29 11:08:14 -04:00
|
|
|
expect(Gitlab::ErrorTracking::Logger).to receive(:error).with(
|
2020-06-12 08:08:56 -04:00
|
|
|
hash_including('extra.sidekiq' => { 'class' => 'UnknownWorker', 'args' => ['[FILTERED]', '1', '2'] }))
|
2020-05-29 11:08:14 -04:00
|
|
|
|
|
|
|
described_class.track_exception(exception, extra)
|
2020-06-12 08:08:56 -04:00
|
|
|
|
|
|
|
sentry_event = Gitlab::Json.parse(Raven.client.transport.events.last[1])
|
|
|
|
|
|
|
|
expect(sentry_event.dig('extra', 'sidekiq', 'args')).to eq(['[FILTERED]', 1, 2])
|
2020-05-29 11:08:14 -04:00
|
|
|
end
|
|
|
|
end
|
2018-11-14 06:10:36 -05:00
|
|
|
end
|
2017-07-20 02:53:57 -04:00
|
|
|
end
|