Spring Sentry #WIP#

TODO

https://github.com/spring-boot-tutorials/spring-sentry

In this article we will configure Spring Sentry.

Based on: https://docs.sentry.io/platforms/java/guides/spring-boot/

Install & Run Sentry

Execute in terminal

cd sentry
docker compose up -d
docker compose exec sentry sentry upgrade # to setup database and create admin user
docker compose restart sentry

Sentry is now running on public port http://localhost:9000.

Get DSN for this App

Example DSN property value:

  • sentry.dsn=http://ea8e63b0d20e459da55a48cb2f7b4c6b@localhost:9000/2

  • value template https://{public_key}@{host}:{port}/{project_id}

  • To obtain DSN for this project goto the Sentry UI - create new project - navigate to settings - click Client Keys (DSN)

  • save value into application.properties

Create Initial Code Base

Dependency Management

In pom.xml

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.sentry</groupId>
            <artifactId>sentry-bom</artifactId>
            <version>${sentry.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Properties

Add the following into ````:

sentry.dsn=http://ea8e63b0d20e459da55a48cb2f7b4c6b@localhost:9000/2
# value template https://{public_key}@{host}:{port}/{project_id}
# To obtain DSN for this project goto the Sentry UI
# - create new project
# - navigate to `settings`
# - click `Client Keys (DSN)`

# Add data like request headers and IP for users,
# see https://docs.sentry.io/platforms/java/guides/spring-boot/data-management/data-collected/ for more info
sentry.send-default-pii=true

# By default, only unhandled exceptions are sent to Sentry.
# This behavior can be tuned through configuring the
# sentry.exception-resolver-order property. For example,
# setting it to -2147483647 (the value of
# org.springframework.core.Ordered#HIGHEST_PRECEDENCE)
# ensures exceptions that have been handled by exception
# resolvers with higher order are sent to Sentry -
# including ones handled by @ExceptionHandler annotated methods
sentry.exception-resolver-order=-2147483647

Controller

Let’s create a new file src/main/java/com/example/spring_sentry/DefaultController.java

@RestController
public class DefaultController {

    @GetMapping({"/"})
    public String home() throws Exception {
        try {
            throw new UnsupportedOperationException("You shouldn't call this!");
        } catch (Exception e) {
            Sentry.captureException(e);
            throw e;
        }
    }
}

Create another controller src/main/java/com/example/spring_rest_docs/IndexController.java:

@RestController
@RequestMapping("/")
public class IndexController {

    static class CustomRepresentationModel extends RepresentationModel<CustomRepresentationModel> {
        public CustomRepresentationModel(Link initialLink) {
            super(initialLink);
        }
    }

    @GetMapping
    public CustomRepresentationModel index() {
        return new CustomRepresentationModel(linkTo(CrudController.class).withRel("crud"));
    }
}

Run & Verify Application

Build JAR

mvn clean package

CURL sentry.jar thingymajig

curl https://repo1.maven.org/maven2/io/sentry/sentry-opentelemetry-agent/8.16.0/sentry-opentelemetry-agent-8.16.0.jar -o sentry-opentelemetry-agent-8.16.0.jar

RUN application

SENTRY_AUTO_INIT=false
JAVA_TOOL_OPTIONS="-javaagent:sentry-opentelemetry-agent-8.16.0.jar"
java -jar target/spring-sentry-0.0.1-SNAPSHOT.jar