-
-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
243 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/webflux/ReactorUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.sentry.spring.jakarta.webflux; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import io.sentry.IHub; | ||
import io.sentry.Sentry; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import reactor.util.context.Context; | ||
|
||
@ApiStatus.Experimental | ||
public final class ReactorUtils { | ||
|
||
/** | ||
* Writes the Sentry {@link IHub} to the {@link Context} and uses {@link io.micrometer.context.ThreadLocalAccessor} to propagate it. | ||
* | ||
* This requires | ||
* - reactor.core.publisher.Hooks#enableAutomaticContextPropagation() to be enabled | ||
* - having `io.micrometer:context-propagation:1.0.2` or newer as dependency | ||
* - having `io.projectreactor:reactor-core:3.5.3` or newer as dependency | ||
*/ | ||
@ApiStatus.Experimental | ||
public static <T> Mono<T> withSentry(Mono<T> mono) { | ||
final @NotNull IHub oldHub = Sentry.getCurrentHub(); | ||
final @NotNull IHub clonedHub = oldHub.clone(); | ||
|
||
/** | ||
* WARNING: Cannot set the clonedHub as current hub. | ||
* It would be used by others to clone again causing shared hubs and scopes and thus | ||
* leading to issues like unrelated breadcrumbs showing up in events. | ||
*/ | ||
// Sentry.setCurrentHub(clonedHub); | ||
|
||
return Mono.deferContextual(ctx -> mono).contextWrite(Context.of(SentryReactorThreadLocalAccessor.KEY, clonedHub)); | ||
} | ||
|
||
/** | ||
* Writes the Sentry {@link IHub} to the {@link Context} and uses {@link io.micrometer.context.ThreadLocalAccessor} to propagate it. | ||
* | ||
* This requires | ||
* - reactor.core.publisher.Hooks#enableAutomaticContextPropagation() to be enabled | ||
* - having `io.micrometer:context-propagation:1.0.2` or newer as dependency | ||
* - having `io.projectreactor:reactor-core:3.5.3` or newer as dependency | ||
*/ | ||
@ApiStatus.Experimental | ||
public static <T> Flux<T> withSentry(Flux<T> flux) { | ||
final @NotNull IHub oldHub = Sentry.getCurrentHub(); | ||
final @NotNull IHub clonedHub = oldHub.clone(); | ||
|
||
/** | ||
* WARNING: Cannot set the clonedHub as current hub. | ||
* It would be used by others to clone again causing shared hubs and scopes and thus | ||
* leading to issues like unrelated breadcrumbs showing up in events. | ||
*/ | ||
// Sentry.setCurrentHub(clonedHub); | ||
|
||
return Flux.deferContextual(ctx -> flux).contextWrite(Context.of(SentryReactorThreadLocalAccessor.KEY, clonedHub)); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...arta/src/main/java/io/sentry/spring/jakarta/webflux/SentryReactorThreadLocalAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.sentry.spring.jakarta.webflux; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
import io.micrometer.context.ThreadLocalAccessor; | ||
import io.sentry.IHub; | ||
import io.sentry.NoOpHub; | ||
import io.sentry.Sentry; | ||
|
||
@ApiStatus.Experimental | ||
public final class SentryReactorThreadLocalAccessor implements ThreadLocalAccessor<IHub> { | ||
|
||
public static final String KEY = "sentry-hub"; | ||
|
||
@Override | ||
public Object key() { | ||
// Sentry.getCurrentHub().getOptions().getLogger().log(SentryLevel.WARNING, "get"); | ||
return KEY; | ||
} | ||
|
||
@Override | ||
public IHub getValue() { | ||
// Sentry.getCurrentHub().getOptions().getLogger().log(SentryLevel.WARNING, "get value"); | ||
return Sentry.getCurrentHub(); | ||
} | ||
|
||
@Override | ||
public void setValue(IHub value) { | ||
// Sentry.getCurrentHub().getOptions().getLogger().log(SentryLevel.WARNING, "set value " + value); | ||
Sentry.setCurrentHub(value); | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
// Sentry.getCurrentHub().getOptions().getLogger().log(SentryLevel.WARNING, "reset"); | ||
Sentry.setCurrentHub(NoOpHub.getInstance()); | ||
} | ||
|
||
// @Override | ||
// public void restore(IHub previousValue) { | ||
//// Sentry.getCurrentHub().getOptions().getLogger().log(SentryLevel.WARNING, "restore value " + previousValue); | ||
// ThreadLocalAccessor.super.restore(previousValue); | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...rc/main/java/io/sentry/spring/jakarta/webflux/SentryWebFilterWithThreadLocalAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.sentry.spring.jakarta.webflux; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import org.springframework.web.server.WebFilterChain; | ||
|
||
import io.sentry.IHub; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** Manages {@link io.sentry.Scope} in Webflux request processing. */ | ||
@ApiStatus.Experimental | ||
public final class SentryWebFilterWithThreadLocalAccessor extends SentryWebFilter { | ||
|
||
public SentryWebFilterWithThreadLocalAccessor(final @NotNull IHub hub) { | ||
super(hub); | ||
} | ||
|
||
@Override | ||
public Mono<Void> filter( | ||
final @NotNull ServerWebExchange serverWebExchange, | ||
final @NotNull WebFilterChain webFilterChain) { | ||
return ReactorUtils.withSentry(super.filter(serverWebExchange, webFilterChain)); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ng-jakarta/src/main/resources/META-INF/services/io.micrometer.context.ThreadLocalAccessor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.sentry.spring.jakarta.webflux.SentryReactorThreadLocalAccessor |