I wrote previously about our initial Sentry setup with Spring Boot, and let's just say mistakes were made.
Oh well, okay, it's not all bad, there is still some useful info there, which is why I am leaving it up, but there are some caveats.
- You'll see a scary looking message about Sentry being disabled on boot up.
- Errors / Warnings that happen at bootup will not be captured by Sentry.
Just show me how to make it work.
sentry.properties: Put at the root of the Spring classpath
logback-spring.xml: Also put at root of the Spring classpath
<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>
- name: SENTRY_ENVIRONMENT value: <enviroment> - name: SENTRY_DSN value: "<my-dsn>"
Set the environment variables on a per deployment basis, which is going vary depending on your infrastructure. (We have ours as a Helm deployment value).
To disable Sentry, omit the
SENTRY_DSN environment variable.
So when I initially setup Sentry, I very much wanted to store my Sentry configuration alongside my Spring Boot project configuration. As you may have noticed above, I've entirely abandoned that approach, which I am slightly sad about since I like having configuration in one place.
So what went wrong, well let's look at the two issues we had:
Specifically this error,
*** Couldn't find a suitable DSN, Sentry operations will do nothing! See documentation: https://docs.sentry.io/clients/java/ ***
But then you'll start seeing errors going to Sentry just fine. What the heck? This is connected to the next issue.
While you will see errors go to Sentry, it will omit errors at bootup. Why? Because the Spring configuration isn't loaded until the Spring Application Context is created, which means that Sentry misses the boat.
I won't keep you in suspense, here is what is happening:
- Logback boots up.
- The Sentry Logback appender boots up.
- Sentry finds no configuration, in either properties or the runtime environment.
- It logs a missing DSN warning.
- Spring Boot boots.
- The Sentry Spring Boot starter configures Sentry.
- Sentry will now be able to capture errors.
So if you care about capturing errors on bootup,
sentry-spring-boot-starter isn't really doing anything for you. In fact, with the configuration above, all you need is
sentry-logback as a dependency.
So the choices for using Sentry with Spring Boot are the following:
- Use the logback dependency and capture errors both on startup and runtime but use environmental / properties for configuration.
- Use the Spring Boot starter and capture errors only at runtime but be able to use
application.yamlfor configuration. If you want to capture logs, you'll need to put off with a scary-looking warning at startup.
If the latter sounds preferable, check out my first article. But the method I'd recommend is above, even if the configuration options are worse.
Once again, I hope this was helpful. I've been staring Sentry for a bit now, so if you have any questions, feel free to ask below.