-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
SentryAutoConfigurationTest.kt
931 lines (817 loc) · 39.2 KB
/
SentryAutoConfigurationTest.kt
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
package io.sentry.spring.boot
import com.acme.MainBootClass
import io.sentry.AsyncHttpTransportFactory
import io.sentry.Breadcrumb
import io.sentry.EventProcessor
import io.sentry.Hint
import io.sentry.IHub
import io.sentry.ITransportFactory
import io.sentry.Integration
import io.sentry.NoOpTransportFactory
import io.sentry.SamplingContext
import io.sentry.Sentry
import io.sentry.SentryEvent
import io.sentry.SentryLevel
import io.sentry.SentryOptions
import io.sentry.checkEvent
import io.sentry.opentelemetry.OpenTelemetryLinkErrorEventProcessor
import io.sentry.protocol.SentryTransaction
import io.sentry.protocol.User
import io.sentry.spring.ContextTagsEventProcessor
import io.sentry.spring.HttpServletRequestSentryUserProvider
import io.sentry.spring.SentryExceptionResolver
import io.sentry.spring.SentryUserFilter
import io.sentry.spring.SentryUserProvider
import io.sentry.spring.SpringSecuritySentryUserProvider
import io.sentry.spring.tracing.SentryTracingFilter
import io.sentry.transport.ITransport
import io.sentry.transport.ITransportGate
import io.sentry.transport.apache.ApacheHttpClientTransportFactory
import org.aspectj.lang.ProceedingJoinPoint
import org.assertj.core.api.Assertions.assertThat
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.slf4j.MDC
import org.springframework.aop.support.NameMatchMethodPointcut
import org.springframework.boot.autoconfigure.AutoConfigurations
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
import org.springframework.boot.context.annotation.UserConfigurations
import org.springframework.boot.info.GitProperties
import org.springframework.boot.test.context.FilteredClassLoader
import org.springframework.boot.test.context.assertj.ApplicationContextAssert
import org.springframework.boot.test.context.runner.WebApplicationContextRunner
import org.springframework.boot.web.servlet.FilterRegistrationBean
import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.Ordered
import org.springframework.core.annotation.Order
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.web.client.RestTemplate
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.servlet.HandlerExceptionResolver
import java.lang.RuntimeException
import javax.servlet.Filter
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class SentryAutoConfigurationTest {
private val contextRunner = WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(SentryAutoConfiguration::class.java, WebMvcAutoConfiguration::class.java))
@Test
fun `hub is not created when auto-configuration dsn is not set`() {
contextRunner
.run {
assertThat(it).doesNotHaveBean(IHub::class.java)
}
}
@Test
fun `hub is created when dsn is provided`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.run {
assertThat(it).hasSingleBean(IHub::class.java)
}
}
@Test
fun `OptionsConfiguration is created if custom one with name sentryOptionsConfiguration is not provided`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.run {
assertThat(it).hasSingleBean(Sentry.OptionsConfiguration::class.java)
}
}
@Test
fun `OptionsConfiguration with name sentryOptionsConfiguration is created if another one with different name is provided`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.withUserConfiguration(CustomOptionsConfigurationConfiguration::class.java)
.run {
assertThat(it).getBeans(Sentry.OptionsConfiguration::class.java).hasSize(2)
assertThat(it).getBean("sentryOptionsConfiguration")
.isNotNull()
.isInstanceOf(Sentry.OptionsConfiguration::class.java)
assertThat(it).getBean("customOptionsConfiguration")
.isNotNull()
.isInstanceOf(Sentry.OptionsConfiguration::class.java)
}
}
@Test
fun `sentryOptionsConfiguration bean is configured before custom OptionsConfiguration`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.withUserConfiguration(CustomOptionsConfigurationConfiguration::class.java)
.run {
val options = it.getBean(SentryOptions::class.java)
assertThat(options.beforeSend).isNull()
}
}
@Test
fun `OptionsConfiguration is not created if custom one with name sentryOptionsConfiguration is provided`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.withUserConfiguration(OverridingOptionsConfigurationConfiguration::class.java)
.run {
assertThat(it).hasSingleBean(Sentry.OptionsConfiguration::class.java)
assertThat(it.getBean(Sentry.OptionsConfiguration::class.java, "customOptionsConfiguration")).isNotNull
}
}
@Test
fun `properties are applied to SentryOptions`() {
contextRunner.withPropertyValues(
"sentry.dsn=http://key@localhost/proj",
"sentry.read-timeout-millis=10",
"sentry.shutdown-timeout=20",
"sentry.flush-timeout-millis=30",
"sentry.debug=true",
"sentry.diagnostic-level=INFO",
"sentry.sentry-client-name=my-client",
"sentry.max-breadcrumbs=100",
"sentry.release=1.0.3",
"sentry.environment=production",
"sentry.sample-rate=0.2",
"sentry.in-app-includes=org.springframework,com.myapp",
"sentry.in-app-excludes=org.jboss,com.microsoft",
"sentry.dist=my-dist",
"sentry.attach-threads=true",
"sentry.attach-stacktrace=true",
"sentry.server-name=host-001",
"sentry.exception-resolver-order=100",
"sentry.proxy.host=example.proxy.com",
"sentry.proxy.port=8090",
"sentry.proxy.user=proxy-user",
"sentry.proxy.pass=proxy-pass",
"sentry.traces-sample-rate=0.3",
"sentry.tags.tag1=tag1-value",
"sentry.tags.tag2=tag2-value",
"sentry.ignored-exceptions-for-type=java.lang.RuntimeException,java.lang.IllegalStateException,io.sentry.Sentry",
"sentry.trace-propagation-targets=localhost,^(http|https)://api\\..*\$"
).run {
val options = it.getBean(SentryProperties::class.java)
assertThat(options.readTimeoutMillis).isEqualTo(10)
assertThat(options.shutdownTimeoutMillis).isEqualTo(20)
assertThat(options.flushTimeoutMillis).isEqualTo(30)
assertThat(options.isDebug).isTrue()
assertThat(options.diagnosticLevel).isEqualTo(SentryLevel.INFO)
assertThat(options.maxBreadcrumbs).isEqualTo(100)
assertThat(options.release).isEqualTo("1.0.3")
assertThat(options.environment).isEqualTo("production")
assertThat(options.sampleRate).isEqualTo(0.2)
assertThat(options.inAppIncludes).containsOnly("org.springframework", "com.myapp")
assertThat(options.inAppExcludes).containsOnly("com.microsoft", "org.jboss")
assertThat(options.dist).isEqualTo("my-dist")
assertThat(options.isAttachThreads).isEqualTo(true)
assertThat(options.isAttachStacktrace).isEqualTo(true)
assertThat(options.serverName).isEqualTo("host-001")
assertThat(options.exceptionResolverOrder).isEqualTo(100)
assertThat(options.proxy).isNotNull
assertThat(options.proxy!!.host).isEqualTo("example.proxy.com")
assertThat(options.proxy!!.port).isEqualTo("8090")
assertThat(options.proxy!!.user).isEqualTo("proxy-user")
assertThat(options.proxy!!.pass).isEqualTo("proxy-pass")
assertThat(options.tracesSampleRate).isEqualTo(0.3)
assertThat(options.tags).containsEntry("tag1", "tag1-value").containsEntry("tag2", "tag2-value")
assertThat(options.ignoredExceptionsForType).containsOnly(RuntimeException::class.java, IllegalStateException::class.java)
assertThat(options.tracePropagationTargets).containsOnly("localhost", "^(http|https)://api\\..*\$")
}
}
@Test
fun `when tracePropagationTargets are not set, default is returned`() {
contextRunner.withPropertyValues(
"sentry.dsn=http://key@localhost/proj"
).run {
val options = it.getBean(SentryProperties::class.java)
assertThat(options.tracePropagationTargets).isNotNull().containsOnly(".*")
}
}
@Test
fun `when tracePropagationTargets property is set to empty list, empty list is returned`() {
contextRunner.withPropertyValues(
"sentry.dsn=http://key@localhost/proj",
"sentry.trace-propagation-targets="
).run {
val options = it.getBean(SentryProperties::class.java)
assertThat(options.tracePropagationTargets).isNotNull().isEmpty()
}
}
@Test
fun `when setting tracingOrigins it still works`() {
contextRunner.withPropertyValues(
"sentry.dsn=http://key@localhost/proj",
"sentry.tracing-origins=somehost,otherhost"
).run {
val options = it.getBean(SentryProperties::class.java)
assertThat(options.tracePropagationTargets).isNotNull().isEqualTo(listOf("somehost", "otherhost"))
}
}
@Test
fun `when traces sample rate is set to null and tracing is enabled, traces sample rate should be set to 0`() {
contextRunner.withPropertyValues(
"sentry.dsn=http://key@localhost/proj"
).run {
val options = it.getBean(SentryProperties::class.java)
assertThat(options.tracesSampleRate).isNotNull().isEqualTo(0.0)
}
}
@Test
fun `when traces sample rate is set to a value and tracing is enabled, traces sample rate should not be overwritten`() {
contextRunner.withPropertyValues(
"sentry.dsn=http://key@localhost/proj",
"sentry.traces-sample-rate=0.3"
).run {
val options = it.getBean(SentryProperties::class.java)
assertThat(options.tracesSampleRate).isNotNull().isEqualTo(0.3)
}
}
@Test
fun `sets sentryClientName property on SentryOptions`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.run {
assertThat(it.getBean(SentryOptions::class.java).sentryClientName).isEqualTo("sentry.java.spring-boot${optionalJakartaPrefix()}")
}
}
@Test
fun `sets SDK version on sent events`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.withUserConfiguration(MockTransportConfiguration::class.java)
.run {
Sentry.captureMessage("Some message")
val transport = it.getBean(ITransport::class.java)
verify(transport).send(
checkEvent { event ->
assertThat(event.sdk).isNotNull()
val sdk = event.sdk!!
assertThat(sdk.version).isEqualTo(BuildConfig.VERSION_NAME)
assertThat(sdk.name).isEqualTo(BuildConfig.SENTRY_SPRING_BOOT_SDK_NAME)
assertThat(sdk.packages).anyMatch { pkg ->
pkg.name == "maven:io.sentry:sentry-spring-boot-starter" && pkg.version == BuildConfig.VERSION_NAME
}
},
anyOrNull()
)
}
}
@Test
fun `registers beforeSendCallback on SentryOptions`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.withUserConfiguration(CustomBeforeSendCallbackConfiguration::class.java)
.run {
assertThat(it.getBean(SentryOptions::class.java).beforeSend).isInstanceOf(CustomBeforeSendCallback::class.java)
}
}
@Test
fun `registers beforeSendTransactionCallback on SentryOptions`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.withUserConfiguration(CustomBeforeSendTransactionCallbackConfiguration::class.java)
.run {
assertThat(it.getBean(SentryOptions::class.java).beforeSendTransaction).isInstanceOf(CustomBeforeSendTransactionCallback::class.java)
}
}
@Test
fun `registers beforeBreadcrumbCallback on SentryOptions`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.withUserConfiguration(CustomBeforeBreadcrumbCallbackConfiguration::class.java)
.run {
assertThat(it.getBean(SentryOptions::class.java).beforeBreadcrumb).isInstanceOf(CustomBeforeBreadcrumbCallback::class.java)
}
}
@Test
fun `registers event processor on SentryOptions`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.withUserConfiguration(CustomEventProcessorConfiguration::class.java)
.run {
assertThat(it.getBean(SentryOptions::class.java).eventProcessors).anyMatch { processor -> processor.javaClass == CustomEventProcessor::class.java }
}
}
@Test
fun `registers transport gate on SentryOptions`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.withUserConfiguration(CustomTransportGateConfiguration::class.java)
.run {
assertThat(it.getBean(SentryOptions::class.java).transportGate).isInstanceOf(CustomTransportGate::class.java)
}
}
@Test
fun `registers custom integration on SentryOptions`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.withUserConfiguration(CustomIntegration::class.java)
.run {
assertThat(it.getBean(SentryOptions::class.java).integrations).anyMatch { integration -> integration.javaClass == CustomIntegration::class.java }
}
}
@Test
fun `sets release on SentryEvents if Git integration is configured`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.withUserConfiguration(MockTransportConfiguration::class.java, MockGitPropertiesConfiguration::class.java)
.run {
Sentry.captureMessage("Some message")
val transport = it.getBean(ITransport::class.java)
verify(transport).send(
checkEvent { event ->
assertThat(event.release).isEqualTo("git-commit-id")
},
anyOrNull()
)
}
}
@Test
fun `sets custom release on SentryEvents if release property is set and Git integration is configured`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.release=my-release")
.withUserConfiguration(MockTransportConfiguration::class.java, MockGitPropertiesConfiguration::class.java)
.run {
Sentry.captureMessage("Some message")
val transport = it.getBean(ITransport::class.java)
verify(transport).send(
checkEvent { event ->
assertThat(event.release).isEqualTo("my-release")
},
anyOrNull()
)
}
}
@Test
fun `sets inAppIncludes on SentryOptions from a class annotated with @SpringBootApplication`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.withUserConfiguration(MainBootClass::class.java)
.run {
assertThat(it.getBean(SentryProperties::class.java).inAppIncludes)
.containsOnly("com.acme")
}
}
@Test
fun `when custom SentryUserProvider bean is configured, it's added after HttpServletRequestSentryUserProvider`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.send-default-pii=true")
.withConfiguration(UserConfigurations.of(SentryUserProviderConfiguration::class.java))
.run {
val userProviders = it.getSentryUserProviders()
assertEquals(3, userProviders.size)
assertTrue(userProviders[0] is HttpServletRequestSentryUserProvider)
assertTrue(userProviders[1] is SpringSecuritySentryUserProvider)
assertTrue(userProviders[2] is CustomSentryUserProvider)
}
}
@Test
fun `when custom SentryUserProvider bean with higher order is configured, it's added before HttpServletRequestSentryUserProvider`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.send-default-pii=true")
.withConfiguration(UserConfigurations.of(SentryHighestOrderUserProviderConfiguration::class.java))
.run {
val userProviders = it.getSentryUserProviders()
assertEquals(3, userProviders.size)
assertTrue(userProviders[0] is CustomSentryUserProvider)
assertTrue(userProviders[1] is HttpServletRequestSentryUserProvider)
assertTrue(userProviders[2] is SpringSecuritySentryUserProvider)
}
}
@Test
fun `when Spring Security is not on the classpath, SpringSecuritySentryUserProvider is not configured`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.send-default-pii=true")
.withClassLoader(FilteredClassLoader(SecurityContextHolder::class.java))
.run { ctx ->
val userProviders = ctx.getSentryUserProviders()
assertTrue(userProviders.isNotEmpty())
userProviders.forEach {
assertFalse(it is SpringSecuritySentryUserProvider)
}
}
}
@Test
fun `when Spring MVC is not on the classpath, SentryExceptionResolver is not configured`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.send-default-pii=true")
.withClassLoader(FilteredClassLoader(HandlerExceptionResolver::class.java))
.run {
assertThat(it).doesNotHaveBean(SentryExceptionResolver::class.java)
}
}
@Test
fun `when tracing is enabled, creates tracing filter`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.traces-sample-rate=1.0")
.run {
assertThat(it).hasBean("sentryTracingFilter")
}
}
@Test
fun `when traces sample rate is set, creates tracing filter`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.traces-sample-rate=0.2")
.run {
assertThat(it).hasBean("sentryTracingFilter")
}
}
@Test
fun `when enable tracing is set to false and traces sample rate is set, creates tracing filter`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.traces-sample-rate=0.2")
.run {
assertThat(it).hasBean("sentryTracingFilter")
}
}
@Test
fun `when traces sample rate is set to 0, creates tracing filter`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.traces-sample-rate=0.0")
.run {
assertThat(it).hasBean("sentryTracingFilter")
}
}
@Test
fun `when custom traces sampler callback is registered, creates tracing filter`() {
contextRunner.withUserConfiguration(CustomTracesSamplerCallbackConfiguration::class.java)
.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.run {
assertThat(it).hasBean("sentryTracingFilter")
}
}
@Test
fun `when tracing is set, does not create tracing filter`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.run {
assertThat(it).doesNotHaveBean("sentryTracingFilter")
}
}
@Test
fun `when tracing is disabled, does not create tracing filter`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.run {
assertThat(it).doesNotHaveBean("sentryTracingFilter")
}
}
@Test
fun `when tracing is enabled and sentryTracingFilter already exists, does not create tracing filter`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.traces-sample-rate=1.0")
.withUserConfiguration(CustomSentryTracingFilter::class.java)
.run {
assertThat(it).hasBean("sentryTracingFilter")
val filter = it.getBean("sentryTracingFilter")
if (filter is FilterRegistrationBean<*>) {
assertThat(filter.filter).isNotInstanceOf(SentryTracingFilter::class.java)
} else {
assertThat(filter).isNotInstanceOf(SentryTracingFilter::class.java)
}
}
}
@Test
fun `when tracing is enabled creates AOP beans to support @SentryTransaction`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.traces-sample-rate=1.0")
.run {
assertThat(it).hasSentryTransactionBeans()
}
}
@Test
fun `when traces sample rate is set to 0, creates AOP beans to support @SentryTransaction`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.traces-sample-rate=0.0")
.run {
assertThat(it).hasSentryTransactionBeans()
}
}
@Test
fun `when custom traces sampler callback is registered, creates AOP beans to support @SentryTransaction`() {
contextRunner.withUserConfiguration(CustomTracesSamplerCallbackConfiguration::class.java)
.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.run {
assertThat(it).hasSentryTransactionBeans()
}
}
@Test
fun `when tracing is disabled, does not create AOP beans to support @SentryTransaction`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.run {
assertThat(it).doesNotHaveSentryTransactionBeans()
}
}
@Test
fun `when Spring AOP is not on the classpath, does not create AOP beans to support @SentryTransaction`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.traces-sample-rate=1.0")
.withClassLoader(FilteredClassLoader(ProceedingJoinPoint::class.java))
.run {
assertThat(it).doesNotHaveSentryTransactionBeans()
}
}
@Test
fun `when tracing is enabled and custom sentryTransactionPointcut is provided, sentryTransactionPointcut bean is not created`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.traces-sample-rate=1.0")
.withUserConfiguration(CustomSentryPerformancePointcutConfiguration::class.java)
.run {
assertThat(it).hasBean("sentryTransactionPointcut")
val pointcut = it.getBean("sentryTransactionPointcut")
assertThat(pointcut).isInstanceOf(NameMatchMethodPointcut::class.java)
}
}
@Test
fun `when tracing is enabled creates AOP beans to support @SentrySpan`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.traces-sample-rate=1.0")
.run {
assertThat(it).hasSentrySpanBeans()
}
}
@Test
fun `when traces sample rate is set to 0, creates AOP beans to support @SentrySpan`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.traces-sample-rate=0.0")
.run {
assertThat(it).hasSentrySpanBeans()
}
}
@Test
fun `when custom traces sampler callback is registered, creates AOP beans to support @SentrySpan`() {
contextRunner.withUserConfiguration(CustomTracesSamplerCallbackConfiguration::class.java)
.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.run {
assertThat(it).hasSentrySpanBeans()
}
}
@Test
fun `when tracing is disabled, does not create AOP beans to support @Span`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.run {
assertThat(it).doesNotHaveSentrySpanBeans()
}
}
@Test
fun `when Spring AOP is not on the classpath, does not create AOP beans to support @SentrySpan`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.traces-sample-rate=1.0")
.withClassLoader(FilteredClassLoader(ProceedingJoinPoint::class.java))
.run {
assertThat(it).doesNotHaveSentrySpanBeans()
}
}
@Test
fun `when tracing is enabled and custom sentrySpanPointcut is provided, sentrySpanPointcut bean is not created`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.traces-sample-rate=1.0")
.withUserConfiguration(CustomSentryPerformancePointcutConfiguration::class.java)
.run {
assertThat(it).hasBean("sentrySpanPointcut")
val pointcut = it.getBean("sentrySpanPointcut")
assertThat(pointcut).isInstanceOf(NameMatchMethodPointcut::class.java)
}
}
@Test
fun `when tracing is enabled and RestTemplate is on the classpath, SentrySpanRestTemplateCustomizer bean is created`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.traces-sample-rate=1.0")
.run {
assertThat(it).hasSingleBean(SentrySpanRestTemplateCustomizer::class.java)
}
}
@Test
fun `when tracing is enabled and RestTemplate is not on the classpath, SentrySpanRestTemplateCustomizer bean is not created`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.traces-sample-rate=1.0")
.withClassLoader(FilteredClassLoader(RestTemplate::class.java))
.run {
assertThat(it).doesNotHaveBean(SentrySpanRestTemplateCustomizer::class.java)
}
}
@Test
fun `when tracing is enabled and WebClient is on the classpath, SentrySpanWebClientCustomizer bean is created`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.traces-sample-rate=1.0")
.run {
assertThat(it).hasSingleBean(SentrySpanWebClientCustomizer::class.java)
}
}
@Test
fun `when tracing is enabled and WebClient is not on the classpath, SentrySpanWebClientCustomizer bean is not created`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.traces-sample-rate=1.0")
.withClassLoader(FilteredClassLoader(WebClient::class.java))
.run {
assertThat(it).doesNotHaveBean(SentrySpanWebClientCustomizer::class.java)
}
}
@Test
fun `registers tracesSamplerCallback on SentryOptions`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.withUserConfiguration(CustomTracesSamplerCallbackConfiguration::class.java)
.run {
assertThat(it.getBean(SentryOptions::class.java).tracesSampler).isInstanceOf(CustomTracesSamplerCallback::class.java)
}
}
@Test
fun `when sentry-apache-http-client-5 is on the classpath, creates apache transport factory`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.run {
assertThat(it.getBean(SentryOptions::class.java).transportFactory).isInstanceOf(ApacheHttpClientTransportFactory::class.java)
}
}
@Test
fun `when sentry-apache-http-client-5 is not on the classpath, does not create apache transport factory`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.withClassLoader(FilteredClassLoader(ApacheHttpClientTransportFactory::class.java))
.run {
assertThat(it.getBean(SentryOptions::class.java).transportFactory).isInstanceOf(AsyncHttpTransportFactory::class.java)
}
}
@Test
fun `when sentry-apache-http-client-5 is on the classpath and custom transport factory bean is set, does not create apache transport factory`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.withUserConfiguration(MockTransportConfiguration::class.java)
.run {
assertThat(it.getBean(SentryOptions::class.java).transportFactory)
.isNotInstanceOf(ApacheHttpClientTransportFactory::class.java)
.isNotInstanceOf(NoOpTransportFactory::class.java)
}
}
@Test
fun `when MDC is on the classpath, creates ContextTagsEventProcessor`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.run {
assertThat(it).hasSingleBean(ContextTagsEventProcessor::class.java)
val options = it.getBean(SentryOptions::class.java)
assertThat(options.eventProcessors).anyMatch { processor -> processor.javaClass == ContextTagsEventProcessor::class.java }
}
}
@Test
fun `when MDC is not on the classpath, does not create ContextTagsEventProcessor`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.withClassLoader(FilteredClassLoader(MDC::class.java))
.run {
assertThat(it).doesNotHaveBean(ContextTagsEventProcessor::class.java)
val options = it.getBean(SentryOptions::class.java)
assertThat(options.eventProcessors).noneMatch { processor -> processor.javaClass == ContextTagsEventProcessor::class.java }
}
}
@Test
fun `when OpenTelemetryLinkErrorEventProcessor is on the classpath and auto init off, creates OpenTelemetryLinkErrorEventProcessor`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.auto-init=false")
.run {
assertThat(it).hasSingleBean(OpenTelemetryLinkErrorEventProcessor::class.java)
val options = it.getBean(SentryOptions::class.java)
assertThat(options.eventProcessors).anyMatch { processor -> processor.javaClass == OpenTelemetryLinkErrorEventProcessor::class.java }
}
}
@Test
fun `when OpenTelemetryLinkErrorEventProcessor is on the classpath but auto init on, does not create OpenTelemetryLinkErrorEventProcessor`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.auto-init=true")
.run {
assertThat(it).doesNotHaveBean(OpenTelemetryLinkErrorEventProcessor::class.java)
val options = it.getBean(SentryOptions::class.java)
assertThat(options.eventProcessors).noneMatch { processor -> processor.javaClass == OpenTelemetryLinkErrorEventProcessor::class.java }
}
}
@Test
fun `when OpenTelemetryLinkErrorEventProcessor is on the classpath but auto init default, does not create OpenTelemetryLinkErrorEventProcessor`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
.run {
assertThat(it).doesNotHaveBean(OpenTelemetryLinkErrorEventProcessor::class.java)
val options = it.getBean(SentryOptions::class.java)
assertThat(options.eventProcessors).noneMatch { processor -> processor.javaClass == OpenTelemetryLinkErrorEventProcessor::class.java }
}
}
@Test
fun `when OpenTelemetryLinkErrorEventProcessor is not on the classpath, does not create OpenTelemetryLinkErrorEventProcessor`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.auto-init=false")
.withClassLoader(FilteredClassLoader(OpenTelemetryLinkErrorEventProcessor::class.java))
.run {
assertThat(it).doesNotHaveBean(OpenTelemetryLinkErrorEventProcessor::class.java)
val options = it.getBean(SentryOptions::class.java)
assertThat(options.eventProcessors).noneMatch { processor -> processor.javaClass == OpenTelemetryLinkErrorEventProcessor::class.java }
}
}
@Configuration(proxyBeanMethods = false)
open class CustomOptionsConfigurationConfiguration {
@Bean
open fun customOptionsConfiguration() = Sentry.OptionsConfiguration<SentryOptions> {
it.setBeforeSend(null)
}
@Bean
open fun beforeSendCallback() = CustomBeforeSendCallback()
}
@Configuration(proxyBeanMethods = false)
open class OverridingOptionsConfigurationConfiguration {
@Bean
open fun sentryOptionsConfiguration() = Sentry.OptionsConfiguration<SentryOptions> {
}
}
@Configuration(proxyBeanMethods = false)
open class MockTransportConfiguration {
private val transport = mock<ITransport>()
@Bean
open fun mockTransportFactory(): ITransportFactory {
val factory = mock<ITransportFactory>()
whenever(factory.create(any(), any())).thenReturn(transport)
return factory
}
@Bean
open fun sentryTransport() = transport
}
@Configuration(proxyBeanMethods = false)
open class CustomBeforeSendCallbackConfiguration {
@Bean
open fun beforeSendCallback() = CustomBeforeSendCallback()
}
class CustomBeforeSendCallback : SentryOptions.BeforeSendCallback {
override fun execute(event: SentryEvent, hint: Hint): SentryEvent? = null
}
@Configuration(proxyBeanMethods = false)
open class CustomBeforeSendTransactionCallbackConfiguration {
@Bean
open fun beforeSendTransactionCallback() = CustomBeforeSendTransactionCallback()
}
class CustomBeforeSendTransactionCallback : SentryOptions.BeforeSendTransactionCallback {
override fun execute(event: SentryTransaction, hint: Hint): SentryTransaction? = null
}
@Configuration(proxyBeanMethods = false)
open class CustomBeforeBreadcrumbCallbackConfiguration {
@Bean
open fun beforeBreadcrumbCallback() = CustomBeforeBreadcrumbCallback()
}
class CustomBeforeBreadcrumbCallback : SentryOptions.BeforeBreadcrumbCallback {
override fun execute(breadcrumb: Breadcrumb, hint: Hint): Breadcrumb? = null
}
@Configuration(proxyBeanMethods = false)
open class CustomEventProcessorConfiguration {
@Bean
open fun customEventProcessor() = CustomEventProcessor()
}
class CustomEventProcessor : EventProcessor {
override fun process(event: SentryEvent, hint: Hint) = null
}
@Configuration(proxyBeanMethods = false)
open class CustomIntegrationConfiguration {
@Bean
open fun customIntegration() = CustomIntegration()
}
class CustomIntegration : Integration {
override fun register(hub: IHub, options: SentryOptions) {}
}
@Configuration(proxyBeanMethods = false)
open class CustomTransportGateConfiguration {
@Bean
open fun customTransportGate() = CustomTransportGate()
}
class CustomTransportGate : ITransportGate {
override fun isConnected() = true
}
@Configuration(proxyBeanMethods = false)
open class MockGitPropertiesConfiguration {
@Bean
open fun gitProperties(): GitProperties {
val git = mock<GitProperties>()
whenever(git.commitId).thenReturn("git-commit-id")
return git
}
}
@Configuration
open class SentryUserProviderConfiguration {
@Bean
open fun userProvider() = CustomSentryUserProvider()
}
@Configuration
open class SentryHighestOrderUserProviderConfiguration {
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
open fun userProvider() = CustomSentryUserProvider()
}
@Configuration
open class CustomSentryTracingFilter {
@Bean
open fun sentryTracingFilter() = mock<Filter>()
}
@Configuration
open class CustomSentryPerformancePointcutConfiguration {
@Bean
open fun sentryTransactionPointcut() = NameMatchMethodPointcut()
@Bean
open fun sentrySpanPointcut() = NameMatchMethodPointcut()
}
@Configuration
open class CustomTracesSamplerCallbackConfiguration {
@Bean
open fun tracingSamplerCallback() = CustomTracesSamplerCallback()
}
class CustomTracesSamplerCallback : SentryOptions.TracesSamplerCallback {
override fun sample(samplingContext: SamplingContext) = 1.0
}
open class CustomSentryUserProvider : SentryUserProvider {
override fun provideUser(): User? {
val user = User()
user.username = "john.smith"
return user
}
}
private fun <C : ApplicationContext> ApplicationContextAssert<C>.hasSentryTransactionBeans(): ApplicationContextAssert<C> {
this.hasBean("sentryTransactionPointcut")
this.hasBean("sentryTransactionAdvice")
this.hasBean("sentryTransactionAdvisor")
return this
}
private fun <C : ApplicationContext> ApplicationContextAssert<C>.doesNotHaveSentryTransactionBeans(): ApplicationContextAssert<C> {
this.doesNotHaveBean("sentryTransactionPointcut")
this.doesNotHaveBean("sentryTransactionAdvice")
this.doesNotHaveBean("sentryTransactionAdvisor")
return this
}
private fun <C : ApplicationContext> ApplicationContextAssert<C>.hasSentrySpanBeans(): ApplicationContextAssert<C> {
this.hasBean("sentrySpanPointcut")
this.hasBean("sentrySpanAdvice")
this.hasBean("sentrySpanAdvisor")
return this
}
private fun <C : ApplicationContext> ApplicationContextAssert<C>.doesNotHaveSentrySpanBeans(): ApplicationContextAssert<C> {
this.doesNotHaveBean("sentrySpanPointcut")
this.doesNotHaveBean("sentrySpanAdvice")
this.doesNotHaveBean("sentrySpanAdvisor")
return this
}
private fun ApplicationContext.getSentryUserProviders(): List<SentryUserProvider> {
val userFilter = this.getBean("sentryUserFilter", FilterRegistrationBean::class.java).filter as SentryUserFilter
return userFilter.sentryUserProviders
}
private fun optionalJakartaPrefix(): String {
if (this.javaClass.packageName.endsWith("jakarta")) {
return ".jakarta"
}
return ""
}
}