Welcome to mirror list, hosted at ThFree Co, Russian Federation.

error_tracking_spec.rb « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c40369f5965857b18da5ccf9c0b59de6d9daa073 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# frozen_string_literal: true

require 'spec_helper'

require 'raven/transports/dummy'

describe Gitlab::ErrorTracking do
  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

    allow(described_class).to receive(:sentry_dsn).and_return(Gitlab.config.sentry.dsn)
    allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return('cid')

    described_class.configure do |config|
      config.encoding = 'json'
    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
        expect(described_class).to receive(:should_raise_for_dev?).and_return(true)
      end

      it 'raises the exception' do
        expect(Raven).to receive(:capture_exception)

        expect { described_class.track_and_raise_for_dev_exception(exception) }
          .to raise_error(RuntimeError)
      end
    end

    context 'when exceptions for dev should not be raised' do
      before do
        expect(described_class).to receive(:should_raise_for_dev?).and_return(false)
      end

      it 'logs the exception with all attributes passed' do
        expected_extras = {
          some_other_info: 'info',
          issue_url: 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1'
        }

        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_and_raise_for_dev_exception(
          exception,
          issue_url: issue_url,
          some_other_info: 'info'
        )
      end

      it 'calls Gitlab::ErrorTracking::Logger.error with formatted payload' do
        expect(Gitlab::ErrorTracking::Logger).to receive(:error)
          .with(a_hash_including(*expected_payload_includes))

        described_class.track_and_raise_for_dev_exception(
          exception,
          issue_url: issue_url,
          some_other_info: 'info'
        )
      end
    end
  end

  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

    it 'calls Gitlab::ErrorTracking::Logger.error with formatted payload' do
      expect(Gitlab::ErrorTracking::Logger).to receive(:error)
        .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)
    end
  end

  describe '.track_exception' do
    it 'calls Raven.capture_exception' do
      expected_extras = {
        some_other_info: 'info',
        issue_url: issue_url
      }

      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_exception(
        exception,
        issue_url: issue_url,
        some_other_info: 'info'
      )
    end

    it 'calls Gitlab::ErrorTracking::Logger.error with formatted payload' do
      expect(Gitlab::ErrorTracking::Logger).to receive(:error)
        .with(a_hash_including(*expected_payload_includes))

      described_class.track_exception(
        exception,
        issue_url: issue_url,
        some_other_info: 'info'
      )
    end

    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

    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, backtrace: caller) }

      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_exception(exception)
      end
    end

    context 'the exception implements :sentry_extra_data, which returns nil' do
      let(:exception) { double(message: 'bang!', sentry_extra_data: nil, backtrace: caller) }

      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_exception(exception, extra_info)
      end
    end

    context 'with sidekiq args' do
      it 'ensures extra.sidekiq.args is a string' do
        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] } }

        expect(Gitlab::ErrorTracking::Logger).to receive(:error).with(
          hash_including('extra.sidekiq' => { 'class' => 'UnknownWorker', 'args' => ['[FILTERED]', '1', '2'] }))

        described_class.track_exception(exception, extra)

        sentry_event = Gitlab::Json.parse(Raven.client.transport.events.last[1])

        expect(sentry_event.dig('extra', 'sidekiq', 'args')).to eq(['[FILTERED]', 1, 2])
      end
    end
  end
end