Blog

Sentry with Spring Boot

07.07.2020 | Devops Backend | James McMahon

hero

After writing this article I've revised my approach. See this follow up article for details. The approach below still works, with some caveats.


We are trying out Sentry for error alerting at Focused Labs, and I wanted to set it up in my Spring Boot backend. For the most part, Sentry has been super easy to add to a project, but I hit a few little snags with the Spring Boot integration that I wanted to share with the community.

I'll keep this short, this is what I ended up with:

build.gradle.kts:

implementation("io.sentry:sentry-spring-boot-starter:1.7.30")
implementation("io.sentry:sentry-logback:1.7.30")

In some scenarios, this may lead to duplicate errors. The official docs warn against including both sentry-spring (which is pulled in by sentry-spring-boot-starter) and sentry-logback. Which makes sense since the Spring integration with Sentry is handling exceptions and sending them to Sentry and the Logback integration is watching logs, and then sending them to Sentry.

We are using GraphQL in our backend, so the sentry-spring behavior wasn't catching our exceptions since they are exclusively catching errors in the context of an MVC controller. Still, potentially, an exception would both be logged and caught by the Sentry exception handler, resulting in a duplicate error. If this becomes an issue, I'll have to revisit my dependencies.

This is why I am bothering with sentry-spring-boot-starter

application.yaml:

---
sentry:
  dsn: '<my-dsn>'
  stacktrace:
    app-packages:
      - com.mypackage
spring:
  profiles:
    active: dev
---
spring:
  profiles: dev
sentry:
  enabled: false
  environment: dev
---
spring:
  profiles: production
sentry:
  environment: production

Sweet sweet YAML.

Thanks to the configuration support in sentry-spring-boot-starter I can just locate my Sentry configuration right next to the rest of my Spring Boot configuration.

logback-spring.xml:

<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <appender name="SENTRY" class="io.sentry.logback.SentryAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>WARN</level>
        </filter>
    </appender>

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

By using include here, I am able to defer to the default Spring Boot log configuration and only override what I absolutely need to. In this case, adding an additional appender to the root logger.

And that's it! It looks straight forward when laid out here, but took a few iterations to get everything working. Hopefully, this helps other people out with the same process.

Share

Read More

Related Posts

related_image

06.30.2021 | Culture | Katy Scott

At Focused Labs, collaboration is key to how we work together; it helps our teams learn from each other, brings us closer and helps us become more efficient...

related_image

06.23.2021 | Culture | Austyn

Late-night feedings and diaper changes, the 3-4 month sleep regression, teething, and a growth spurt all mean I'm getting less sleep than...

related_image

05.12.2021 | Culture Backend Frontend | Ryan Taylor

Temporarily disrupts "normal" business operations and allow self-organized teams to rapid prototype around their interest areas

related_image

04.27.2021 | Culture | Erin Hochstatter

Several years ago, I'd been trying to find an approach to software consulting that made sense for me [...]

related_image

01.28.2021 | Backend | Parker Drake

Recently I found myself needing to validate fields in a Spring Boot controller written in Kotlin...

related_image

01.22.2021 | Tutorial | Luke Mueller

⌘+⇧+g is the way to go

related_image

01.21.2021 | Devops | Katy G

Kube jobs running wild? To delete successful jobs...

additional accent
accent
FocusedLabs

171 N Aberdeen St
Suite 400
Chicago, IL 60607

[email protected]

© 2021 FocusedLabs, All Rights Reserved.

  • facebook icon
  • twitter icon
  • linkedin icon
  • github icon