forked from getsentry/sentry-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction_spec.rb
617 lines (490 loc) · 20.1 KB
/
transaction_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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
require "spec_helper"
RSpec.describe Sentry::Transaction do
before do
perform_basic_setup
end
subject do
described_class.new(
op: "sql.query",
description: "SELECT * FROM users;",
status: "ok",
sampled: true,
parent_sampled: true,
name: "foo",
source: :view,
hub: Sentry.get_current_hub
)
end
describe ".from_sentry_trace" do
let(:sentry_trace) { subject.to_sentry_trace }
let(:baggage) {
"other-vendor-value-1=foo;bar;baz, "\
"sentry-trace_id=771a43a4192642f0b136d5159a501700, "\
"sentry-public_key=49d0f7386ad645858ae85020e393bef3, "\
"sentry-sample_rate=0.01337, "\
"sentry-user_id=Am%C3%A9lie, "\
"other-vendor-value-2=foo;bar;"
}
let(:configuration) do
Sentry.configuration
end
context "when tracing is enabled (> 0)" do
before do
configuration.traces_sample_rate = 1.0
end
it "returns correctly-formatted value" do
child_transaction = described_class.from_sentry_trace(sentry_trace, op: "child")
expect(child_transaction.trace_id).to eq(subject.trace_id)
expect(child_transaction.parent_span_id).to eq(subject.span_id)
expect(child_transaction.parent_sampled).to eq(true)
# doesn't set the sampled value
expect(child_transaction.sampled).to eq(nil)
expect(child_transaction.op).to eq("child")
end
it "handles invalid values without crashing" do
child_transaction = described_class.from_sentry_trace("dummy", op: "child")
expect(child_transaction).to be_nil
end
it "stores frozen empty baggage on incoming traces from older SDKs" do
child_transaction = described_class.from_sentry_trace(sentry_trace, baggage: nil, op: "child")
expect(child_transaction.baggage).not_to be_nil
expect(child_transaction.baggage.mutable).to be(false)
expect(child_transaction.baggage.items).to eq({})
end
it "stores correct baggage on incoming baggage header" do
child_transaction = described_class.from_sentry_trace(sentry_trace, baggage: baggage, op: "child")
expect(child_transaction.baggage).not_to be_nil
expect(child_transaction.baggage.mutable).to be(false)
expect(child_transaction.baggage.items).to eq({
"sample_rate" => "0.01337",
"public_key" => "49d0f7386ad645858ae85020e393bef3",
"trace_id" => "771a43a4192642f0b136d5159a501700",
"user_id" => "Amélie"
})
end
end
context "when tracing is enabled (= 0)" do
before do
configuration.traces_sample_rate = 0.0
end
it "returns correctly-formatted value" do
child_transaction = described_class.from_sentry_trace(sentry_trace, op: "child")
expect(child_transaction.trace_id).to eq(subject.trace_id)
expect(child_transaction.parent_span_id).to eq(subject.span_id)
expect(child_transaction.parent_sampled).to eq(true)
# doesn't set the sampled value
expect(child_transaction.sampled).to eq(nil)
expect(child_transaction.op).to eq("child")
end
end
context "when tracing is disabled" do
before do
configuration.traces_sample_rate = nil
end
it "returns nil" do
expect(described_class.from_sentry_trace(sentry_trace, op: "child")).to be_nil
end
end
end
describe "#deep_dup" do
before do
subject.start_child(op: "first child")
subject.start_child(op: "second child")
end
it "copies all the values and spans from the original transaction" do
copy = subject.deep_dup
subject.set_op("foo")
subject.set_description("bar")
# the copy should have the same attributes, including span_id
expect(copy.op).to eq("sql.query")
expect(copy.description).to eq("SELECT * FROM users;")
expect(copy.status).to eq("ok")
expect(copy.trace_id).to eq(subject.trace_id)
expect(copy.trace_id.length).to eq(32)
expect(copy.span_id).to eq(subject.span_id)
expect(copy.span_id.length).to eq(16)
# child spans should also be copied
expect(copy.span_recorder.spans.count).to eq(3)
# but span recorder should have the correct first span (shouldn't be the subject)
expect(copy.span_recorder.spans.first).to eq(copy)
# child spans should have identical attributes
expect(subject.span_recorder.spans[1].op).to eq("first child")
expect(copy.span_recorder.spans[1].op).to eq("first child")
expect(copy.span_recorder.spans[1].span_id).to eq(subject.span_recorder.spans[1].span_id)
expect(subject.span_recorder.spans[2].op).to eq("second child")
expect(copy.span_recorder.spans[2].op).to eq("second child")
expect(copy.span_recorder.spans[2].span_id).to eq(subject.span_recorder.spans[2].span_id)
# but they should not be the same
expect(copy.span_recorder.spans[1]).not_to eq(subject.span_recorder.spans[1])
expect(copy.span_recorder.spans[2]).not_to eq(subject.span_recorder.spans[2])
# and mutations shouldn't be shared
subject.span_recorder.spans[1].set_op("foo")
expect(copy.span_recorder.spans[1].op).to eq("first child")
end
end
describe "#set_measurement" do
it "sets the measurement" do
subject.set_measurement("metric.foo", 0.1, "second")
subject.set_measurement("metric.bar", 1.0, "minute")
subject.set_measurement("metric.baz", 1.0)
expect(subject.measurements).to eq(
{
"metric.foo" => { value: 0.1, unit: "second" },
"metric.bar" => { value: 1.0, unit: "minute" },
"metric.baz" => { value: 1.0, unit: "" },
}
)
subject.set_measurement("metric.foo", 2, "second")
expect(subject.measurements).to eq(
{
"metric.foo" => { value: 2, unit: "second" },
"metric.bar" => { value: 1.0, unit: "minute" },
"metric.baz" => { value: 1.0, unit: "" },
}
)
end
end
describe "#start_child" do
it "initializes a new child Span and assigns the 'transaction' attribute with itself" do
# create subject span and wait for a sec for making time difference
subject
new_span = subject.start_child(op: "sql.query", description: "SELECT * FROM orders WHERE orders.user_id = 1", status: "ok")
expect(new_span.op).to eq("sql.query")
expect(new_span.description).to eq("SELECT * FROM orders WHERE orders.user_id = 1")
expect(new_span.status).to eq("ok")
expect(new_span.trace_id).to eq(subject.trace_id)
expect(new_span.span_id).not_to eq(subject.span_id)
expect(new_span.parent_span_id).to eq(subject.span_id)
expect(new_span.sampled).to eq(true)
expect(new_span.transaction).to eq(subject)
end
end
describe "#set_initial_sample_decision" do
let(:string_io) { StringIO.new }
let(:logger) do
::Logger.new(string_io)
end
before do
perform_basic_setup do |config|
config.logger = logger
end
end
context "when tracing is not enabled" do
before do
allow(Sentry.configuration).to receive(:tracing_enabled?).and_return(false)
end
it "sets @sampled to false and return" do
allow(Sentry.configuration).to receive(:tracing_enabled?).and_return(false)
transaction = described_class.new(sampled: true, hub: Sentry.get_current_hub)
transaction.set_initial_sample_decision(sampling_context: {})
expect(transaction.sampled).to eq(false)
end
end
context "when tracing is enabled" do
let(:subject) { described_class.new(op: "rack.request", hub: Sentry.get_current_hub) }
before do
allow(Sentry.configuration).to receive(:tracing_enabled?).and_return(true)
end
context "when the transaction already has a decision" do
it "doesn't change it" do
transaction = described_class.new(sampled: true, hub: Sentry.get_current_hub)
transaction.set_initial_sample_decision(sampling_context: {})
expect(transaction.sampled).to eq(true)
expect(transaction.effective_sample_rate).to eq(1.0)
transaction = described_class.new(sampled: false, hub: Sentry.get_current_hub)
transaction.set_initial_sample_decision(sampling_context: {})
expect(transaction.sampled).to eq(false)
expect(transaction.effective_sample_rate).to eq(0.0)
end
end
context "when traces_sampler is not set" do
before do
Sentry.configuration.traces_sample_rate = 0.5
end
it "prioritizes inherited decision over traces_sample_rate" do
allow(Random).to receive(:rand).and_return(0.4)
subject.set_initial_sample_decision(sampling_context: { parent_sampled: false })
expect(subject.sampled).to eq(false)
expect(subject.effective_sample_rate).to eq(0.0)
end
it "uses traces_sample_rate for sampling (positive result)" do
allow(Random).to receive(:rand).and_return(0.4)
subject.set_initial_sample_decision(sampling_context: {})
expect(subject.sampled).to eq(true)
expect(subject.effective_sample_rate).to eq(0.5)
expect(string_io.string).to include(
"[Tracing] Starting <rack.request> transaction"
)
end
it "uses traces_sample_rate for sampling (negative result)" do
allow(Random).to receive(:rand).and_return(0.6)
subject.set_initial_sample_decision(sampling_context: {})
expect(subject.sampled).to eq(false)
expect(subject.effective_sample_rate).to eq(0.5)
expect(string_io.string).to include(
"[Tracing] Discarding <rack.request> transaction because it's not included in the random sample (sampling rate = 0.5)"
)
end
it "accepts integer traces_sample_rate" do
Sentry.configuration.traces_sample_rate = 1
subject.set_initial_sample_decision(sampling_context: {})
expect(subject.sampled).to eq(true)
expect(subject.effective_sample_rate).to eq(1.0)
end
end
context "when traces_sampler is provided" do
it "prioritizes traces_sampler over traces_sample_rate" do
Sentry.configuration.traces_sample_rate = 1.0
Sentry.configuration.traces_sampler = -> (_) { false }
subject.set_initial_sample_decision(sampling_context: {})
expect(subject.sampled).to eq(false)
expect(subject.effective_sample_rate).to eq(0.0)
end
it "prioritizes traces_sampler over inherited decision" do
Sentry.configuration.traces_sampler = -> (_) { false }
subject.set_initial_sample_decision(sampling_context: { parent_sampled: true })
expect(subject.sampled).to eq(false)
expect(subject.effective_sample_rate).to eq(0.0)
end
it "ignores the sampler if it's not callable" do
Sentry.configuration.traces_sampler = ""
expect do
subject.set_initial_sample_decision(sampling_context: {})
end.not_to raise_error
end
it "discards the transaction if generated sample rate is not valid" do
Sentry.configuration.traces_sampler = -> (_) { "foo" }
subject.set_initial_sample_decision(sampling_context: {})
expect(subject.sampled).to eq(false)
expect(string_io.string).to include(
"[Tracing] Discarding <rack.request> transaction because of invalid sample_rate: foo"
)
end
it "uses the genereted rate for sampling (positive)" do
expect(Sentry.configuration.logger).to receive(:debug).exactly(3).and_call_original
Sentry.configuration.traces_sampler = -> (_) { true }
subject = described_class.new(hub: Sentry.get_current_hub)
subject.set_initial_sample_decision(sampling_context: {})
expect(subject.sampled).to eq(true)
expect(subject.effective_sample_rate).to eq(1.0)
Sentry.configuration.traces_sampler = -> (_) { 1.0 }
subject = described_class.new(hub: Sentry.get_current_hub)
subject.set_initial_sample_decision(sampling_context: {})
expect(subject.sampled).to eq(true)
expect(subject.effective_sample_rate).to eq(1.0)
Sentry.configuration.traces_sampler = -> (_) { 1 }
subject = described_class.new(hub: Sentry.get_current_hub)
subject.set_initial_sample_decision(sampling_context: {})
expect(subject.sampled).to eq(true)
expect(subject.effective_sample_rate).to eq(1.0)
expect(string_io.string).to include(
"[Tracing] Starting transaction"
)
end
it "uses the genereted rate for sampling (negative)" do
expect(Sentry.configuration.logger).to receive(:debug).exactly(2).and_call_original
Sentry.configuration.traces_sampler = -> (_) { false }
subject = described_class.new(hub: Sentry.get_current_hub)
subject.set_initial_sample_decision(sampling_context: {})
expect(subject.sampled).to eq(false)
expect(subject.effective_sample_rate).to eq(0.0)
Sentry.configuration.traces_sampler = -> (_) { 0.0 }
subject = described_class.new(hub: Sentry.get_current_hub)
subject.set_initial_sample_decision(sampling_context: {})
expect(subject.sampled).to eq(false)
expect(subject.effective_sample_rate).to eq(0.0)
expect(string_io.string).to include(
"[Tracing] Discarding transaction because traces_sampler returned 0 or false"
)
end
end
end
end
describe "#to_hash" do
it "returns correct data" do
hash = subject.to_hash
expect(hash[:op]).to eq("sql.query")
expect(hash[:description]).to eq("SELECT * FROM users;")
expect(hash[:status]).to eq("ok")
expect(hash[:trace_id].length).to eq(32)
expect(hash[:span_id].length).to eq(16)
expect(hash[:sampled]).to eq(true)
expect(hash[:parent_sampled]).to eq(true)
expect(hash[:name]).to eq("foo")
expect(hash[:source]).to eq(:view)
end
end
describe "#finish" do
let(:events) do
Sentry.get_current_client.transport.events
end
let(:another_hub) do
Sentry.get_current_hub.clone
end
it "finishes the transaction, converts it into an Event and send it" do
subject.finish
expect(events.count).to eq(1)
event = events.last.to_hash
# don't contain itself
expect(event[:spans]).to be_empty
end
it "assigns the transaction's transaction name" do
subject.finish
expect(events.count).to eq(1)
event = events.last.to_hash
expect(event[:transaction]).to eq("foo")
end
it "finishes the transaction with explicit timestamp" do
timestamp = Sentry.utc_now.to_f
subject.finish(end_timestamp: timestamp)
expect(events.count).to eq(1)
event = events.last.to_hash
expect(event[:timestamp]).to eq(timestamp)
end
it "assigns the transaction's tags" do
Sentry.set_tags(name: "apple")
subject.set_tag(:foo, 'bar')
subject.finish
expect(events.count).to eq(1)
event = events.last.to_hash
expect(event[:tags]).to eq({ foo: 'bar', name: "apple" })
end
it "only follows sampling decision" do
Sentry.configuration.traces_sampler = proc { false }
subject = described_class.new(parent_sampled: true, hub: Sentry.get_current_hub)
subject.finish
expect(events.count).to eq(0)
end
describe "hub selection" do
it "prioritizes the optional hub argument and uses it to submit the transaction" do
expect(another_hub).to receive(:capture_event)
subject.finish(hub: another_hub)
end
it "submits the event with the transaction's hub by default" do
subject.instance_variable_set(:@hub, another_hub)
expect(another_hub).to receive(:capture_event)
subject.finish
end
end
context "if the transaction is not sampled" do
subject { described_class.new(sampled: false, hub: Sentry.get_current_hub) }
it "doesn't send it" do
subject.finish
expect(events.count).to eq(0)
end
it "records lost event" do
subject.finish
expect(Sentry.get_current_client.transport).to have_recorded_lost_event(:sample_rate, 'transaction')
end
end
context "if the transaction doesn't have a name" do
subject { described_class.new(sampled: true, hub: Sentry.get_current_hub) }
it "adds a default name" do
subject.finish
expect(subject.name).to eq("<unlabeled transaction>")
end
end
describe "#set_measurement" do
it "adds measurements the event" do
subject.set_measurement("metric.foo", 0.5, "second")
subject.finish
transaction = events.last.to_hash
expect(transaction[:measurements]).to eq(
{ "metric.foo" => { value: 0.5, unit: "second" } }
)
end
end
end
describe "#get_baggage" do
before do
allow(Sentry.configuration).to receive(:tracing_enabled?).and_return(true)
end
let(:source) { :view }
subject do
transaction = described_class.new(
sampled: true,
parent_sampled: true,
name: "foo",
source: source,
hub: Sentry.get_current_hub,
baggage: incoming_baggage
)
transaction.set_initial_sample_decision(sampling_context: {})
transaction
end
context "when no incoming baggage" do
let(:incoming_baggage) { nil }
it "populates baggage as head SDK" do
expect(subject).to receive(:populate_head_baggage).and_call_original
baggage = subject.get_baggage
expect(baggage.mutable).to eq(false)
expect(baggage.items).to eq({
"environment" => "development",
"public_key" => "12345",
"trace_id" => subject.trace_id,
"transaction"=>"foo",
"sample_rate" => "1.0",
"sampled" => "true"
})
end
context "when source is low quality" do
let(:source) { :url }
it "populates baggage as head SDK without transaction" do
baggage = subject.get_baggage
expect(baggage.items).not_to include("transaction")
end
end
end
context "when incoming empty frozen baggage from old SDK" do
let(:incoming_baggage) { Sentry::Baggage.new({}, mutable: false) }
it "does not populate new baggage" do
expect(subject).not_to receive(:populate_head_baggage)
baggage = subject.get_baggage
expect(baggage.mutable).to eq(false)
expect(baggage.items).to eq({})
end
end
context "when incoming baggage with sentry items" do
let(:incoming_baggage) do
Sentry::Baggage.from_baggage_string("sentry-trace_id=12345,foo=bar")
end
it "does not populate new baggage" do
expect(subject).not_to receive(:populate_head_baggage)
baggage = subject.get_baggage
expect(baggage.mutable).to eq(false)
expect(baggage.items).to eq({ "trace_id" => "12345" })
end
end
context "when incoming baggage with no sentry items" do
let(:incoming_baggage) do
Sentry::Baggage.from_baggage_string("foo=bar")
end
it "populates sentry baggage" do
expect(subject).to receive(:populate_head_baggage).and_call_original
baggage = subject.get_baggage
expect(baggage.mutable).to eq(false)
expect(baggage.items).to eq({
"environment" => "development",
"public_key" => "12345",
"trace_id" => subject.trace_id,
"transaction"=>"foo",
"sample_rate" => "1.0",
"sampled" => "true"
})
end
end
end
describe "#set_name" do
it "sets name and source directly" do
subject.set_name("bar", source: :url)
expect(subject.name).to eq("bar")
expect(subject.source).to eq(:url)
end
end
describe "#set_context" do
it "sets arbitrary context" do
subject.set_context(:foo, { bar: 42 })
expect(subject.contexts).to eq({ foo: { bar: 42 } })
end
end
end