Spring
The sentry-spring library provides Spring MVC support for Sentry via:
- a HandlerExceptionResolver that sends unhandled exceptions to Sentry.
- attaches HTTP request information to all
SentryEvents recorded within the scope of the request - adds option to attach user information retrieved from HTTP request to all
SentryEvents recorded within the scope of the request - adds a
SentryUserProviderhook that can be used to provide additional user information
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
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-spring</artifactId>
<version>3.0.0</version>
</dependency>For other dependency managers see the central Maven repository.
Usage
The sentry-spring library provides @EnableSentry annotation that registers all required Spring beans. @EnableSentry can be places on any class annotated with @Configuration including main entry class in Spring Boot applications annotated with @SpringBootApplication.
import io.sentry.spring.EnableSentry;
// NOTE: Replace the test DSN below with YOUR OWN DSN to see the events from this app in your Sentry
// project/dashboard
@EnableSentry(dsn = "https://examplePublicKey@o0.ingest.sentry.io/0")
@Configuration
class SentryConfiguration {
}The DSN can be also proided through the system property sentry.dsn, environment variable SENTRY_DSN or the dsn property in sentry.properties file. See the configuration page for more details on external configuration.
Recording User Information From HTTP Request
In order to record user's IP address and Principal#name as the username, sending personal information flag has to be set to true.
import io.sentry.spring.EnableSentry;
@EnableSentry(dsn = "https://examplePublicKey@o0.ingest.sentry.io/0", sendDefaultPii = true)
@Configuration
class SentryConfiguration {
}Recording Custom User Information
In order to record custom user information, you care register a bean that implements SentryUserProvider interface.
import org.springframework.stereotype.Component;
import io.sentry.core.protocol.User;
import io.sentry.spring.SentryUserProvider;
@Component
class CustomSentryUserProvider implements SentryUserProvider {
public User provideUser() {
User user = User();
// ... set user information
return user
}
}