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

Replace ThreadLocal in Sentry with HubStorage and a wrapped ThreadLoc… #2224

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
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
@@ -1,5 +1,13 @@
package io.sentry.samples.spring.boot;

import static io.sentry.spring.webflux.SentryWebfluxHubHolder.withSentryFinally;
import static io.sentry.spring.webflux.SentryWebfluxHubHolder.withSentryOnComplete;
import static io.sentry.spring.webflux.SentryWebfluxHubHolder.withSentryOnError;
import static io.sentry.spring.webflux.SentryWebfluxHubHolder.withSentryOnFirst;
import static io.sentry.spring.webflux.SentryWebfluxHubHolder.withSentryOnNext;

import io.sentry.spring.webflux.SentryWebfluxHubHolder;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -8,6 +16,8 @@
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 reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
Expand All @@ -20,12 +30,122 @@ 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
@@ -1,8 +1,11 @@
package io.sentry.samples.spring.boot;

import static io.sentry.spring.webflux.SentryWebfluxHubHolder.withSentryOnNext;

import io.sentry.Sentry;
import java.time.Duration;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

Expand All @@ -12,7 +15,43 @@ 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,17 +2,20 @@

import com.jakewharton.nopen.annotation.Open;
import io.sentry.IHub;
import io.sentry.spring.webflux.SentryScheduleHook;
import io.sentry.spring.webflux.SentryWebExceptionHandler;
import io.sentry.spring.webflux.SentryWebFilter;
import io.sentry.spring.webflux.SentryWebTracingFilter;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
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.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 reactor.core.scheduler.Schedulers;

/** Configures Sentry integration for Spring Webflux and Project Reactor. */
Expand All @@ -24,20 +27,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
@@ -1,9 +1,7 @@
package io.sentry.spring.webflux;

import com.jakewharton.nopen.annotation.Open;
import io.sentry.IHub;
import io.sentry.protocol.Request;
import io.sentry.util.Objects;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand All @@ -20,11 +18,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 +34,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 +45,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
Loading