From 90211b62bb0c5d62dea8b31f87caf17579ecd5a1 Mon Sep 17 00:00:00 2001 From: Maciej Walkowiak Date: Thu, 3 Dec 2020 14:32:19 +0100 Subject: [PATCH 1/2] Add the ability to register multiple OptionsConfiguration beans. --- .../api/sentry-spring-boot-starter.api | 1 + .../spring/boot/InAppIncludesResolver.java | 2 +- .../spring/boot/SentryAutoConfiguration.java | 11 +++-- .../boot/SentryAutoConfigurationTest.kt | 41 ++++++++++++++++++- 4 files changed, 48 insertions(+), 7 deletions(-) diff --git a/sentry-spring-boot-starter/api/sentry-spring-boot-starter.api b/sentry-spring-boot-starter/api/sentry-spring-boot-starter.api index 9c79946bf0..34ea548614 100644 --- a/sentry-spring-boot-starter/api/sentry-spring-boot-starter.api +++ b/sentry-spring-boot-starter/api/sentry-spring-boot-starter.api @@ -5,6 +5,7 @@ public final class io/sentry/spring/boot/BuildConfig { public class io/sentry/spring/boot/InAppIncludesResolver : org/springframework/context/ApplicationContextAware { public fun ()V + public fun resolveInAppIncludes ()Ljava/util/List; public fun setApplicationContext (Lorg/springframework/context/ApplicationContext;)V } diff --git a/sentry-spring-boot-starter/src/main/java/io/sentry/spring/boot/InAppIncludesResolver.java b/sentry-spring-boot-starter/src/main/java/io/sentry/spring/boot/InAppIncludesResolver.java index 1d589675f2..e4d3ad5438 100644 --- a/sentry-spring-boot-starter/src/main/java/io/sentry/spring/boot/InAppIncludesResolver.java +++ b/sentry-spring-boot-starter/src/main/java/io/sentry/spring/boot/InAppIncludesResolver.java @@ -23,7 +23,7 @@ public class InAppIncludesResolver implements ApplicationContextAware { private @Nullable ApplicationContext applicationContext; @Nullable - List resolveInAppIncludes() { + public List resolveInAppIncludes() { if (applicationContext != null) { Map beansWithAnnotation = applicationContext.getBeansWithAnnotation(SpringBootConfiguration.class); diff --git a/sentry-spring-boot-starter/src/main/java/io/sentry/spring/boot/SentryAutoConfiguration.java b/sentry-spring-boot-starter/src/main/java/io/sentry/spring/boot/SentryAutoConfiguration.java index 6fdc93639e..6f185053da 100644 --- a/sentry-spring-boot-starter/src/main/java/io/sentry/spring/boot/SentryAutoConfiguration.java +++ b/sentry-spring-boot-starter/src/main/java/io/sentry/spring/boot/SentryAutoConfiguration.java @@ -43,6 +43,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; import org.springframework.web.client.RestTemplate; @Configuration @@ -57,8 +58,9 @@ public class SentryAutoConfiguration { static class HubConfiguration { @Bean - @ConditionalOnMissingBean - public @NotNull Sentry.OptionsConfiguration optionsOptionsConfiguration( + @ConditionalOnMissingBean(name = "sentryOptionsConfiguration") + @Order(Ordered.HIGHEST_PRECEDENCE) + public @NotNull Sentry.OptionsConfiguration sentryOptionsConfiguration( final @NotNull ObjectProvider beforeSendCallback, final @NotNull ObjectProvider beforeBreadcrumbCallback, @@ -92,10 +94,11 @@ static class HubConfiguration { @Bean public @NotNull IHub sentryHub( - final @NotNull Sentry.OptionsConfiguration optionsConfiguration, + final @NotNull List> optionsConfigurations, final @NotNull SentryProperties options, final @NotNull ObjectProvider gitProperties) { - optionsConfiguration.configure(options); + optionsConfigurations.forEach( + optionsConfiguration -> optionsConfiguration.configure(options)); gitProperties.ifAvailable( git -> { if (options.getRelease() == null && options.isUseGitCommitIdAsRelease()) { diff --git a/sentry-spring-boot-starter/src/test/kotlin/io/sentry/spring/boot/SentryAutoConfigurationTest.kt b/sentry-spring-boot-starter/src/test/kotlin/io/sentry/spring/boot/SentryAutoConfigurationTest.kt index 4358c45cdc..61febd0a9e 100644 --- a/sentry-spring-boot-starter/src/test/kotlin/io/sentry/spring/boot/SentryAutoConfigurationTest.kt +++ b/sentry-spring-boot-starter/src/test/kotlin/io/sentry/spring/boot/SentryAutoConfigurationTest.kt @@ -64,7 +64,7 @@ class SentryAutoConfigurationTest { } @Test - fun `OptionsConfiguration is created if custom one is not provided`() { + 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) @@ -72,9 +72,34 @@ class SentryAutoConfigurationTest { } @Test - fun `OptionsConfiguration is not created if custom one is provided`() { + 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 @@ -432,6 +457,18 @@ class SentryAutoConfigurationTest { @Bean open fun customOptionsConfiguration() = Sentry.OptionsConfiguration { + it.setBeforeSend(null) + } + + @Bean + open fun beforeSendCallback() = CustomBeforeSendCallback() + } + + @Configuration(proxyBeanMethods = false) + open class OverridingOptionsConfigurationConfiguration { + + @Bean + open fun sentryOptionsConfiguration() = Sentry.OptionsConfiguration { } } From 5621eb18f7b95ebc72e039c1737ef7b50080de82 Mon Sep 17 00:00:00 2001 From: Maciej Walkowiak Date: Thu, 3 Dec 2020 14:34:53 +0100 Subject: [PATCH 2/2] Changelog. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index afc178f791..f629f04a39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * Enhancement: Resolve in-app-includes and in-app-excludes parameters from the external configuration * Enhancement: Make InAppIncludesResolver public (#1084) * Ref: Change "op" to "operation" in @SentrySpan and @SentryTransaction +* Enhancement: Add the ability to register multiple OptionsConfiguration beans (#1093) # 4.0.0-alpha.1