Spring Boot

The sentry-spring-boot-starter library enhances Sentry Spring support with an auto-configuration for Spring Boot providing following features:

  • fine-grained configuration via application.properties or application.yaml
  • automatically includes the release when Spring Boot Git integration is configured
  • automatically registering BeforeSendCallback, BeforeBreadcrumbCallback, EventProcessor, Integration beans

For the best experience we recommend using Sentry Spring Boot integration with one of the logging framework integrations as they seamlessly work together:

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.

Install

Copied
<dependency>
    <groupId>io.sentry</groupId>
    <artifactId>sentry-spring-boot-starter</artifactId>
    <version>3.1.1</version>
</dependency>

Configure

The sentry-spring-boot-starter must be provided with a sentry.dsn property via application.properties or application.yml:

application.properties
Copied
sentry.dsn=https://examplePublicKey@o0.ingest.sentry.io/0

Sentry Spring Boot integration auto-configures sentry.in-app-packages property with the package where @SpringBootApplication or @SpringBootConfiguration annotated class is located.

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.

Copied
sentry.send-default-pii=true

Recording Custom User Information

To record custom user information, you can register a bean that implements SentryUserProvider interface.

Copied
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
    }
}

Using Git Commit ID As The Release

When Spring Boot is configured to generate Git information every event triggered by Sentry will have a release field set to the current Git commit ID that will enable Monitor Release Health Sentry feature.

This feature can be disabled in application.properties file:

application.properties
Copied
sentry.use-git-commit-id-as-release=false

Registering Custom Event Processor

Any Spring bean implementing EventProcessor will be automatically set on SentryOptions during Sentry SDK auto-configuration. There can be multiple event processors registered in single application.

Copied
import io.sentry.SentryEvent;
import io.sentry.EventProcessor;
import org.springframework.stereotype.Component;

@Component
public class CustomEventProcessor implements EventProcessor {
  @Override
  public SentryEvent process(SentryEvent event, Object hint) {
    // modify the event or return null to drop it
    return event;
  }
}

Registering Custom Before Send Callback

Any Spring bean implementing the BeforeSendCallback will be automatically set on SentryOptions during the SDK's auto-configuration. Note that there can be only a single bean set this way.

Copied
import io.sentry.SentryEvent;
import io.sentry.SentryOptions;
import org.springframework.stereotype.Component;

@Component
public class CustomBeforeSendCallback implements SentryOptions.BeforeSendCallback {
  @Override
  public SentryEvent execute(SentryEvent event, Object hint) {
    // Example: Never send server name in events
    event.setServerName(null);
    return event;
  }
}

Registering Custom Before Breadcrumb Callback

Any Spring bean implementing the BeforeBreadcrumbCallback will be automatically set on SentryOptions during the SDK's auto-configuration. Note that there can be only a single bean set this way.

Copied
import io.sentry.Breadcrumb;
import io.sentry.SentryOptions;
import org.springframework.stereotype.Component;

@Component
public class CustomBeforeBreadcrumbCallback implements SentryOptions.BeforeBreadcrumbCallback {

  @Override
  public Breadcrumb execute(Breadcrumb breadcrumb, Object hint) {
    // Don't add breadcrumbs with message containing:
    if (breadcrumb.getMessage() != null
      && breadcrumb.getMessage().contains("bad breadcrumb")) {
      return null;
    }
    return breadcrumb;
  }
}

Using With Logging Framework Integration

For the best experience we recommend using Sentry Spring Boot integration with one of the logging framework integrations as they seamlessly work together.

Logback

To use Sentry Logback integration in Spring Boot application you must include a dependency to sentry-logback module and Sentry Spring Boot Starter will auto-configure SentryAppender:

Copied
<dependency>
    <groupId>io.sentry</groupId>
    <artifactId>sentry-logback</artifactId>
    <version>3.1.1</version>
</dependency>

Minimum logging levels for SentryAppender can be configured in application.properties or application.yml file.

Copied
sentry.logging.minimum-event-level=info
sentry.logging.minimum-breadcrumb-level=debug

The default values are:

  • info or higher to include a log message as breadcrumb.
  • error or higher will send an event to Sentry.

When SentryAppender auto-configuration does not suit your needs, it can be turned off by setting:

Copied
sentry.logging.enabled=false

If you decide to opt-out from application.properties based Spring Boot logging configuration and configure logging in logback-spring.xml file, SentryAppender can be configured in the following way:

Copied
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
  <include resource="org/springframework/boot/logging/logback/console-appender.xml" />

  <appender name="SENTRY" class="io.sentry.logback.SentryAppender" />

  <root level="info">
    <appender-ref ref="CONSOLE" />
    <appender-ref ref="SENTRY" />
  </root>
</configuration>

Log4j2

To use Sentry Log4j2 integration in Spring Boot application, follow the guide on configuring Log4j2 with Spring Boot and configure SentryAppender in log4j2.xml file:

Copied
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" packages="org.apache.logging.log4j.core,io.sentry.log4j2">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <Sentry name="Sentry" />
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Sentry"/>
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>
You can edit this page on GitHub.