-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
sentry_spec.rb
1175 lines (934 loc) · 36.9 KB
/
sentry_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
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
require "spec_helper"
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
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.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" do
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 include_local_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_hash
last_frame = event.dig(:exception, :values, 0, :stacktrace, :frames).last
expect(last_frame[:vars]).to eq(nil)
end
end
context "with include_local_variables = true" do
before do
perform_basic_setup do |config|
config.include_local_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_hash
last_frame = event.dig(:exception, :values, 0, :stacktrace, :frames).last
expect(last_frame[:vars]).to include({ a: "1", b: "0" })
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 ".start_transaction" do
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)
allow(Random).to receive(:rand).and_return(0.4)
transaction = Sentry.continue_trace({ "sentry-trace" => not_sampled_trace }, op: "rack.request", name: "/payment")
described_class.start_transaction(transaction: transaction)
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
allow(Random).to receive(:rand).and_return(0.4)
transaction = described_class.start_transaction(op: "rack.request", name: "/payment")
expect(transaction.sampled).to eq(true)
allow(Random).to receive(:rand).and_return(0.6)
transaction = described_class.start_transaction(op: "rack.request", name: "/payment")
expect(transaction.sampled).to eq(false)
end
it "gives /api 0.2 of rate" do
allow(Random).to receive(:rand).and_return(0.1)
transaction = described_class.start_transaction(op: "rack.request", name: "/api")
expect(transaction.sampled).to eq(true)
allow(Random).to receive(:rand).and_return(0.3)
transaction = described_class.start_transaction(op: "rack.request", name: "/api")
expect(transaction.sampled).to eq(false)
end
it "gives other paths 0.1 of rate" do
allow(Random).to receive(:rand).and_return(0.05)
transaction = described_class.start_transaction(op: "rack.request", name: "/orders")
expect(transaction.sampled).to eq(true)
allow(Random).to receive(:rand).and_return(0.2)
transaction = described_class.start_transaction(op: "rack.request", name: "/orders")
expect(transaction.sampled).to eq(false)
end
it "gives sidekiq ops 0.01 of rate" do
allow(Random).to receive(:rand).and_return(0.005)
transaction = described_class.start_transaction(op: "sidekiq")
expect(transaction.sampled).to eq(true)
allow(Random).to receive(:rand).and_return(0.02)
transaction = described_class.start_transaction(op: "sidekiq")
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(hub: Sentry.get_current_hub)
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
transaction = Sentry::Transaction.new(op: "foo", hub: Sentry.get_current_hub)
described_class.start_transaction(transaction: transaction)
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
transaction = Sentry::Transaction.new(parent_sampled: true, hub: Sentry.get_current_hub)
described_class.start_transaction(transaction: transaction)
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.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 = Sentry::Transaction.new(op: "foo", hub: Sentry.get_current_hub)
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 ".csp_report_uri" do
it "returns the csp_report_uri generated from the main Configuration" do
expect(Sentry.configuration).to receive(:csp_report_uri).and_call_original
expect(described_class.csp_report_uri).to eq("http://sentry.localdomain/api/42/security/?sentry_key=12345&sentry_environment=development")
end
end
describe ".exception_captured?" do
let(:exception) { Exception.new }
it "returns true if the exception has been captured by the SDK" do
expect(described_class.exception_captured?(exception)).to eq(false)
described_class.capture_exception(exception)
expect(described_class.exception_captured?(exception)).to eq(true)
end
end
describe ".get_traceparent" do
it "returns a valid traceparent header from scope propagation context" do
traceparent = described_class.get_traceparent
propagation_context = described_class.get_current_scope.propagation_context
expect(traceparent).to match(Sentry::PropagationContext::SENTRY_TRACE_REGEXP)
expect(traceparent).to eq("#{propagation_context.trace_id}-#{propagation_context.span_id}")
end
it "returns a valid traceparent header from scope current span" do
transaction = Sentry::Transaction.new(op: "foo", hub: Sentry.get_current_hub, sampled: true)
span = transaction.start_child(op: "parent")
described_class.get_current_scope.set_span(span)
traceparent = described_class.get_traceparent
expect(traceparent).to match(Sentry::PropagationContext::SENTRY_TRACE_REGEXP)
expect(traceparent).to eq("#{span.trace_id}-#{span.span_id}-1")
end
end
describe ".get_baggage" do
it "returns a valid baggage header from scope propagation context" do
baggage = described_class.get_baggage
propagation_context = described_class.get_current_scope.propagation_context
expect(baggage).to eq("sentry-trace_id=#{propagation_context.trace_id},sentry-environment=development,sentry-public_key=12345")
end
it "returns a valid baggage header from scope current span" do
transaction = Sentry::Transaction.new(op: "foo", hub: Sentry.get_current_hub, sampled: true)
span = transaction.start_child(op: "parent")
described_class.get_current_scope.set_span(span)
baggage = described_class.get_baggage
expect(baggage).to eq("sentry-trace_id=#{span.trace_id},sentry-sampled=true,sentry-environment=development,sentry-public_key=12345")
end
end
describe ".get_trace_propagation_headers" do
it "returns a Hash of sentry-trace and baggage" do
expect(described_class.get_trace_propagation_headers).to eq({
"sentry-trace" => described_class.get_traceparent,
"baggage" => described_class.get_baggage
})
end
end
describe ".continue_trace" do
context "without incoming sentry trace" do
let(:env) { { "HTTP_FOO" => "bar" } }
it "returns nil with tracing disabled" do
expect(described_class.continue_trace(env)).to eq(nil)
end
it "returns nil with tracing enabled" do
Sentry.configuration.traces_sample_rate = 1.0
expect(described_class.continue_trace(env)).to eq(nil)
end
it "sets new propagation context on scope" do
expect(Sentry.get_current_scope).to receive(:generate_propagation_context).and_call_original
described_class.continue_trace(env)
propagation_context = Sentry.get_current_scope.propagation_context
expect(propagation_context.incoming_trace).to eq(false)
end
end
context "with incoming sentry trace" do
let(:incoming_prop_context) { Sentry::PropagationContext.new(Sentry::Scope.new) }
let(:env) do
{
"HTTP_SENTRY_TRACE" => incoming_prop_context.get_traceparent,
"HTTP_BAGGAGE" => incoming_prop_context.get_baggage.serialize
}
end
it "returns nil with tracing disabled" do
expect(described_class.continue_trace(env)).to eq(nil)
end
it "sets new propagation context from env on scope" do
expect(Sentry.get_current_scope).to receive(:generate_propagation_context).and_call_original
described_class.continue_trace(env)
propagation_context = Sentry.get_current_scope.propagation_context
expect(propagation_context.incoming_trace).to eq(true)
expect(propagation_context.trace_id).to eq(incoming_prop_context.trace_id)
expect(propagation_context.parent_span_id).to eq(incoming_prop_context.span_id)
expect(propagation_context.parent_sampled).to eq(nil)
expect(propagation_context.baggage.items).to eq(incoming_prop_context.get_baggage.items)
expect(propagation_context.baggage.mutable).to eq(false)
end
it "returns new Transaction with tracing enabled" do
Sentry.configuration.traces_sample_rate = 1.0
transaction = described_class.continue_trace(env, name: "foobar")
expect(transaction).to be_a(Sentry::Transaction)
expect(transaction.name).to eq("foobar")
expect(transaction.trace_id).to eq(incoming_prop_context.trace_id)
expect(transaction.parent_span_id).to eq(incoming_prop_context.span_id)
expect(transaction.baggage.items).to eq(incoming_prop_context.get_baggage.items)
expect(transaction.baggage.mutable).to eq(false)
end
end
end
describe 'release detection' do
let(:fake_root) { "/tmp/sentry/" }
before do
allow_any_instance_of(Sentry::Configuration).to receive(:project_root).and_return(fake_root)
ENV["SENTRY_DSN"] = Sentry::TestHelper::DUMMY_DSN
end
it 'defaults to nil' do
described_class.init
expect(described_class.configuration.release).to eq(nil)
end
it "respects user's config" do
described_class.init do |config|
config.release = "foo"
end
expect(described_class.configuration.release).to eq("foo")
end
it 'uses `SENTRY_RELEASE` env variable' do
ENV['SENTRY_RELEASE'] = 'v1'
described_class.init
expect(described_class.configuration.release).to eq('v1')
ENV.delete('SENTRY_CURRENT_ENV')
end
context "when the DSN is not set" do
before do
ENV.delete("SENTRY_DSN")
end
it "doesn't detect release" do
ENV['SENTRY_RELEASE'] = 'v1'
described_class.init
expect(described_class.configuration.release).to eq(nil)
ENV.delete('SENTRY_CURRENT_ENV')
end
end
context "when the SDK is not enabled under the current env" do
it "doesn't detect release" do
ENV['SENTRY_RELEASE'] = 'v1'
described_class.init do |config|
config.enabled_environments = "production"
end
expect(described_class.configuration.release).to eq(nil)
ENV.delete('SENTRY_CURRENT_ENV')
end
end
context "when git is available" do
before do
allow(File).to receive(:directory?).and_return(false)
allow(File).to receive(:directory?).with(".git").and_return(true)
end
it 'gets release from git' do
allow(Sentry).to receive(:`).with("git rev-parse HEAD 2>&1").and_return("COMMIT_SHA")
described_class.init
expect(described_class.configuration.release).to eq('COMMIT_SHA')
end
end
context "when Capistrano is available" do
let(:revision) { "2019010101000" }
before do
Dir.mkdir(fake_root) unless Dir.exist?(fake_root)
File.write(filename, file_content)
end
after do
File.delete(filename)
Dir.delete(fake_root)
end
context "when the REVISION file is present" do
let(:filename) do
File.join(fake_root, "REVISION")
end
let(:file_content) { revision }
it "gets release from the REVISION file" do
described_class.init
expect(described_class.configuration.release).to eq(revision)
end
end
context "when the revisions.log file is present" do
let(:filename) do
File.join(fake_root, "..", "revisions.log")
end
let(:file_content) do
"Branch master (at COMMIT_SHA) deployed as release #{revision} by alice"
end
it "gets release from the REVISION file" do
described_class.init
expect(described_class.configuration.release).to eq(revision)
end
end
end
context "when running on heroku" do
before do
allow(File).to receive(:directory?).and_return(false)
allow(File).to receive(:directory?).with("/etc/heroku").and_return(true)
end
context "when it's on heroku ci" do
it "returns nil" do
begin
original_ci_val = ENV["CI"]
ENV["CI"] = "true"
described_class.init
expect(described_class.configuration.release).to eq(nil)
ensure
ENV["CI"] = original_ci_val
end
end
end
context "when it's not on heroku ci" do
around do |example|
begin
original_ci_val = ENV["CI"]
ENV["CI"] = nil
example.run
ensure
ENV["CI"] = original_ci_val
end
end
it "returns nil + logs an warning if HEROKU_SLUG_COMMIT is not set" do
string_io = StringIO.new
logger = Logger.new(string_io)
described_class.init do |config|
config.logger = logger
end
expect(described_class.configuration.release).to eq(nil)
expect(string_io.string).to include(Sentry::Configuration::HEROKU_DYNO_METADATA_MESSAGE)