From 2f59bffbf029e273f249414aad3e0aca50becee9 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 11 Feb 2025 14:40:45 +0100 Subject: [PATCH] fixes --- sentry-reactor/api/sentry-reactor.api | 2 +- .../SentryReactorThreadLocalAccessor.java | 2 - .../io/sentry/reactor/SentryReactorUtils.java | 6 +- .../io.micrometer.context.ThreadLocalAccessor | 0 .../sentry/reactor/SentryReactorUtilsTest.kt | 121 +++++++++--------- .../api/sentry-spring-jakarta.api | 4 + .../jakarta/webflux/reactor/ReactorUtils.java | 9 ++ 7 files changed, 76 insertions(+), 68 deletions(-) rename {sentry-spring-jakarta => sentry-reactor}/src/main/resources/META-INF/services/io.micrometer.context.ThreadLocalAccessor (100%) create mode 100644 sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/reactor/ReactorUtils.java diff --git a/sentry-reactor/api/sentry-reactor.api b/sentry-reactor/api/sentry-reactor.api index a756cf7eafc..bb38ca1df4d 100644 --- a/sentry-reactor/api/sentry-reactor.api +++ b/sentry-reactor/api/sentry-reactor.api @@ -14,7 +14,7 @@ public final class io/sentry/reactor/SentryReactorThreadLocalAccessor : io/micro public synthetic fun setValue (Ljava/lang/Object;)V } -public final class io/sentry/reactor/SentryReactorUtils { +public class io/sentry/reactor/SentryReactorUtils { public fun ()V public static fun withSentry (Lreactor/core/publisher/Flux;)Lreactor/core/publisher/Flux; public static fun withSentry (Lreactor/core/publisher/Mono;)Lreactor/core/publisher/Mono; diff --git a/sentry-reactor/src/main/java/io/sentry/reactor/SentryReactorThreadLocalAccessor.java b/sentry-reactor/src/main/java/io/sentry/reactor/SentryReactorThreadLocalAccessor.java index 4da9889d5f2..7ef4bb9bd1e 100644 --- a/sentry-reactor/src/main/java/io/sentry/reactor/SentryReactorThreadLocalAccessor.java +++ b/sentry-reactor/src/main/java/io/sentry/reactor/SentryReactorThreadLocalAccessor.java @@ -4,9 +4,7 @@ import io.sentry.IScopes; import io.sentry.NoOpScopes; import io.sentry.Sentry; -import org.jetbrains.annotations.ApiStatus; -@ApiStatus.Experimental public final class SentryReactorThreadLocalAccessor implements ThreadLocalAccessor { public static final String KEY = "sentry-scopes"; diff --git a/sentry-reactor/src/main/java/io/sentry/reactor/SentryReactorUtils.java b/sentry-reactor/src/main/java/io/sentry/reactor/SentryReactorUtils.java index 2e6972b8df6..16fed92bd01 100644 --- a/sentry-reactor/src/main/java/io/sentry/reactor/SentryReactorUtils.java +++ b/sentry-reactor/src/main/java/io/sentry/reactor/SentryReactorUtils.java @@ -1,15 +1,15 @@ package io.sentry.reactor; +import com.jakewharton.nopen.annotation.Open; import io.sentry.IScopes; import io.sentry.Sentry; -import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.util.context.Context; -@ApiStatus.Experimental -public final class SentryReactorUtils { +@Open +public class SentryReactorUtils { /** * Writes the current Sentry {@link IScopes} to the {@link Context} and uses {@link diff --git a/sentry-spring-jakarta/src/main/resources/META-INF/services/io.micrometer.context.ThreadLocalAccessor b/sentry-reactor/src/main/resources/META-INF/services/io.micrometer.context.ThreadLocalAccessor similarity index 100% rename from sentry-spring-jakarta/src/main/resources/META-INF/services/io.micrometer.context.ThreadLocalAccessor rename to sentry-reactor/src/main/resources/META-INF/services/io.micrometer.context.ThreadLocalAccessor diff --git a/sentry-reactor/src/test/kotlin/io/sentry/reactor/SentryReactorUtilsTest.kt b/sentry-reactor/src/test/kotlin/io/sentry/reactor/SentryReactorUtilsTest.kt index 2a1370bb2b9..e2edfa9d53f 100644 --- a/sentry-reactor/src/test/kotlin/io/sentry/reactor/SentryReactorUtilsTest.kt +++ b/sentry-reactor/src/test/kotlin/io/sentry/reactor/SentryReactorUtilsTest.kt @@ -1,10 +1,12 @@ package io.sentry.reactor -import io.micrometer.context.ContextRegistry import io.sentry.IScopes import io.sentry.NoOpScopes import io.sentry.Sentry +import org.mockito.kotlin.any import org.mockito.kotlin.mock +import org.mockito.kotlin.verify +import org.mockito.kotlin.whenever import reactor.core.publisher.Flux import reactor.core.publisher.Hooks import reactor.core.publisher.Mono @@ -14,61 +16,56 @@ import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotSame -// import org.mockito.kotlin.any -// import org.mockito.kotlin.verify -// import org.mockito.kotlin.whenever -// import kotlin.test.assertSame +import kotlin.test.assertSame class SentryReactorUtilsTest { @BeforeTest fun setup() { - println("setUp") - ContextRegistry.getInstance().registerThreadLocalAccessor(SentryReactorThreadLocalAccessor()) Hooks.enableAutomaticContextPropagation() + Sentry.init("https://key@sentry.io/proj") } @AfterTest fun teardown() { - println("teardown") Sentry.setCurrentScopes(NoOpScopes.getInstance()) } - // @Test - // fun `propagates scopes inside mono`() { - // val scopesToUse = mock() - // var scopesInside: IScopes? = null - // val mono = SentryReactorUtils.withSentryScopes( - // Mono.just("hello") - // .publishOn(Schedulers.boundedElastic()) - // .map { it -> - // scopesInside = Sentry.getCurrentScopes() - // it - // }, - // scopesToUse - // ) - - // assertEquals("hello", mono.block()) - // assertSame(scopesToUse, scopesInside) - // } - - // @Test - // fun `propagates scopes inside flux`() { - // val scopesToUse = mock() - // var scopesInside: IScopes? = null - // val flux = SentryReactorUtils.withSentryScopes( - // Flux.just("hello") - // .publishOn(Schedulers.boundedElastic()) - // .map { it -> - // scopesInside = Sentry.getCurrentScopes() - // it - // }, - // scopesToUse - // ) - - // assertEquals("hello", flux.blockFirst()) - // assertSame(scopesToUse, scopesInside) - // } + @Test + fun `propagates scopes inside mono`() { + val scopesToUse = mock() + var scopesInside: IScopes? = null + val mono = SentryReactorUtils.withSentryScopes( + Mono.just("hello") + .publishOn(Schedulers.boundedElastic()) + .map { it -> + scopesInside = Sentry.getCurrentScopes() + it + }, + scopesToUse + ) + + assertEquals("hello", mono.block()) + assertSame(scopesToUse, scopesInside) + } + + @Test + fun `propagates scopes inside flux`() { + val scopesToUse = mock() + var scopesInside: IScopes? = null + val flux = SentryReactorUtils.withSentryScopes( + Flux.just("hello") + .publishOn(Schedulers.boundedElastic()) + .map { it -> + scopesInside = Sentry.getCurrentScopes() + it + }, + scopesToUse + ) + + assertEquals("hello", flux.blockFirst()) + assertSame(scopesToUse, scopesInside) + } @Test fun `without reactive utils scopes is not propagated to mono`() { @@ -100,23 +97,23 @@ class SentryReactorUtilsTest { assertNotSame(scopesToUse, scopesInside) } - // @Test - // fun `clones scopes for mono`() { - // val mockScopes = mock() - // whenever(mockScopes.forkedCurrentScope(any())).thenReturn(mock()) - // Sentry.setCurrentScopes(mockScopes) - // SentryReactorUtils.withSentry(Mono.just("hello")).block() - - // verify(mockScopes).forkedCurrentScope(any()) - // } - - // @Test - // fun `clones scopes for flux`() { - // val mockScopes = mock() - // whenever(mockScopes.forkedCurrentScope(any())).thenReturn(mock()) - // Sentry.setCurrentScopes(mockScopes) - // SentryReactorUtils.withSentry(Flux.just("hello")).blockFirst() - - // verify(mockScopes).forkedCurrentScope(any()) - // } + @Test + fun `clones scopes for mono`() { + val mockScopes = mock() + whenever(mockScopes.forkedCurrentScope(any())).thenReturn(mock()) + Sentry.setCurrentScopes(mockScopes) + SentryReactorUtils.withSentry(Mono.just("hello")).block() + + verify(mockScopes).forkedCurrentScope(any()) + } + + @Test + fun `clones scopes for flux`() { + val mockScopes = mock() + whenever(mockScopes.forkedCurrentScope(any())).thenReturn(mock()) + Sentry.setCurrentScopes(mockScopes) + SentryReactorUtils.withSentry(Flux.just("hello")).blockFirst() + + verify(mockScopes).forkedCurrentScope(any()) + } } diff --git a/sentry-spring-jakarta/api/sentry-spring-jakarta.api b/sentry-spring-jakarta/api/sentry-spring-jakarta.api index b7d24d1c3bd..cb27fb7f824 100644 --- a/sentry-spring-jakarta/api/sentry-spring-jakarta.api +++ b/sentry-spring-jakarta/api/sentry-spring-jakarta.api @@ -334,3 +334,7 @@ public final class io/sentry/spring/jakarta/webflux/SentryWebFilterWithThreadLoc public fun filter (Lorg/springframework/web/server/ServerWebExchange;Lorg/springframework/web/server/WebFilterChain;)Lreactor/core/publisher/Mono; } +public final class io/sentry/spring/jakarta/webflux/reactor/ReactorUtils : io/sentry/reactor/SentryReactorUtils { + public fun ()V +} + diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/reactor/ReactorUtils.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/reactor/ReactorUtils.java new file mode 100644 index 00000000000..b3249570369 --- /dev/null +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/reactor/ReactorUtils.java @@ -0,0 +1,9 @@ +package io.sentry.spring.jakarta.webflux.reactor; + +import io.sentry.reactor.SentryReactorUtils; + +/** + * @deprecated Please use {@link SentryReactorUtils} directly. + */ +@Deprecated +public final class ReactorUtils extends SentryReactorUtils {}