Async Methods
Learn about how to propagate Scope to Spring async methods.
Sentry's SDK for Java stores the scope and the context in a thread-local variable. To make sure that an async method has access to the correct Sentry context, a SentryTaskDecorator must be set on the ThreadPoolTaskExecutor.
To propagate Sentry scope to Spring MVC methods returning a Callable or a StreamingResponseBody, a ThreadPoolTaskExecutor decorated with SentryTaskDecorator must be set on the AsyncSupportConfigurer through an implementation of WebMvcConfigurer:
Copied
import org.springframework.context.annotation.Configuration; import org.springframework.core.task.AsyncTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import io.sentry.spring.SentryTaskDecorator; @Configuration class AsyncWebMvcConfiguration implements WebMvcConfigurer { @Override public void configureAsyncSupport(AsyncSupportConfigurer configurer) { configurer.setTaskExecutor(asyncExecutor()); } private AsyncTaskExecutor asyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setTaskDecorator(new SentryTaskDecorator()); executor.initialize(); return executor; } } To propagate Sentry scope to Spring @Async annotated methods, a custom AsyncConfigurerSupport must be configured to return a ThreadPoolTaskExecutor decorated with SentryTaskDecorator:
Copied
import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.AsyncConfigurerSupport; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import io.sentry.spring.SentryTaskDecorator; import java.util.concurrent.Executor; @Configuration class AsyncMethodConfiguration extends AsyncConfigurerSupport { @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setTaskDecorator(new SentryTaskDecorator()); executor.initialize(); return executor; } } Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").