|
| 1 | +# sentry-reactor |
| 2 | + |
| 3 | +This module provides a set of utilities to use Sentry with [Reactor](https://projectreactor.io/). |
| 4 | + |
| 5 | +## Setup |
| 6 | + |
| 7 | +Please refer to the documentation on how to set up our [Java SDK](https://docs.sentry.io/platforms/java/), |
| 8 | +or our [Spring](https://docs.sentry.io/platforms/java/guides/spring/) |
| 9 | +or [Spring Boot](https://docs.sentry.io/platforms/java/guides/spring-boot/) integrations if you're using Spring WebFlux. |
| 10 | + |
| 11 | +If you're using our Spring Boot SDK with Spring Boot (`sentry-spring-boot` or `sentry-spring-boot-jakarta`), this module will be available and used under the hood to automatically instrument WebFlux. |
| 12 | +If you're using our Spring SDK (`sentry-spring` or `sentry-spring-jakarta`), you need to configure WebFlux as we do in [SentryWebFluxAutoConfiguration](https://github.com/getsentry/sentry-java/blob/a5098280b52aec28c71c150e286b5c937767634d/sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryWebfluxAutoConfiguration.java) for Spring Boot. |
| 13 | + |
| 14 | +Otherwise, read on to find out how to set up and use the integration. |
| 15 | + |
| 16 | +Add the latest version of `io.sentry.reactor` as a dependency. |
| 17 | +Make sure you're using `io.micrometer:context-propagation:1.0.2` or later, and `io.projectreactor:reactor-core:3.5.3` or later. |
| 18 | + |
| 19 | +Then, enable automatic context propagation: |
| 20 | +```java |
| 21 | +import reactor.core.publisher.Hooks; |
| 22 | +// ... |
| 23 | +Hooks.enableAutomaticContextPropagation(); |
| 24 | +``` |
| 25 | + |
| 26 | +Finally, enable the `SentryReactorThreadLocalAccessor`: |
| 27 | +```java |
| 28 | +import io.micrometer.context.ContextRegistry; |
| 29 | +import io.sentry.reactor.SentryReactorThreadLocalAccessor; |
| 30 | +// ... |
| 31 | +ContextRegistry.getInstance().registerThreadLocalAccessor(SentryReactorThreadLocalAccessor()); |
| 32 | +``` |
| 33 | + |
| 34 | +You can also use SPI to enable it by creating a file in `resources/META-INF.services/io.micrometer.context.ThreadLocalAccessor` with the content: |
| 35 | +``` |
| 36 | +io.sentry.reactor.SentryReactorThreadLocalAccessor |
| 37 | +``` |
| 38 | +and then calling |
| 39 | +```java |
| 40 | +import io.micrometer.context.ContextRegistry; |
| 41 | +// ... |
| 42 | +ContextRegistry.getInstance().loadThreadLocalAccessors(); |
| 43 | +``` |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +You can use the utilities provided by this module to wrap `Mono` and `Flux` objects to enable correct errors, breadcrumbs and tracing in your application. |
| 48 | + |
| 49 | +For normal use cases, you should wrap your operations on `Mono` or `Flux` objects using the `withSentry` function. |
| 50 | +This will fork the *current scopes* and use them throughout the stream's execution context. |
| 51 | + |
| 52 | +For example: |
| 53 | +```java |
| 54 | +import reactor.core.publisher.Mono; |
| 55 | +import io.sentry.Sentry; |
| 56 | +import io.sentry.ISpan; |
| 57 | +import io.sentry.ITransaction; |
| 58 | +import io.sentry.TransactionOptions; |
| 59 | + |
| 60 | +TransactionOptions txOptions = new TransactionOptions(); |
| 61 | +txOptions.setBindToScope(true); |
| 62 | +ITransaction tx = Sentry.startTransaction("Transaction", "op", txOptions); |
| 63 | +ISpan child = tx.startChild("Outside Mono", "op") |
| 64 | +Sentry.captureMessage("Message outside Mono") |
| 65 | +child.finish() |
| 66 | +String result = SentryReactorUtils.withSentry( |
| 67 | + Mono.just("hello") |
| 68 | + .map({ (it) -> |
| 69 | + ISpan span = Sentry.getCurrentScopes().transaction.startChild("Inside Mono", "map"); |
| 70 | + Sentry.captureMessage("Message inside Mono"); |
| 71 | + span.finish(); |
| 72 | + return it; |
| 73 | + }) |
| 74 | +).block(); |
| 75 | +System.out.println(result); |
| 76 | +tx.finish(); |
| 77 | +``` |
| 78 | + |
| 79 | +For more complex use cases, you can also use `withSentryForkedRoots` to fork the root scopes or `withSentryScopes` to wrap the operation in arbitrary scopes. |
| 80 | + |
| 81 | +For more information on scopes and scope forking, please consult our [scopes documentation](https://docs.sentry.io/platforms/java/enriching-events/scopes). |
| 82 | + |
| 83 | +Examples of usage of this module (with Spring WebFlux) are provided in |
| 84 | +[sentry-samples-spring-boot-webflux](https://github.com/getsentry/sentry-java/tree/main/sentry-samples/sentry-samples-spring-boot-webflux) |
| 85 | +and |
| 86 | +[sentry-samples-spring-boot-webflux-jakarta](https://github.com/getsentry/sentry-java/tree/main/sentry-samples/sentry-samples-spring-boot-webflux-jakarta) |
| 87 | +. |
0 commit comments