Skip to content

feat(java): add Reactor integration docs #12686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions docs/platforms/java/common/integrations/reactor.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---
title: Reactor Integration
description: "Learn how to capture errors, breadcrumbs and traces for your reactive applications."
---

Sentry's [Reactor](https://projectreactor.io/) integration provides a set of utilities to use Sentry with Reactor.

Using the utilities ensures that your errors and traces are always enriched with the correct context and breadcrumbs.
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.

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.

Otherwise, read on to learn how to install and use the Sentry Reactor integration.

## Installation

To install use:

```groovy {tabTitle:Gradle}
implementation 'io.sentry:sentry-reactor:{{@inject packages.version('sentry.java.reactor', '8.3.0') }}'
```

```xml {tabTitle:Maven}
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-reactor</artifactId>
<version>{{@inject packages.version('sentry.java.reactor', '8.3.0') }}</version>
</dependency>
```

```scala {tabTitle: SBT}
libraryDependencies += "io.sentry" % "sentry-reactor" % "{{@inject packages.version('sentry.java.reactor', '8.3.0') }}"
```

For other dependency managers, check out the [central Maven repository](https://search.maven.org/artifact/io.sentry/sentry-reactor).

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.

## Set Up

Enable automatic context propagation:
```java
import reactor.core.publisher.Hooks;
// ...
Hooks.enableAutomaticContextPropagation();
```

```kotlin
import reactor.core.publisher.Hooks
// ...
Hooks.enableAutomaticContextPropagation()
```

## Usage

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.
This will execute your publisher in the context of *forked current scopes*, which should be the default for most use cases.

For example:
```java
import reactor.core.publisher.Mono;
import io.sentry.Sentry;
import io.sentry.ISpan;
import io.sentry.ITransaction;
import io.sentry.TransactionOptions;

TransactionOptions txOptions = new TransactionOptions();
txOptions.setBindToScope(true);
ITransaction tx = Sentry.startTransaction("Transaction", "op", txOptions);
ISpan child = tx.startChild("Outside Mono", "op")
Sentry.captureMessage("Message outside Mono")
child.finish()
String result = SentryReactorUtils.withSentry(
Mono.just("hello")
.map({ (it) ->
ISpan span = Sentry.getCurrentScopes().transaction.startChild("Inside Mono", "map");
Sentry.captureMessage("Message inside Mono");
span.finish();
return it;
})
).block();
System.out.println(result);
tx.finish();
```

```kotlin
import reactor.core.publisher.Mono
import io.sentry.Sentry
import io.sentry.TransactionOptions

val tx = Sentry.startTransaction("Transaction Mono", "op", TransactionOptions().also { it.isBindToScope = true })
val child = tx.startChild("Outside Mono", "op")
Sentry.captureMessage("Message outside Mono")
child.finish()
val result = SentryReactorUtils.withSentry(
Mono.just("hello")
.map { it ->
val span = Sentry.getCurrentScopes().transaction?.startChild("Inside Mono", "map")
Sentry.captureMessage("Message inside Mono")
span?.finish()
it
}
).block()
println(result)
tx.finish()
```

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.

For more information on scopes and scope forking, please consult our [scopes documentation](https://docs.sentry.io/platforms/java/enriching-events/scopes).

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).