Skip to content

Commit

Permalink
Replace ThreadLocal in Sentry with HubStorage and a wrapped ThreadLoc…
Browse files Browse the repository at this point in the history
…al Hub; also fix Webflux integration and add transaction support for webflux
  • Loading branch information
adinauer committed Aug 26, 2022
1 parent d425298 commit 8ad9b37
Show file tree
Hide file tree
Showing 14 changed files with 654 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ dependencies {
implementation(kotlin(Config.kotlinStdLib, KotlinCompilerVersion.VERSION))
implementation(projects.sentrySpringBootStarter)
implementation(projects.sentryLogback)
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("io.micrometer:micrometer-registry-prometheus")
testImplementation(Config.Libs.springBootStarterTest) {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;

import java.util.UUID;

import io.sentry.spring.webflux.SentryWebfluxHubHolder;
import static io.sentry.spring.webflux.SentryWebfluxHubHolder.withSentryOnComplete;
import static io.sentry.spring.webflux.SentryWebfluxHubHolder.withSentryOnFirst;
import static io.sentry.spring.webflux.SentryWebfluxHubHolder.withSentryFinally;
import static io.sentry.spring.webflux.SentryWebfluxHubHolder.withSentryOnError;
import static io.sentry.spring.webflux.SentryWebfluxHubHolder.withSentryOnNext;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
Expand All @@ -20,12 +31,91 @@ public PersonController(PersonService personService) {
this.personService = personService;
}

@GetMapping("{id}")
@GetMapping("p/{id}")
Person person(@PathVariable Long id) {
LOGGER.info("Loading person with id={}", id);
throw new IllegalArgumentException("Something went wrong [id=" + id + "]");
}

@GetMapping("p4/{id}")
Person person4(@PathVariable Long id) {
return new Person("first", "last");
}

@GetMapping("p2/{id}")
Mono<Person> person2(@PathVariable Long id) {
return Mono.error(new IllegalArgumentException("Something went wrong2 [id=" + id + "]"));
// LOGGER.info("Loading person2 with id={}", id);
}

@GetMapping("p3/{id}")
Mono<Person> person3(@PathVariable Long id, ServerWebExchange serverWebExchange) {
String uniq = UUID.randomUUID().toString();
return personService.find(id)
.doFirst(withSentryOnFirst(serverWebExchange, () -> LOGGER.info("Finding person with id " + uniq)))
.doOnError(withSentryOnError(serverWebExchange, e -> LOGGER.error("Hello from error " + uniq, e)))
.doFinally(withSentryFinally(serverWebExchange, s -> LOGGER.warn("Finally for person with id " + uniq + ":" + s)))
.doOnEach(withSentryOnComplete(x -> LOGGER.info("oncomplete " + uniq)))
.doOnEach(withSentryOnNext(p -> LOGGER.info("Found " + uniq)))
.doOnEach(withSentryOnError(e -> LOGGER.error("oneach error " + uniq, e)));
}

@GetMapping("all1")
Flux<Person> all(ServerWebExchange serverWebExchange) {
String uniq = UUID.randomUUID().toString();
return personService.findAll()
.doFirst(withSentryOnFirst(serverWebExchange, () -> LOGGER.info("Finding all people " + uniq)))
.doOnError(withSentryOnError(serverWebExchange, e -> LOGGER.error("Hello from error " + uniq, e)))
.doFinally(withSentryFinally(serverWebExchange, __ -> LOGGER.warn("Finally for all people " + uniq)))
.doOnEach(withSentryOnComplete(__ -> LOGGER.info("oncomplete " + uniq)))
.doOnEach(withSentryOnNext(p -> LOGGER.info("Found " + uniq + " " + p.toString())))
.doOnComplete(withSentryOnComplete(serverWebExchange, () -> LOGGER.info("on complete " + uniq)))
.doOnEach(withSentryOnError(e -> LOGGER.error("oneach error " + uniq, e)));
}

@GetMapping("allerror")
Flux<Person> allError(ServerWebExchange serverWebExchange) {
String uniq = UUID.randomUUID().toString();
return personService.findAllException()
.doFirst(withSentryOnFirst(serverWebExchange, () -> LOGGER.info("Finding all people with error " + uniq)))
.doOnError(withSentryOnError(serverWebExchange, e -> LOGGER.error("Hello from error " + uniq, e)))
.doFinally(withSentryFinally(serverWebExchange, s -> LOGGER.warn("Finally for all people with error " + uniq)))
.doOnEach(withSentryOnComplete(x -> LOGGER.info("oncomplete " + uniq)))
.doOnEach(withSentryOnNext(p -> LOGGER.info("Found " + uniq + " " + p.toString())))
.doOnComplete(withSentryOnComplete(serverWebExchange, () -> LOGGER.info("on complete " + uniq)))
.doOnEach(withSentryOnError(e -> LOGGER.error("oneach error " + uniq, e)));
}

@GetMapping("allerror3")
Flux<Person> allError3(ServerWebExchange serverWebExchange) {
String uniq = UUID.randomUUID().toString();
return personService.findAll()
.doFirst(withSentryOnFirst(serverWebExchange, () -> LOGGER.info("Finding all people with error 3 " + uniq)))
.doOnError(withSentryOnError(serverWebExchange, e -> LOGGER.error("Hello from error 3 " + uniq, e)))
.doFinally(withSentryFinally(serverWebExchange, s -> LOGGER.error("Finally for all people with error 3 " + uniq)))
.doOnEach(withSentryOnComplete(x -> LOGGER.info("oncomplete 3 " + uniq)))
.doOnEach(withSentryOnNext(p -> LOGGER.info("Found 3 " + uniq + " " + p.toString())))
.doOnComplete(withSentryOnComplete(serverWebExchange, () -> LOGGER.info("on complete 3 " + uniq)))
.doOnEach(withSentryOnError(e -> LOGGER.error("oneach error 3 " + uniq, e)));
}

@GetMapping("all")
Flux<Person> findAll(ServerWebExchange serverWebExchange) {
return personService.findAll()
.doFirst(withSentryOnFirst(serverWebExchange, () -> LOGGER.info("doFirst")))
.doOnNext(withSentryOnNext(serverWebExchange, p -> LOGGER.info("doOnNext " + p.toString())))
.doOnComplete(withSentryOnComplete(serverWebExchange, () -> LOGGER.info("doOnComplete")))
.doOnError(withSentryOnError(serverWebExchange, e -> LOGGER.error("doOnError", e)))
.doFinally(withSentryFinally(serverWebExchange, __ -> LOGGER.info("doFinally")))
.doOnEach(withSentryOnComplete(__ -> LOGGER.info("onEachComplete")))
.doOnEach(withSentryOnNext(p -> LOGGER.info("onEachNext " + p.toString())))
.doOnEach(withSentryOnError(e -> LOGGER.error("onEachError", e)))
.flatMap(p -> SentryWebfluxHubHolder.getHubFlux()
.map(hub -> hub.captureMessage("Hello message"))
.flatMap(__ -> Flux.just(p)))
;
}

@PostMapping
Mono<Person> create(@RequestBody Person person) {
return personService.create(person);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import io.sentry.Sentry;
import java.time.Duration;
import org.springframework.stereotype.Service;

import static io.sentry.spring.webflux.SentryWebfluxHubHolder.withSentryOnNext;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

Expand All @@ -12,7 +15,35 @@ public class PersonService {
Mono<Person> create(Person person) {
return Mono.delay(Duration.ofMillis(100))
.publishOn(Schedulers.boundedElastic())
.doOnNext(__ -> Sentry.captureMessage("Creating person"))
.doOnEach(withSentryOnNext(__ -> Sentry.captureMessage("Creating person")))
// .doOnNext(__ -> Sentry.captureMessage("Creating person"))
.map(__ -> person);
}

Mono<Person> find(Long id) {
return Mono.delay(Duration.ofMillis(100))
.publishOn(Schedulers.boundedElastic())
.doOnEach(withSentryOnNext(__ -> Sentry.captureMessage("Finding person")))
.flatMap(p -> {
if (id > 10) {
return Mono.<Person>error(new RuntimeException("Caused on purpose for webflux " + Thread.currentThread().getName()));
} else {
return Mono.<Person>just(new Person("first", "last"));
}
});
}

Flux<Person> findAll() {
return Mono.delay(Duration.ofMillis(100)).flux()
.publishOn(Schedulers.boundedElastic())
.doOnEach(withSentryOnNext(__ -> Sentry.captureMessage("Finding all people")))
.flatMap(__ -> Flux.<Person>just(new Person("first1", "last1"), new Person("first2", "last2")));
}

Flux<Person> findAllException() {
return Mono.delay(Duration.ofMillis(100)).flux()
.publishOn(Schedulers.boundedElastic())
.doOnEach(withSentryOnNext(__ -> Sentry.captureMessage("Found another person")))
.flatMap(__ -> Flux.<Person>error(new RuntimeException("Caused on purpose for webflux findAll")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ sentry.debug=true
# Sentry Spring Boot integration allows more fine-grained SentryOptions configuration
sentry.max-breadcrumbs=150
# Logback integration configuration options
sentry.logging.minimum-event-level=info
sentry.logging.minimum-event-level=error
sentry.logging.minimum-breadcrumb-level=debug
#sentry.traces-sample-rate=1.0
management.endpoints.web.exposure.include=health,info,prometheus
logging.level.root=INFO
logging.level.io.sentry=DEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.jakewharton.nopen.annotation.Open;
import io.sentry.IHub;
import io.sentry.spring.tracing.SentryTracingFilter;
import io.sentry.spring.tracing.TransactionNameProvider;
import io.sentry.spring.webflux.SentryScheduleHook;
import io.sentry.spring.webflux.SentryWebExceptionHandler;
import io.sentry.spring.webflux.SentryWebFilter;
Expand All @@ -10,9 +12,16 @@
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import io.sentry.spring.webflux.SentryWebTracingFilter;
import reactor.core.scheduler.Schedulers;

/** Configures Sentry integration for Spring Webflux and Project Reactor. */
Expand All @@ -24,20 +33,31 @@
@ApiStatus.Experimental
public class SentryWebfluxAutoConfiguration {

private static final int SENTRY_SPRING_FILTER_PRECEDENCE = Ordered.HIGHEST_PRECEDENCE;

/** Configures hook that sets correct hub on the executing thread. */
@Bean
public @NotNull ApplicationRunner sentryScheduleHookApplicationRunner() {
return args -> {
Schedulers.onScheduleHook("sentry", new SentryScheduleHook());
};
}
// @Bean
// public @NotNull ApplicationRunner sentryScheduleHookApplicationRunner() {
// return args -> {
// Schedulers.onScheduleHook("sentry", new SentryScheduleHook());
// };
// }

/** Configures a filter that sets up Sentry {@link io.sentry.Scope} for each request. */
@Bean
@Order(SENTRY_SPRING_FILTER_PRECEDENCE)
public @NotNull SentryWebFilter sentryWebFilter(final @NotNull IHub hub) {
return new SentryWebFilter(hub);
}

@Bean
@Order(SENTRY_SPRING_FILTER_PRECEDENCE + 1)
@Conditional(SentryAutoConfiguration.SentryTracingCondition.class)
@ConditionalOnMissingBean(name = "sentryWebTracingFilter")
public @NotNull SentryWebTracingFilter sentryWebTracingFilter() {
return new SentryWebTracingFilter();
}

/** Configures exception handler that handles unhandled exceptions and sends them to Sentry. */
@Bean
public @NotNull SentryWebExceptionHandler sentryWebExceptionHandler(final @NotNull IHub hub) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
public class SentryRequestResolver {
private static final List<String> SENSITIVE_HEADERS =
Arrays.asList("X-FORWARDED-FOR", "AUTHORIZATION", "COOKIE");
private final boolean isSendDefaultPii;

private final @NotNull IHub hub;
// private final @NotNull IHub hub;

public SentryRequestResolver(final @NotNull IHub hub) {
this.hub = Objects.requireNonNull(hub, "options is required");
public SentryRequestResolver(final boolean isSendDefaultPii) {
// this.hub = Objects.requireNonNull(hub, "options is required");
this.isSendDefaultPii = isSendDefaultPii;
}

public @NotNull Request resolveSentryRequest(final @NotNull ServerHttpRequest httpRequest) {
Expand All @@ -34,7 +36,7 @@ public SentryRequestResolver(final @NotNull IHub hub) {
sentryRequest.setUrl(httpRequest.getURI().toString());
sentryRequest.setHeaders(resolveHeadersMap(httpRequest.getHeaders()));

if (hub.getOptions().isSendDefaultPii()) {
if (isSendDefaultPii) {
sentryRequest.setCookies(toString(httpRequest.getHeaders().get("Cookies")));
}
return sentryRequest;
Expand All @@ -45,7 +47,7 @@ Map<String, String> resolveHeadersMap(final HttpHeaders request) {
final Map<String, String> headersMap = new HashMap<>();
for (Map.Entry<String, List<String>> entry : request.entrySet()) {
// do not copy personal information identifiable headers
if (hub.getOptions().isSendDefaultPii()
if (isSendDefaultPii
|| !SENSITIVE_HEADERS.contains(entry.getKey().toUpperCase(Locale.ROOT))) {
headersMap.put(entry.getKey(), toString(entry.getValue()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
import java.util.function.Function;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Hook meant to used with {@link reactor.core.scheduler.Schedulers#onScheduleHook(String,
* Function)} to configure Reactor to copy correct hub into the operating thread.
*/
@ApiStatus.Experimental
public final class SentryScheduleHook implements Function<Runnable, Runnable> {

private static final Logger log = LoggerFactory.getLogger("hooklogger");

@Override
public Runnable apply(final @NotNull Runnable runnable) {
String threadName = Thread.currentThread().getName();
log.debug("Starting hook on " + threadName);
final IHub oldState = Sentry.getCurrentHub();
final IHub newHub = Sentry.getCurrentHub().clone();
return () -> {
Expand All @@ -22,6 +29,7 @@ public Runnable apply(final @NotNull Runnable runnable) {
runnable.run();
} finally {
Sentry.setCurrentHub(oldState);
log.debug("Ended hook on " + threadName);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import io.sentry.util.Objects;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
Expand All @@ -24,15 +26,17 @@
// at -1
@ApiStatus.Experimental
public final class SentryWebExceptionHandler implements WebExceptionHandler {
private final @NotNull IHub hub;
// private final @NotNull IHub hub;
// private static final Logger log = LoggerFactory.getLogger("exceptionlogger");

public SentryWebExceptionHandler(final @NotNull IHub hub) {
this.hub = Objects.requireNonNull(hub, "hub is required");
// this.hub = Objects.requireNonNull(hub, "hub is required");
}

@Override
public @NotNull Mono<Void> handle(
final @NotNull ServerWebExchange serverWebExchange, final @NotNull Throwable ex) {
// log.debug("Exception handled on thread " + Thread.currentThread().getName());
if (!(ex instanceof ResponseStatusException)) {
final Mechanism mechanism = new Mechanism();
mechanism.setType("SentryWebExceptionHandler");
Expand All @@ -47,7 +51,16 @@ public SentryWebExceptionHandler(final @NotNull IHub hub) {
hint.set(WEBFLUX_EXCEPTION_HANDLER_REQUEST, serverWebExchange.getRequest());
hint.set(WEBFLUX_EXCEPTION_HANDLER_RESPONSE, serverWebExchange.getResponse());

hub.captureEvent(event, hint);
return SentryWebfluxHubHolder.getHub(serverWebExchange).flatMap(hub -> {
// log.warn("hub " + hub);
hub.captureEvent(event, hint);
return Mono.error(ex);
});
// return SentryWebfluxHubHolder.getHub(serverWebExchange).map(hub -> {
// log.warn("hub " + hub);
// hub.captureEvent(event, hint);
// return null;
// });
}
return Mono.error(ex);
}
Expand Down

0 comments on commit 8ad9b37

Please sign in to comment.