-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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). |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.