2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::Sentry do
|
|
|
|
describe '.context' do
|
2019-09-04 21:01:54 +05:30
|
|
|
it 'adds the expected tags' do
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(described_class).to receive(:enabled?).and_return(true)
|
2019-09-04 21:01:54 +05:30
|
|
|
allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return('cid')
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
described_class.context(nil)
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
expect(Raven.tags_context[:locale].to_s).to eq(I18n.locale.to_s)
|
2019-09-04 21:01:54 +05:30
|
|
|
expect(Raven.tags_context[Labkit::Correlation::CorrelationId::LOG_KEY.to_sym].to_s)
|
|
|
|
.to eq('cid')
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.track_exception' do
|
|
|
|
let(:exception) { RuntimeError.new('boom') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:enabled?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises the exception if it should' do
|
2019-02-15 15:39:39 +05:30
|
|
|
expect(described_class).to receive(:should_raise_for_dev?).and_return(true)
|
2018-10-15 14:42:47 +05:30
|
|
|
expect { described_class.track_exception(exception) }
|
|
|
|
.to raise_error(RuntimeError)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when exceptions should not be raised' do
|
|
|
|
before do
|
2019-02-15 15:39:39 +05:30
|
|
|
allow(described_class).to receive(:should_raise_for_dev?).and_return(false)
|
2019-07-31 22:56:46 +05:30
|
|
|
allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return('cid')
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs the exception with all attributes passed' do
|
|
|
|
expected_extras = {
|
|
|
|
some_other_info: 'info',
|
2019-12-04 20:38:33 +05:30
|
|
|
issue_url: 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1'
|
2018-10-15 14:42:47 +05:30
|
|
|
}
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
expected_tags = {
|
|
|
|
correlation_id: 'cid'
|
|
|
|
}
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
expect(Raven).to receive(:capture_exception)
|
2019-02-15 15:39:39 +05:30
|
|
|
.with(exception,
|
|
|
|
tags: a_hash_including(expected_tags),
|
|
|
|
extra: a_hash_including(expected_extras))
|
2018-10-15 14:42:47 +05:30
|
|
|
|
|
|
|
described_class.track_exception(
|
|
|
|
exception,
|
2019-12-04 20:38:33 +05:30
|
|
|
issue_url: 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1',
|
2018-10-15 14:42:47 +05:30
|
|
|
extra: { some_other_info: 'info' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the context' do
|
|
|
|
expect(described_class).to receive(:context)
|
|
|
|
|
|
|
|
described_class.track_exception(exception)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
context '.track_acceptable_exception' do
|
|
|
|
let(:exception) { RuntimeError.new('boom') }
|
2019-12-04 20:38:33 +05:30
|
|
|
let(:issue_url) { 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1' }
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:enabled?).and_return(true)
|
2019-07-31 22:56:46 +05:30
|
|
|
allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return('cid')
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls Raven.capture_exception' do
|
|
|
|
expected_extras = {
|
|
|
|
some_other_info: 'info',
|
2019-12-04 20:38:33 +05:30
|
|
|
issue_url: issue_url
|
2019-02-15 15:39:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
expected_tags = {
|
|
|
|
correlation_id: 'cid'
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(Raven).to receive(:capture_exception)
|
|
|
|
.with(exception,
|
|
|
|
tags: a_hash_including(expected_tags),
|
|
|
|
extra: a_hash_including(expected_extras))
|
|
|
|
|
|
|
|
described_class.track_acceptable_exception(
|
|
|
|
exception,
|
2019-12-04 20:38:33 +05:30
|
|
|
issue_url: issue_url,
|
2019-02-15 15:39:39 +05:30
|
|
|
extra: { some_other_info: 'info' }
|
|
|
|
)
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
context 'the exception implements :sentry_extra_data' do
|
|
|
|
let(:extra_info) { { event: 'explosion', size: :massive } }
|
|
|
|
let(:exception) { double(message: 'bang!', sentry_extra_data: extra_info) }
|
|
|
|
|
|
|
|
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)))
|
|
|
|
|
|
|
|
described_class.track_acceptable_exception(exception)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'the exception implements :sentry_extra_data, which returns nil' do
|
|
|
|
let(:exception) { double(message: 'bang!', sentry_extra_data: nil) }
|
|
|
|
|
|
|
|
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)))
|
|
|
|
|
|
|
|
described_class.track_acceptable_exception(exception, extra_info)
|
|
|
|
end
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|