|
| 1 | +--- |
| 2 | +title: Reactor Integration |
| 3 | +description: "Learn how to capture errors, breadcrumbs and traces for your reactive applications." |
| 4 | +--- |
| 5 | + |
| 6 | +Sentry's [Reactor](https://projectreactor.io/) integration provides a set of utilities to use Sentry with Reactor. |
| 7 | + |
| 8 | +Using the utilities ensures that your errors and traces are always enriched with the correct context and breadcrumbs. |
| 9 | +Due to how Reactor works under the hood, if you don't use this integration, you might run into issues such as unrelated breadcrumbs appearing in the events that are sent to Sentry within the context of Reactive Streams. |
| 10 | + |
| 11 | +If you're using Spring Boot WebFlux, you just need to install our [Spring Boot SDK](/platforms/java/guides/spring-boot/) and the Reactor integration will be automatically configured and enabled under the hood. |
| 12 | + |
| 13 | +Otherwise, read on to learn how to install and use the Sentry Reactor integration. |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +To install use: |
| 18 | + |
| 19 | +```groovy {tabTitle:Gradle} |
| 20 | +implementation 'io.sentry:sentry-reactor:{{@inject packages.version('sentry.java.reactor', '8.3.0') }}' |
| 21 | +``` |
| 22 | + |
| 23 | +```xml {tabTitle:Maven} |
| 24 | +<dependency> |
| 25 | + <groupId>io.sentry</groupId> |
| 26 | + <artifactId>sentry-reactor</artifactId> |
| 27 | + <version>{{@inject packages.version('sentry.java.reactor', '8.3.0') }}</version> |
| 28 | +</dependency> |
| 29 | +``` |
| 30 | + |
| 31 | +```scala {tabTitle: SBT} |
| 32 | +libraryDependencies += "io.sentry" % "sentry-reactor" % "{{@inject packages.version('sentry.java.reactor', '8.3.0') }}" |
| 33 | +``` |
| 34 | + |
| 35 | +For other dependency managers, check out the [central Maven repository](https://search.maven.org/artifact/io.sentry/sentry-reactor). |
| 36 | + |
| 37 | +Make sure your dependencies include `io.micrometer:context-propagation` version `1.0.2` or later, and `io.projectreactor:reactor-core` version `3.5.3` or later, as those are required for the integration to work. |
| 38 | + |
| 39 | +## Set Up |
| 40 | + |
| 41 | +Enable automatic context propagation: |
| 42 | +```java |
| 43 | +import reactor.core.publisher.Hooks; |
| 44 | +// ... |
| 45 | +Hooks.enableAutomaticContextPropagation(); |
| 46 | +``` |
| 47 | + |
| 48 | +```kotlin |
| 49 | +import reactor.core.publisher.Hooks |
| 50 | +// ... |
| 51 | +Hooks.enableAutomaticContextPropagation() |
| 52 | +``` |
| 53 | + |
| 54 | +## Usage |
| 55 | + |
| 56 | +Wrap your `Mono` and `Flux` objects using the `SentryReactorUtils.withSentry` function to enable correct capturing of errors, breadcrumbs and traces in the context of your Reactive Streams. |
| 57 | +This will execute your publisher in the context of *forked current scopes*, which should be the default for most use cases. |
| 58 | + |
| 59 | +For example: |
| 60 | +```java |
| 61 | +import reactor.core.publisher.Mono; |
| 62 | +import io.sentry.Sentry; |
| 63 | +import io.sentry.ISpan; |
| 64 | +import io.sentry.ITransaction; |
| 65 | +import io.sentry.TransactionOptions; |
| 66 | + |
| 67 | +TransactionOptions txOptions = new TransactionOptions(); |
| 68 | +txOptions.setBindToScope(true); |
| 69 | +ITransaction tx = Sentry.startTransaction("Transaction", "op", txOptions); |
| 70 | +ISpan child = tx.startChild("Outside Mono", "op") |
| 71 | +Sentry.captureMessage("Message outside Mono") |
| 72 | +child.finish() |
| 73 | +String result = SentryReactorUtils.withSentry( |
| 74 | + Mono.just("hello") |
| 75 | + .map({ (it) -> |
| 76 | + ISpan span = Sentry.getCurrentScopes().transaction.startChild("Inside Mono", "map"); |
| 77 | + Sentry.captureMessage("Message inside Mono"); |
| 78 | + span.finish(); |
| 79 | + return it; |
| 80 | + }) |
| 81 | +).block(); |
| 82 | +System.out.println(result); |
| 83 | +tx.finish(); |
| 84 | +``` |
| 85 | + |
| 86 | +```kotlin |
| 87 | +import reactor.core.publisher.Mono |
| 88 | +import io.sentry.Sentry |
| 89 | +import io.sentry.TransactionOptions |
| 90 | + |
| 91 | +val tx = Sentry.startTransaction("Transaction Mono", "op", TransactionOptions().also { it.isBindToScope = true }) |
| 92 | +val child = tx.startChild("Outside Mono", "op") |
| 93 | +Sentry.captureMessage("Message outside Mono") |
| 94 | +child.finish() |
| 95 | +val result = SentryReactorUtils.withSentry( |
| 96 | + Mono.just("hello") |
| 97 | + .map { it -> |
| 98 | + val span = Sentry.getCurrentScopes().transaction?.startChild("Inside Mono", "map") |
| 99 | + Sentry.captureMessage("Message inside Mono") |
| 100 | + span?.finish() |
| 101 | + it |
| 102 | + } |
| 103 | +).block() |
| 104 | +println(result) |
| 105 | +tx.finish() |
| 106 | +``` |
| 107 | + |
| 108 | +For more complex use cases, you can also use `SentryReactorUtils.withSentryForkedRoots` to fork the root scopes or `SentryReactorUtils.withSentryScopes` to wrap the operation in arbitrary scopes. |
| 109 | + |
| 110 | +For more information on scopes and scope forking, please consult our [scopes documentation](https://docs.sentry.io/platforms/java/enriching-events/scopes). |
| 111 | + |
| 112 | +You can also consult our GitHub repository for practical examples on how to use our Reactor integration with Spring WebFlux with [Spring Boot 2](https://github.com/getsentry/sentry-java/tree/main/sentry-samples/sentry-samples-spring-boot-webflux) and [Spring Boot 3](https://github.com/getsentry/sentry-java/tree/main/sentry-samples/sentry-samples-spring-boot-webflux-jakarta). |
0 commit comments