Spring

The sentry-spring library provides Spring support for Sentry via a HandlerExceptionResolver that sends exceptions to Sentry. Once this integration is configured you can also use Sentry’s static API, as shown on the usage page, in order to do things like record breadcrumbs, set the current user, or manually send events.

The source can be found on Github.

Installation

Using Maven:

<dependency>
    <groupId>io.sentry</groupId>
    <artifactId>sentry-spring</artifactId>
    <version>1.6.3</version>
</dependency>

Using Gradle:

compile 'io.sentry:sentry-spring:1.6.3'

Using SBT:

libraryDependencies += "io.sentry" % "sentry-spring" % "1.6.3"

For other dependency managers see the central Maven repository.

Usage

The sentry-spring library provides two classes that can be enabled by registering them as Beans in your Spring application.

Recording Exceptions

In order to record all exceptions thrown by your controllers, you can register io.sentry.spring.SentryExceptionResolver as a Bean in your application. Once registered, all exceptions will be sent to Sentry and then passed on to the default exception handlers.

Note that you should not configure the SentryExceptionResolver alongside a logging integration (such as sentry-logback), or you will most likely double-report exceptions. You should use one or the other depending on your needs. A logging integration is more general and will capture errors (and possibly warnings, depending on your configuration) that occur inside or outside of a Spring controller.

Configuration via web.xml:

<bean class="io.sentry.spring.SentryExceptionResolver"/>

Or via a configuration class:

@Bean
public HandlerExceptionResolver sentryExceptionResolver() {
    return new io.sentry.spring.SentryExceptionResolver();
}

Next, you’ll need to configure your DSN (client key) and optionally other values such as environment and release. See the configuration page for ways you can do this.

Spring Boot HTTP Data

Spring Boot doesn’t automatically load any javax.servlet.ServletContainerInitializer, which means the Sentry SDK doesn’t have an opportunity to hook into the request cycle to collect information about the HTTP request. In order to add HTTP request data to your Sentry events in Spring Boot, you need to register the io.sentry.spring.SentryServletContextInitializer class as a Bean in your application.

Configuration via web.xml:

<bean class="io.sentry.spring.SentryServletContextInitializer"/>

Or via a configuration class:

@Bean
public ServletContextInitializer sentryServletContextInitializer() {
    return new io.sentry.spring.SentryServletContextInitializer();
}

After that, your Sentry events should contain information such as HTTP request headers.