Seamless error monitoring with Spring Boot and Raygun
Posted Sep 9, 2024 | 4 min. (646 words)This guest post comes from long-time Raygun customer Midtrans, a leading payment gateway in Southeast Asia. As Midtrans grew, so did the number of applications requiring error monitoring. To tackle the challenges of scaling and standardizing Raygun across multiple teams and services, they created a custom Spring Boot starter for Raygun. Now, Midtrans is excited to share this open-source Spring Boot auto-configuration with the community. Raygun is proud to highlight this contribution from our friends at Midtrans.
At Midtrans, we started using Raygun back in January 2014 with version 1.3.2, and it quickly became our go-to tool for error monitoring. As the company grew, we began developing more applications, and naturally, we needed to monitor those as well. To handle this, we copied the Raygun setup code from our original application into the new ones.
As our organization continued to expand, we split into smaller teams. Each team started building their own applications, adding their own Raygun setup, and over time, this led to inconsistencies in how Raygun was configured across Midtrans.
One of the key issues we encountered because of this lack of standardization was processing thread exhaustion, which happened when some apps weren’t using a thread pool to send errors with Raygun. While some apps had a thread pool in place, others didn’t, which resulted in performance problems. We realized we needed a standardized approach, and since we’re primarily a Spring Boot shop, writing our own Spring Boot starter for Raygun seemed like the ideal solution.
We went ahead and developed a custom starter, rolling it out in a few of our applications and now, we’re excited to share our Raygun auto-configuration as an open-source library with the community.
Integration Steps
Report Errors
You need a Raygun account and a Spring Boot application. Do note that the custom starter was written with Spring Boot version 2.7.x. Even though we never tested with newer Spring Boot versions, they should work.
Add the dependency in your build.gradle
file. Please adjust accordingly if you are using Maven.
dependencies {
implementation 'com.midtrans:raygun-spring-boot-starter:0.7.3'
}
Add your Raygun API key to your application.properties
file. Please adjust accordingly if you are using environment variables or other means of externalized configuration.
raygun.api-key={your-api-key-value}
A RaygunTemplate
bean is auto-configured and can be autowired.
@Component
class UserService {
final RaygunTemplate;
UserService(RaygunTemplate raygunTemplate) {
this.raygunTemplate = raygunTemplate;
}
void businessLogic() {
try {
//some business logic...
} catch (Exception ex) {
raygunTemplate.send(ex);
}
}
}
You can manually send exceptions to Raygun using the template bean.
If your Spring Boot application is a web or a web service application, exceptions thrown by your controllers or your endpoints are sent to Raygun automatically. You do not have to do anything for seamless error monitoring with Spring Boot and Raygun.
Your application errors will start showing in your Raygun dashboard and your email. If you set up a notification integration with Slack, Microsoft Teams, or other tools, the errors would also be sent to those integrations.
Exclude Errors
One of the common use cases we had was to exclude the errors being sent to Raygun. To achieve this, you only need to register a bean.
@Component
class UserRaygunExcludeExceptionRegistrar implements RaygunExceptionExcludeRegistrar {
@Override
public void registerExceptions(RaygunExceptionExcludeRegistry registry) {
registry.registerException(RuntimeException.class);
}
}
Documentation
If you need further assistance, please check Spring Boot Raygun documentation or our README in our repository.
Issues and Features
In case you found a bug or need more features and more integrations with Spring Boot–integration with RestTemplate
perhaps–please open an issue in our repository. We would be happy to discuss further with you. Also, do not hesitate to contribute back and open a pull request to our repository.
Raygun support
If you have a question about Raygun or their products, you can contact the support team here or read Raygun’s documentation.
Not a Raygun customer? Try out the full Crash Reporting application free for 14 days!