-
-
Notifications
You must be signed in to change notification settings - Fork 539
Expand file tree
/
Copy pathsentry_spec.rb
More file actions
1779 lines (1403 loc) · 59 KB
/
Copy pathsentry_spec.rb
File metadata and controls
1779 lines (1403 loc) · 59 KB
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
require 'contexts/with_request_mock'
RSpec.describe Sentry do
include_context "with request mock"
before do
perform_basic_setup
end
let(:event) do
Sentry::ErrorEvent.new(configuration: Sentry::Configuration.new)
end
describe ".init" do
context "with block argument" do
it "initializes the current hub and main hub" do
described_class.init do |config|
config.dsn = Sentry::TestHelper::DUMMY_DSN
end
current_hub = described_class.get_current_hub
expect(current_hub).to be_a(Sentry::Hub)
expect(current_hub.current_scope).to be_a(Sentry::Scope)
expect(subject.get_main_hub).to eq(current_hub)
end
end
context "without block argument" do
it "initializes the current hub and main hub" do
ENV['SENTRY_DSN'] = Sentry::TestHelper::DUMMY_DSN
described_class.init
current_hub = described_class.get_current_hub
expect(current_hub).to be_a(Sentry::Hub)
expect(current_hub.current_scope).to be_a(Sentry::Scope)
expect(subject.get_main_hub).to eq(current_hub)
end
end
it "initializes Scope with correct max_breadcrumbs" do
described_class.init do |config|
config.max_breadcrumbs = 1
end
current_scope = described_class.get_current_scope
expect(current_scope.breadcrumbs.buffer.size).to eq(1)
end
context "with config.auto_session_tracking = true" do
it "initializes session flusher" do
described_class.init do |config|
config.auto_session_tracking = true
end
expect(described_class.session_flusher).to be_a(Sentry::SessionFlusher)
end
context "when it's not under the enabled environment" do
it "doesn't initialize any session flusher" do
described_class.init do |config|
config.auto_session_tracking = true
config.enabled_environments = ["production"]
end
expect(described_class.session_flusher).to be_nil
end
end
end
end
describe "#clone_hub_to_current_thread" do
it "clones a new hub to the current thread" do
main_hub = described_class.get_main_hub
new_thread = Thread.new do
described_class.clone_hub_to_current_thread
thread_hub = described_class.get_current_hub
expect(thread_hub).to be_a(Sentry::Hub)
expect(thread_hub).not_to eq(main_hub)
expect(thread_hub.current_client).to eq(main_hub.current_client)
expect(described_class.get_main_hub).to eq(main_hub)
end
new_thread.join
end
it "stores the hub in a thread variable (instead of just fiber variable)" do
Sentry.set_tags(outside_fiber: true)
fiber = Fiber.new do
Sentry.set_tags(inside_fiber: true)
end
fiber.resume
expect(Sentry.get_current_scope.tags).to eq({ outside_fiber: true, inside_fiber: true })
end
end
describe ".configure_scope" do
it "yields the current hub's scope" do
scope = nil
described_class.configure_scope { |s| scope = s }
expect(scope).to eq(described_class.get_current_hub.current_scope)
end
end
describe "fiber isolation", when: { fiber_storage?: [] } do
before do
perform_basic_setup { |config| config.hub_isolation_level = :fiber }
end
after do
described_class.set_current_hub_internal(nil)
described_class.instance_variable_set(:@hub_isolation_level, :thread)
end
# Regression for cross-request contamination on fiber-based servers (Falcon,
# async), where concurrent requests are sibling fibers on one thread: a scope
# held across a reactor yield must not be visible to the others.
it "keeps each sibling fiber's scope isolated across a yield" do
transport = described_class.get_main_hub.current_client.transport
transport.events.clear
# Deliberately no clone_hub_to_current_thread: real request fibers never
# call it, so the implicit path is the one that has to isolate.
requests = 3.times.map do |i|
Fiber.new do
described_class.configure_scope { |scope| scope.set_user(id: i) }
Fiber.yield # simulate yielding to the reactor mid-request
described_class.capture_message(i.to_s)
end
end
requests.each(&:resume) # every request sets its user, then yields
requests.each(&:resume) # every request now captures its event
attributed = transport.events.to_h { |e| [e.message.to_i, e.user[:id]] }
expect(attributed).to eq({ 0 => 0, 1 => 1, 2 => 2 })
end
it "gives a child fiber the parent's context in its own hub" do
described_class.clone_hub_to_current_thread
described_class.set_tags(request: "req-1")
parent_hub = described_class.get_current_hub
child_tags, child_hub = Fiber.new do
described_class.set_tags(subtask: true)
[described_class.get_current_scope.tags, described_class.get_current_hub]
end.resume
expect(child_tags).to eq({ request: "req-1", subtask: true })
expect(child_hub).not_to be(parent_hub)
expect(described_class.get_current_scope.tags).to eq({ request: "req-1" })
end
# Fiber storage is inherited by Thread.new too, so a worker pool started
# after anything seeded a hub would otherwise share it across every worker.
it "gives a thread started from a fiber its own hub" do
described_class.clone_hub_to_current_thread
parent_hub = described_class.get_current_hub
hubs = 4.times.map { Thread.new { described_class.get_current_hub } }.map(&:value)
expect(hubs.uniq.size).to eq(4)
expect(hubs).not_to include(parent_hub)
end
# Isolating the scope must not detach the trace: on Async servers the
# fan-out inside a request is exactly what we need spans for.
it "records a child fiber's spans on the parent's transaction" do
perform_basic_setup do |config|
config.hub_isolation_level = :fiber
config.traces_sample_rate = 1.0
end
transaction = described_class.start_transaction(name: "req", op: "http.server")
described_class.get_current_scope.set_span(transaction)
# Threads are deliberately not covered here: the fiber-storage polyfill
# used on Ruby < 3.2 does not inherit into Thread.new, so a thread there
# starts from the main hub and cannot stay on this trace.
Fiber.new { described_class.with_child_span(op: "child.in.fiber") { } }.resume
transaction.finish
expect(transaction.span_recorder.spans.map(&:op)).to eq(["http.server", "child.in.fiber"])
end
it "keeps concurrent threads' with_scope blocks from seeing each other's tags" do
described_class.clone_hub_to_current_thread
ready = Queue.new
gate = Queue.new
threads = 4.times.map do |i|
Thread.new do
described_class.with_scope do |scope|
scope.set_tags(job: i)
ready << :in
gate.pop # hold every job open inside its own scope
described_class.get_current_scope.tags[:job]
end
end
end
4.times { ready.pop }
4.times { gate << :go }
expect(threads.map(&:value)).to eq([0, 1, 2, 3])
end
it "stores the hub in a fiber variable (instead of a thread variable)" do
described_class.set_tags(outside_fiber: true)
fiber = Fiber.new do
described_class.clone_hub_to_current_thread
described_class.set_tags(inside_fiber: true)
described_class.get_current_scope.tags
end
inside_tags = fiber.resume
expect(inside_tags).to eq({ outside_fiber: true, inside_fiber: true })
expect(described_class.get_current_scope.tags).to eq({ outside_fiber: true })
end
end
shared_examples "capture_helper" do
context "with sending_allowed? condition" do
before do
expect(Sentry.configuration).to receive(:sending_allowed?).and_return(false)
capture_subject
end
it "doesn't build the event" do
# don't even initialize event objects
expect(Sentry::Event).not_to receive(:new)
described_class.send(capture_helper, capture_subject)
end
end
context "with sending allowed but sending to dsn not allowed" do
before do
allow(Sentry.configuration).to receive(:sending_allowed?).and_return(true)
allow(Sentry.configuration).to receive(:sending_to_dsn_allowed?).and_return(false)
end
it "doesn't send the event nor assign last_event_id" do
described_class.send(capture_helper, capture_subject)
expect(sentry_events).to be_empty
end
end
context "when rate limited" do
let(:string_io) { StringIO.new }
before do
perform_basic_setup do |config|
config.sdk_logger = Logger.new(string_io)
config.transport.transport_class = Sentry::HTTPTransport
end
Sentry.get_current_client.transport.rate_limits.merge!("error" => Time.now + 100)
end
it "stops the event and logs correct message" do
described_class.send(capture_helper, capture_subject)
expect(string_io.string).to match(/\[Transport\] Envelope item \[event\] not sent: rate limiting/)
end
end
end
describe ".send_event" do
let(:event) { Sentry.get_current_client.event_from_message("test message") }
before do
Sentry.configuration.before_send = lambda do |event, hint|
event.tags[:hint] = hint
event
end
end
it "sends the event" do
described_class.send_event(event)
expect(sentry_events.count).to eq(1)
end
it "sends the event with hint" do
described_class.send_event(event, { foo: "bar" })
expect(sentry_events.count).to eq(1)
event = last_sentry_event
expect(event.tags[:hint][:foo]).to eq("bar")
end
context "with spotlight" do
before { perform_basic_setup { |c| c.spotlight = true } }
it "sends the event to spotlight too", webmock: false do
sentry_stub_request(build_fake_response("200")) do |request, http_obj|
expect(request["Content-Type"]).to eq("application/x-sentry-envelope")
expect(request["Content-Encoding"]).to eq("gzip")
expect(http_obj.address).to eq("localhost")
expect(http_obj.port).to eq(8969)
end
described_class.send_event(event)
end
end
end
describe ".capture_event" do
it_behaves_like "capture_helper" do
let(:capture_helper) { :capture_event }
let(:capture_subject) { event }
end
it "sends the event via current hub" do
expect do
described_class.capture_event(event)
end.to change { sentry_events.count }.by(1)
end
end
describe ".capture_exception" do
let(:exception) { ZeroDivisionError.new("divided by 0") }
it_behaves_like "capture_helper" do
let(:capture_helper) { :capture_exception }
let(:capture_subject) { exception }
end
it "returns ErrorEvent" do
event = described_class.capture_exception(exception)
expect(event).to be_a(Sentry::ErrorEvent)
end
it "sends the exception via current hub" do
expect do
described_class.capture_exception(exception)
end.to change { sentry_events.count }.by(1)
end
it "doesn't send captured exception" do
expect do
described_class.capture_exception(exception)
end.to change { sentry_events.count }.by(1)
expect do
described_class.capture_exception(exception)
end.to change { sentry_events.count }.by(0)
end
it "doesn't do anything if the exception is excluded" do
Sentry.get_current_client.configuration.excluded_exceptions = ["ZeroDivisionError"]
expect do
described_class.capture_exception(exception)
end.to change { sentry_events.count }.by(0)
end
it "passes ignore_exclusions hint" do
Sentry.get_current_client.configuration.excluded_exceptions = ["ZeroDivisionError"]
expect do
described_class.capture_exception(exception, hint: { ignore_exclusions: true })
end.to change { sentry_events.count }.by(1)
end
describe 'mechanism' do
it 'has default mechanism' do
event = described_class.capture_exception(exception)
mechanism = event.exception.values.first.mechanism
expect(mechanism).to be_a(Sentry::Mechanism)
expect(mechanism.type).to eq('generic')
expect(mechanism.handled).to eq(true)
end
it 'has custom mechanism if passed' do
mech = Sentry::Mechanism.new(type: 'custom', handled: false)
event = described_class.capture_exception(exception, hint: { mechanism: mech })
mechanism = event.exception.values.first.mechanism
expect(mechanism).to be_a(Sentry::Mechanism)
expect(mechanism.type).to eq('custom')
expect(mechanism.handled).to eq(false)
end
end
context "with data_collection.stack_frame_variables = false (default)" do
it "doens't capture local variables" do
begin
1/0
rescue => e
described_class.capture_exception(e)
end
event = last_sentry_event.to_h
last_frame = event.dig(:exception, :values, 0, :stacktrace, :frames).last
expect(last_frame[:vars]).to eq(nil)
end
end
context "with data_collection.stack_frame_variables = true" do
before do
perform_basic_setup do |config|
config.data_collection.stack_frame_variables = true
end
end
after do
Sentry.exception_locals_tp.disable
end
it 'captures the exception with locals' do
begin
a = 1
b = 0
a/b
rescue => e
described_class.capture_exception(e)
end
event = last_sentry_event.to_h
last_frame = event.dig(:exception, :values, 0, :stacktrace, :frames).last
expect(last_frame[:vars]).to include({ a: "1", b: "0" })
end
it 'does not throw SDK internal exception with empty frames' do
expect do
begin
raise StandardError, 'Stuff', []
rescue => e
Sentry.capture_exception(e)
end
end.not_to raise_error
end
end
end
describe ".with_exception_captured" do
it "returns the block's result" do
result = described_class.with_exception_captured { 2 }
expect(result).to eq(2)
expect(sentry_events.count).to eq(0)
end
it "rescues and reports the exception happened inside the block" do
expect do
described_class.with_exception_captured(tags: { foo: "bar" }) { 1/0 }
end.to raise_error(ZeroDivisionError)
expect(sentry_events.count).to eq(1)
expect(sentry_events.first.tags).to eq(foo: "bar")
end
end
describe ".capture_message" do
let(:message) { "Test" }
it_behaves_like "capture_helper" do
let(:capture_helper) { :capture_message }
let(:capture_subject) { message }
end
it "sends the message via current hub" do
expect do
described_class.capture_message("Test", tags: { foo: "baz" })
end.to change { sentry_events.count }.by(1)
end
it "returns ErrorEvent" do
event = described_class.capture_message(message)
expect(event).to be_a(Sentry::ErrorEvent)
end
end
describe ".capture_log" do
before do
perform_basic_setup do |config|
config.traces_sample_rate = 1.0
config.max_log_events = 1
end
end
it "sends a log event with trace_id" do
expect do
Sentry.with_scope do |scope|
described_class.capture_log("Test", level: :info, tags: { foo: "baz" })
end
end.to_not change { sentry_events.count }
Sentry.get_current_client.flush
expect(sentry_envelopes.count).to be(1)
log_event = sentry_logs.first
expect(log_event[:level]).to eq("info")
expect(log_event[:trace_id]).to_not be(nil)
expect(log_event[:span_id]).to_not be(nil)
end
it "sends a log event with span_id" do
transaction = Sentry.start_transaction(name: "test_transaction", op: "test.op")
span = transaction.start_child(op: "child span")
Sentry.get_current_scope.set_span(span)
described_class.capture_log("Test", level: :info, tags: { foo: "baz" })
transaction.finish
Sentry.get_current_client.flush
# 2 envelopes: log and transaction
expect(sentry_envelopes.size).to be(2)
log_event = sentry_logs.first
expect(log_event[:level]).to eq("info")
expect(log_event[:trace_id]).to eq(span.trace_id)
expect(log_event[:span_id]).to eq(span.span_id)
end
it "includes sentry.origin attribute when origin is provided" do
expect do
described_class.capture_log("Database query executed", level: :info, origin: "auto.log.rails.log_subscriber")
end.to_not change { sentry_events.count }
Sentry.get_current_client.flush
expect(sentry_envelopes.count).to eq(1)
log_event = sentry_logs.first
expect(log_event[:level]).to eq("info")
expect(log_event[:body]).to eq("Database query executed")
expect(log_event[:attributes]).to have_key("sentry.origin")
expect(log_event[:attributes]["sentry.origin"]).to eq({ value: "auto.log.rails.log_subscriber", type: "string" })
end
it "does not include sentry.origin attribute when origin is not provided" do
expect do
described_class.capture_log("Manual log message", level: :info)
end.to_not change { sentry_events.count }
Sentry.get_current_client.flush
expect(sentry_envelopes.count).to eq(1)
log_event = sentry_logs.first
expect(log_event[:level]).to eq("info")
expect(log_event[:body]).to eq("Manual log message")
expect(log_event[:attributes]).not_to have_key("sentry.origin")
end
end
describe ".start_transaction" do
describe "when not continuing an existing trace" do
before do
perform_basic_setup do |config|
config.traces_sample_rate = 1.0
end
end
it "does not adopt the scope's propagation context when it wasn't established for this call" do
propagation_context = Sentry.get_current_scope.propagation_context
transaction = described_class.start_transaction(name: "test", op: "test.op")
# each independent call gets its own, unrelated trace_id by default - only
# calls made with established: true (e.g. a Rack request that went through
# Sentry::Rails::CaptureContext) adopt the scope's propagation context
expect(transaction.trace_id).not_to eq(propagation_context.trace_id)
end
context "when the scope's propagation context was established for this call" do
before do
Sentry.get_current_scope.generate_propagation_context
end
it "adopts the scope's propagation context trace_id and sample_rand" do
propagation_context = Sentry.get_current_scope.propagation_context
transaction = described_class.start_transaction(
name: "test", op: "test.op", established: true
)
expect(transaction.trace_id).to eq(propagation_context.trace_id)
expect(transaction.sample_rand).to eq(propagation_context.sample_rand)
end
it "adopts the scope's propagation context span_id" do
propagation_context = Sentry.get_current_scope.propagation_context
transaction = described_class.start_transaction(
name: "test", op: "test.op", established: true
)
expect(transaction.span_id).to eq(propagation_context.span_id)
end
it "does not override an explicitly provided span_id" do
transaction = described_class.start_transaction(
name: "test", op: "test.op", span_id: "b" * 16, established: true
)
expect(transaction.span_id).to eq("b" * 16)
end
it "does not override an explicitly provided trace_id" do
transaction = described_class.start_transaction(
name: "test", op: "test.op", trace_id: "a" * 32, established: true
)
expect(transaction.trace_id).to eq("a" * 32)
end
it "does not override an explicitly provided sample_rand" do
propagation_context = Sentry.get_current_scope.propagation_context
transaction = described_class.start_transaction(
name: "test", op: "test.op", sample_rand: 0.999999, established: true
)
expect(transaction.trace_id).to eq(propagation_context.trace_id)
expect(transaction.sample_rand).to eq(0.999999)
end
end
end
describe "sampler example" do
before do
perform_basic_setup do |config|
config.traces_sampler = lambda do |sampling_context|
# if this is the continuation of a trace, just use that decision (rate controlled by the caller)
unless sampling_context[:parent_sampled].nil?
next sampling_context[:parent_sampled]
end
# transaction_context is the transaction object in hash form
# keep in mind that sampling happens right after the transaction is initialized
# e.g. at the beginning of the request
transaction_context = sampling_context[:transaction_context]
# transaction_context helps you sample transactions with more sophistication
# for example, you can provide different sample rates based on the operation or name
op = transaction_context[:op]
transaction_name = transaction_context[:name]
case op
when /request/
case transaction_name
when /health_check/
0.0
when /payment/
0.5
when /api/
0.2
else
0.1
end
when /sidekiq/
0.01 # you may want to set a lower rate for background jobs if the number is large
else
0.0 # ignore all other transactions
end
end
end
end
it "prioritizes parent's sampling decision" do
sampled_trace = "d298e6b033f84659928a2267c3879aaa-2a35b8e9a1b974f4-1"
unsampled_trace = "d298e6b033f84659928a2267c3879aaa-2a35b8e9a1b974f4-0"
not_sampled_trace = "d298e6b033f84659928a2267c3879aaa-2a35b8e9a1b974f4-"
transaction = Sentry.continue_trace({ "sentry-trace" => sampled_trace }, op: "rack.request", name: "/payment")
described_class.start_transaction(transaction: transaction)
expect(transaction.sampled).to eq(true)
transaction = Sentry.continue_trace({ "sentry-trace" => unsampled_trace }, op: "rack.request", name: "/payment")
described_class.start_transaction(transaction: transaction)
expect(transaction.sampled).to eq(false)
transaction = described_class.start_transaction(
op: "rack.request", name: "/payment", sample_rand: 0.4
)
expect(transaction.sampled).to eq(true)
end
it "skips /health_check" do
transaction = described_class.start_transaction(op: "rack.request", name: "/health_check")
expect(transaction.sampled).to eq(false)
end
it "gives /payment 0.5 of rate" do
transaction = described_class.start_transaction(op: "rack.request", name: "/payment", sample_rand: 0.4)
expect(transaction.sampled).to eq(true)
transaction = described_class.start_transaction(op: "rack.request", name: "/payment", sample_rand: 0.6)
expect(transaction.sampled).to eq(false)
end
it "gives /api 0.2 of rate" do
transaction = described_class.start_transaction(op: "rack.request", name: "/api", sample_rand: 0.1)
expect(transaction.sampled).to eq(true)
transaction = described_class.start_transaction(op: "rack.request", name: "/api", sample_rand: 0.3)
expect(transaction.sampled).to eq(false)
end
it "gives other paths 0.1 of rate" do
transaction = described_class.start_transaction(op: "rack.request", name: "/orders", sample_rand: 0.05)
expect(transaction.sampled).to eq(true)
transaction = described_class.start_transaction(op: "rack.request", name: "/orders", sample_rand: 0.2)
expect(transaction.sampled).to eq(false)
end
it "gives sidekiq ops 0.01 of rate" do
transaction = described_class.start_transaction(op: "sidekiq", sample_rand: 0.005)
expect(transaction.sampled).to eq(true)
transaction = described_class.start_transaction(op: "sidekiq", sample_rand: 0.02)
expect(transaction.sampled).to eq(false)
end
end
context "when tracing is enabled" do
before do
Sentry.configuration.traces_sample_rate = 1.0
end
it "starts a new transaction" do
transaction = described_class.start_transaction(op: "foo")
expect(transaction).to be_a(Sentry::Transaction)
expect(transaction.op).to eq("foo")
end
context "when given an transaction object" do
it "adds sample decision to it" do
transaction = Sentry::Transaction.new(name: "test")
transaction = described_class.start_transaction(transaction: transaction)
expect(transaction.sampled).to eq(true)
end
it "provides proper sampling context to the traces_sampler" do
context = nil
Sentry.configuration.traces_sampler = lambda do |sampling_context|
context = sampling_context
end
described_class.start_transaction(op: "foo")
expect(context[:parent_sampled]).to be_nil
expect(context[:transaction_context][:op]).to eq("foo")
end
it "passes parent_sampled to the sampling_context" do
context = nil
Sentry.configuration.traces_sampler = lambda do |sampling_context|
context = sampling_context
end
described_class.start_transaction(parent_sampled: true)
expect(context[:parent_sampled]).to eq(true)
end
end
context "when given a custom_sampling_context" do
it "takes that into account" do
context = nil
Sentry.configuration.traces_sampler = lambda do |sampling_context|
context = sampling_context
end
described_class.start_transaction(custom_sampling_context: { foo: "bar" })
expect(context).to include({ foo: "bar" })
end
end
context "when event reporting is not enabled" do
let(:string_io) { StringIO.new }
let(:logger) do
::Logger.new(string_io)
end
before do
Sentry.configuration.sdk_logger = logger
Sentry.configuration.enabled_environments = ["production"]
end
it "sets @sampled to false and return" do
transaction = described_class.start_transaction
expect(transaction).to eq(nil)
expect(string_io.string).not_to include(
"[Tracing]"
)
end
end
end
context "when tracing is disabled" do
it "returns nil" do
expect(described_class.start_transaction(op: "foo")).to eq(nil)
end
end
context "when instrumenter is not :sentry" do
before do
perform_basic_setup do |config|
config.traces_sample_rate = 1.0
config.instrumenter = :otel
end
end
it "noops without explicit instrumenter" do
expect(described_class.start_transaction(op: "foo")).to eq(nil)
end
it "creates transaction with explicit instrumenter" do
transaction = described_class.start_transaction(op: "foo", instrumenter: :otel)
expect(transaction).to be_a(Sentry::Transaction)
end
end
end
describe ".with_child_span" do
context "when the current span is nil" do
before do
expect(described_class.get_current_scope.get_span).to eq(nil)
end
it "yields the block with nil" do
span = nil
executed = false
result = described_class.with_child_span do |child_span|
span = child_span
executed = true
"foobar"
end
expect(result).to eq("foobar")
expect(span).to eq(nil)
expect(executed).to eq(true)
end
end
context "when the current span is present" do
let(:parent_span) do
transaction = described_class.start_transaction(op: "foo")
Sentry::Span.new(op: "parent", transaction: transaction)
end
before do
described_class.get_current_scope.set_span(parent_span)
end
it "records the child span and attaches it to the parent span" do
child_span = nil
result = described_class.with_child_span(op: "child") do |span|
child_span = span
"foobar"
end
expect(result).to eq("foobar")
expect(child_span.parent_span_id).to eq(parent_span.span_id)
expect(child_span.timestamp).to be_a(Float)
end
context "when instrumenter is not :sentry" do
before do
perform_basic_setup do |config|
config.traces_sample_rate = 1.0
config.instrumenter = :otel
end
described_class.get_current_scope.set_span(parent_span)
end
it "yields block with nil without explicit instrumenter" do
span = nil
executed = false
result = described_class.with_child_span do |child_span|
span = child_span
executed = true
"foobar"
end
expect(result).to eq("foobar")
expect(span).to eq(nil)
expect(executed).to eq(true)
end
it "records the child span with explicit instrumenter" do
child_span = nil
result = described_class.with_child_span(instrumenter: :otel, op: "child") do |span|
child_span = span
"foobar"
end
expect(result).to eq("foobar")
expect(child_span.parent_span_id).to eq(parent_span.span_id)
expect(child_span.timestamp).to be_a(Float)
end
end
end
end
describe ".last_event_id" do
it "gets the last_event_id from current_hub" do
expect(described_class.get_current_hub).to receive(:last_event_id)
described_class.last_event_id
end
end
describe ".add_breadcrumb" do
it "adds breadcrumb to the current scope" do
crumb = Sentry::Breadcrumb.new(message: "foo")
described_class.add_breadcrumb(crumb)
expect(described_class.get_current_scope.breadcrumbs.peek).to eq(crumb)
end
it "triggers before_breadcrumb callback" do
Sentry.configuration.before_breadcrumb = lambda do |breadcrumb, hint|
nil
end
crumb = Sentry::Breadcrumb.new(message: "foo")
described_class.add_breadcrumb(crumb)
expect(described_class.get_current_scope.breadcrumbs.peek).to eq(nil)
end
end
describe ".set_tags" do
it "adds tags to the current scope" do
described_class.set_tags(foo: "bar")
expect(described_class.get_current_scope.tags).to eq(foo: "bar")
end
end
describe ".set_extras" do
it "adds extras to the current scope" do
described_class.set_extras(foo: "bar")
expect(described_class.get_current_scope.extra).to eq(foo: "bar")
end
end
describe ".set_context" do
it "adds context to the current scope" do
described_class.set_context("character", { name: "John", age: 25 })
expect(described_class.get_current_scope.contexts).to include("character" => { name: "John", age: 25 })
end
end
describe ".set_user" do
it "adds user to the current scope" do
described_class.set_user(id: 1)
expect(described_class.get_current_scope.user).to eq(id: 1)
end
end
describe ".set_attributes" do
it "adds attributes to the current scope" do
described_class.set_attributes("foo" => "bar", "baz" => 42)
expect(described_class.get_current_scope.attributes).to eq("foo" => "bar", "baz" => 42)
end
end
describe ".set_attribute" do
it "adds a single attribute to the current scope" do
described_class.set_attribute("foo", "bar")
expect(described_class.get_current_scope.attributes).to eq("foo" => "bar")
end
it "wraps the value when given the optional unit: param" do