-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
transport_spec.rb
517 lines (417 loc) · 16.6 KB
/
transport_spec.rb
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
require 'spec_helper'
RSpec.describe Sentry::Transport do
let(:io) { StringIO.new }
let(:logger) { Logger.new(io) }
let(:configuration) do
Sentry::Configuration.new.tap do |config|
config.server = 'http://12345:67890@sentry.localdomain/sentry/42'
config.logger = logger
end
end
let(:client) { Sentry::Client.new(configuration) }
let(:hub) do
Sentry::Hub.new(client, Sentry::Scope.new)
end
let(:dynamic_sampling_context) do
{
"sample_rate" => "0.01337",
"public_key" => "49d0f7386ad645858ae85020e393bef3",
"trace_id" => "771a43a4192642f0b136d5159a501700",
"user_id" => "Amélie"
}
end
subject { client.transport }
describe "#serialize_envelope" do
context "normal event" do
let(:event) do
event = client.event_from_exception(ZeroDivisionError.new("divided by 0"))
event.dynamic_sampling_context = dynamic_sampling_context
event
end
let(:envelope) { subject.envelope_from_event(event) }
it "generates correct envelope content" do
result, _ = subject.serialize_envelope(envelope)
envelope_header, item_header, item = result.split("\n")
envelope_header_parsed = JSON.parse(envelope_header)
expect(envelope_header_parsed).to eq({
"event_id" => event.event_id,
"dsn" => Sentry::TestHelper::DUMMY_DSN,
"sdk" => Sentry.sdk_meta,
"sent_at" => Time.now.utc.iso8601,
"trace" => dynamic_sampling_context
})
expect(item_header).to eq(
'{"type":"event","content_type":"application/json"}'
)
expect(item).to eq(event.to_hash.to_json)
end
end
context "transaction event" do
let(:transaction) do
Sentry::Transaction.new(name: "test transaction", op: "rack.request", hub: hub)
end
let(:event) do
event = client.event_from_transaction(transaction)
event.dynamic_sampling_context = dynamic_sampling_context
event
end
let(:envelope) { subject.envelope_from_event(event) }
it "generates correct envelope content" do
result, _ = subject.serialize_envelope(envelope)
envelope_header, item_header, item = result.split("\n")
envelope_header_parsed = JSON.parse(envelope_header)
expect(envelope_header_parsed).to eq({
"event_id" => event.event_id,
"dsn" => Sentry::TestHelper::DUMMY_DSN,
"sdk" => Sentry.sdk_meta,
"sent_at" => Time.now.utc.iso8601,
"trace" => dynamic_sampling_context
})
expect(item_header).to eq(
'{"type":"transaction","content_type":"application/json"}'
)
expect(item).to eq(event.to_hash.to_json)
end
context "with profiling on transaction" do
let(:profile) do
frames = [
{ function: "foo" },
{ function: "bar" },
{ function: "baz" }
]
stacks = [
[0, 1],
[0, 2],
[1, 2],
[0, 1, 2]
]
samples = [
{ stack_id: 0, elapsed_since_start_ns: 10000 },
{ stack_id: 0, elapsed_since_start_ns: 20000 },
{ stack_id: 1, elapsed_since_start_ns: 30000 },
{ stack_id: 2, elapsed_since_start_ns: 40000 },
{ stack_id: 3, elapsed_since_start_ns: 50000 }
]
{
environment: "test",
release: "release",
profile: {
frames: frames,
stacks: stacks,
samples: samples
}
}
end
let(:event_with_profile) do
event.profile = profile
event
end
let(:envelope) { subject.envelope_from_event(event_with_profile) }
it "adds profile item to envelope" do
result, _ = subject.serialize_envelope(envelope)
profile_header, profile_payload = result.split("\n").last(2)
expect(profile_header).to eq(
'{"type":"profile","content_type":"application/json"}'
)
expect(profile_payload).to eq(profile.to_json)
end
end
end
context "client report" do
let(:event) { client.event_from_exception(ZeroDivisionError.new("divided by 0")) }
let(:envelope) { subject.envelope_from_event(event) }
before do
5.times { subject.record_lost_event(:ratelimit_backoff, 'error') }
3.times { subject.record_lost_event(:queue_overflow, 'transaction') }
end
it "incudes client report in envelope" do
Timecop.travel(Time.now + 90) do
result, _ = subject.serialize_envelope(envelope)
client_report_header, client_report_payload = result.split("\n").last(2)
expect(client_report_header).to eq(
'{"type":"client_report"}'
)
expect(client_report_payload).to eq(
{
timestamp: Time.now.utc.iso8601,
discarded_events: [
{ reason: :ratelimit_backoff, category: 'error', quantity: 5 },
{ reason: :queue_overflow, category: 'transaction', quantity: 3 }
]
}.to_json
)
end
end
end
context "metrics/statsd item" do
let(:payload) do
"foo@none:10.0|c|#tag1:42,tag2:bar|T1709042970\n" +
"bar@second:0.3:0.1:0.9:49.8:100|g|#|T1709042980"
end
let(:envelope) do
envelope = Sentry::Envelope.new
envelope.add_item(
{ type: 'statsd', length: payload.bytesize },
payload
)
envelope
end
it "adds raw payload to envelope item" do
result, _ = subject.serialize_envelope(envelope)
item = result.split("\n", 2).last
item_header, item_payload = item.split("\n", 2)
expect(JSON.parse(item_header)).to eq({ 'type' => 'statsd', 'length' => 93 })
expect(item_payload).to eq(payload)
end
end
context "oversized event" do
context "due to breadcrumb" do
let(:event) { client.event_from_message("foo") }
let(:envelope) { subject.envelope_from_event(event) }
before do
event.breadcrumbs = Sentry::BreadcrumbBuffer.new(1000)
1000.times do |i|
event.breadcrumbs.record Sentry::Breadcrumb.new(category: i.to_s, message: "x" * Sentry::Event::MAX_MESSAGE_SIZE_IN_BYTES)
end
serialized_result = JSON.generate(event.to_hash)
expect(serialized_result.bytesize).to be > Sentry::Envelope::Item::MAX_SERIALIZED_PAYLOAD_SIZE
end
it "removes breadcrumbs and carry on" do
data, _ = subject.serialize_envelope(envelope)
expect(data.bytesize).to be < Sentry::Envelope::Item::MAX_SERIALIZED_PAYLOAD_SIZE
expect(envelope.items.count).to eq(1)
event_item = envelope.items.first
expect(event_item.payload[:breadcrumbs]).to be_nil
end
context "if it's still oversized" do
before do
1000.times do |i|
event.contexts["context_#{i}"] = "s" * Sentry::Event::MAX_MESSAGE_SIZE_IN_BYTES
end
end
it "rejects the item and logs attributes size breakdown" do
data, _ = subject.serialize_envelope(envelope)
expect(data).to be_nil
expect(io.string).not_to match(/Sending envelope with items \[event\]/)
expect(io.string).to match(/tags: 2, contexts: 8208891, extra: 2/)
end
end
end
context "due to stacktrace frames" do
let(:event) { client.event_from_exception(SystemStackError.new("stack level too deep")) }
let(:envelope) { subject.envelope_from_event(event) }
let(:in_app_pattern) do
project_root = "/fake/project_root"
Regexp.new("^(#{project_root}/)?#{Sentry::Backtrace::APP_DIRS_PATTERN}")
end
let(:frame_list_limit) { 500 }
let(:frame_list_size) { frame_list_limit * 20 }
before do
single_exception = event.exception.values[0]
new_stacktrace = Sentry::StacktraceInterface.new(
frames: frame_list_size.times.map do |zero_based_index|
Sentry::StacktraceInterface::Frame.new(
"/fake/path",
Sentry::Backtrace::Line.parse("app.rb:#{zero_based_index + 1}:in `/'", in_app_pattern)
)
end,
)
single_exception.instance_variable_set(:@stacktrace, new_stacktrace)
serialized_result = JSON.generate(event.to_hash)
expect(serialized_result.bytesize).to be > Sentry::Envelope::Item::MAX_SERIALIZED_PAYLOAD_SIZE
end
it "keeps some stacktrace frames and carry on" do
data, _ = subject.serialize_envelope(envelope)
expect(data.bytesize).to be < Sentry::Envelope::Item::MAX_SERIALIZED_PAYLOAD_SIZE
expect(envelope.items.count).to eq(1)
event_item = envelope.items.first
frames = event_item.payload[:exception][:values][0][:stacktrace][:frames]
expect(frames.length).to eq(frame_list_limit)
# Last N lines kept
# N = Frame limit / 2
expect(frames[-1][:lineno]).to eq(frame_list_size)
expect(frames[-1][:filename]).to eq('app.rb')
expect(frames[-1][:function]).to eq('/')
#
expect(frames[-(frame_list_limit / 2)][:lineno]).to eq(frame_list_size - ((frame_list_limit / 2) - 1))
expect(frames[-(frame_list_limit / 2)][:filename]).to eq('app.rb')
expect(frames[-(frame_list_limit / 2)][:function]).to eq('/')
# First N lines kept
# N = Frame limit / 2
expect(frames[0][:lineno]).to eq(1)
expect(frames[0][:filename]).to eq('app.rb')
expect(frames[0][:function]).to eq('/')
expect(frames[(frame_list_limit / 2) - 1][:lineno]).to eq(frame_list_limit / 2)
expect(frames[(frame_list_limit / 2) - 1][:filename]).to eq('app.rb')
expect(frames[(frame_list_limit / 2) - 1][:function]).to eq('/')
end
context "if it's still oversized" do
before do
1000.times do |i|
event.contexts["context_#{i}"] = "s" * Sentry::Event::MAX_MESSAGE_SIZE_IN_BYTES
end
end
it "rejects the item and logs attributes size breakdown" do
data, _ = subject.serialize_envelope(envelope)
expect(data).to be_nil
expect(io.string).not_to match(/Sending envelope with items \[event\]/)
expect(io.string).to match(/tags: 2, contexts: 8208891, extra: 2/)
end
end
end
end
end
describe "#send_envelope" do
context "normal event" do
let(:event) { client.event_from_exception(ZeroDivisionError.new("divided by 0")) }
let(:envelope) { subject.envelope_from_event(event) }
it "sends the event and logs the action" do
expect(subject).to receive(:send_data)
subject.send_envelope(envelope)
expect(io.string).to match(/Sending envelope with items \[event\]/)
end
end
context "transaction event" do
let(:transaction) do
Sentry::Transaction.new(name: "test transaction", op: "rack.request", hub: hub)
end
let(:event) { client.event_from_transaction(transaction) }
let(:envelope) { subject.envelope_from_event(event) }
it "sends the event and logs the action" do
expect(subject).to receive(:send_data)
subject.send_envelope(envelope)
expect(io.string).to match(/Sending envelope with items \[transaction\]/)
end
end
context "client report" do
let(:event) { client.event_from_exception(ZeroDivisionError.new("divided by 0")) }
let(:envelope) { subject.envelope_from_event(event) }
before do
5.times { subject.record_lost_event(:ratelimit_backoff, 'error') }
3.times { subject.record_lost_event(:queue_overflow, 'transaction') }
end
it "sends the event and logs the action" do
Timecop.travel(Time.now + 90) do
expect(subject).to receive(:send_data)
subject.send_envelope(envelope)
expect(io.string).to match(/Sending envelope with items \[event, client_report\]/)
end
end
end
context "oversized event" do
let(:event) { client.event_from_message("foo") }
let(:envelope) { subject.envelope_from_event(event) }
before do
event.breadcrumbs = Sentry::BreadcrumbBuffer.new(1000)
1000.times do |i|
event.breadcrumbs.record Sentry::Breadcrumb.new(category: i.to_s, message: "x" * Sentry::Event::MAX_MESSAGE_SIZE_IN_BYTES)
end
serialized_result = JSON.generate(event.to_hash)
expect(serialized_result.bytesize).to be > Sentry::Envelope::Item::MAX_SERIALIZED_PAYLOAD_SIZE
end
it "deletes the event's breadcrumbs and sends it" do
expect(subject).to receive(:send_data)
subject.send_envelope(envelope)
expect(io.string).to match(/Sending envelope with items \[event\]/)
end
context "when the event hash has string keys" do
let(:envelope) { subject.envelope_from_event(event.to_json_compatible) }
it "deletes the event's breadcrumbs and sends it" do
expect(subject).to receive(:send_data)
subject.send_envelope(envelope)
expect(io.string).to match(/Sending envelope with items \[event\]/)
end
end
context "if it's still oversized" do
before do
1000.times do |i|
event.contexts["context_#{i}"] = "s" * Sentry::Event::MAX_MESSAGE_SIZE_IN_BYTES
end
end
it "rejects the event item and doesn't send the envelope" do
expect(subject).not_to receive(:send_data)
subject.send_envelope(envelope)
expect(io.string).to match(/tags: 2, contexts: 8208891, extra: 2/)
expect(io.string).not_to match(/Sending envelope with items \[event\]/)
end
context "with other types of items" do
before do
5.times { subject.record_lost_event(:ratelimit_backoff, 'error') }
3.times { subject.record_lost_event(:queue_overflow, 'transaction') }
end
it "excludes oversized event and sends the rest" do
Timecop.travel(Time.now + 90) do
expect(subject).to receive(:send_data)
subject.send_envelope(envelope)
expect(io.string).to match(/Sending envelope with items \[client_report\]/)
end
end
end
end
end
end
describe "#send_event" do
let(:client) { Sentry::Client.new(configuration) }
let(:event) { client.event_from_exception(ZeroDivisionError.new("divided by 0")) }
context "when success" do
before do
allow(subject).to receive(:send_data)
end
it "sends Event object" do
expect(subject).not_to receive(:failed_send)
expect(subject.send_event(event)).to eq(event)
end
it "sends Event hash" do
expect(subject).not_to receive(:failed_send)
expect(subject.send_event(event.to_json_compatible)).to eq(event.to_json_compatible)
end
it "logs correct message" do
expect(subject.send_event(event)).to eq(event)
expect(io.string).to match(
/INFO -- sentry: \[Transport\] Sending envelope with items \[event\] #{event.event_id} to Sentry/
)
end
end
context "when failed" do
context "with normal error" do
before do
allow(subject).to receive(:send_data).and_raise(StandardError)
end
it "raises the error" do
expect do
subject.send_event(event)
end.to raise_error(StandardError)
end
end
context "with Faraday::Error" do
it "raises the error" do
expect do
subject.send_event(event)
end.to raise_error(Sentry::ExternalError)
end
end
end
context "when rate limited" do
before do
allow(subject).to receive(:is_rate_limited?).and_return(true)
end
it "records lost event" do
subject.send_event(event)
expect(subject).to have_recorded_lost_event(:ratelimit_backoff, 'error')
end
end
end
describe "#flush" do
it "does not do anything without pending client reports" do
expect(subject).not_to receive(:send_envelope)
subject.flush
end
it "sends pending client reports" do
5.times { subject.record_lost_event(:ratelimit_backoff, 'error') }
3.times { subject.record_lost_event(:queue_overflow, 'transaction') }
expect(subject).to receive(:send_data)
subject.flush
expect(io.string).to match(/Sending envelope with items \[client_report\]/)
end
end
end