Skip to content
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

Add a util function to ReactorUtils that makes it easier to create a span for Reactor / WebFlux #4043

Open
adinauer opened this issue Jan 13, 2025 · 0 comments

Comments

@adinauer
Copy link
Member

Description

When using Reactor (e.g. when using Spring Boot WebFlux), the way to start a Sentry span that is also set as the current span is as follows:

NOTE: This code lacks error handling!

@RestController
public class TodoController {
  private final WebClient webClient;

  public TodoController(WebClient webClient) {
    this.webClient = webClient;
  }

  @GetMapping("/todo-webclient/{id}")
  Mono<Todo> todoWebClient(@PathVariable Long id) {
    return Mono.deferContextual(ctx -> getTodoMono(id).doFinally((signalType) -> {
        ISpan span = ctx.get("sentry-span");
        span.finish();
      }))
      .contextWrite(context -> {
        ISpan span = Sentry.getSpan().startChild("intermediate-span");
        span.makeCurrent();
        return context.put("sentry-span", span);
      });
  }

  private Mono<Todo> getTodoMono(Long id) {
    return webClient
      .get()
      .uri("https://jsonplaceholder.typicode.com/todos/{id}", id)
      .retrieve()
      .bodyToMono(Todo.class)
      .map(response -> response);
  }
}

We could make it easier by adding something like ReactorUtils.withSentrySpan that takes care of the Context manipulation and reading.

This issue shall serve to gauge interest in such a util function. Please 👍 if you would like to see this implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Needs Discussion
Development

No branches or pull requests

1 participant