Published on 2024-02-05, by Javed Shaikh
Spring Boot Actuator is a powerful feature that provides production-ready features to help you monitor and manage your application. It exposes various endpoints that can be used to gather metrics, health checks, and other operational information, making it an essential tool for performance monitoring.
To use Spring Boot Actuator, you need to add the following dependency to your pom.xml (for Maven ):
1<dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-actuator</artifactId>
4</dependency>
In your application.properties or application.yml, you can configure which endpoints to expose. For example:
1management.endpoints.web.exposure.include=health,info,metrics
This configuration exposes the health, info, and metrics endpoints.
Once you have Actuator set up, you can access the various endpoints to monitor your application. Here are some commonly used endpoints:
Health Endpoint: Provides the health status of your application.
Access: http://localhost:8080/actuator/health
Example Response:
1{
2 "status": "UP"
3}
jsonDownloadCopy code{"app":{"name":"My Application","version":"1.0.0"}}
Info Endpoint: Displays application information such as version, description, and custom properties.
Access: http://localhost:8080/actuator/info
Example Response:
1{
2 "app": {
3 "name": "My Application",
4 "version": "1.0.0"
5 }
6}
Metrics Endpoint: Provides various metrics about your application, including memory usage, garbage collection, and more.
Access: http://localhost:8080/actuator/metrics
Example Response:
1{
2 "names": [
3 "jvm.memory.used",
4 "jvm.gc.pause",
5 "system.cpu.usage"
6 ]
7}
You can also create custom metrics to monitor specific aspects of your application. Here’s how to do it:
Create a Custom Metric:
1import org.springframework.boot.actuate.metrics.MetricsEndpoint;
2import org.springframework.stereotype.Component;
3import io.micrometer.core.instrument.MeterRegistry;
4
5@Component
6public class CustomMetrics {
7
8 private final MeterRegistry meterRegistry;
9
10 public CustomMetrics(MeterRegistry meterRegistry) {
11 this.meterRegistry = meterRegistry;
12 }
13
14 public void recordCustomMetric() {
15 meterRegistry.counter("custom.metric.name").increment();
16 }
17}
Access Your Custom Metric:
After we have defined your custom metric, we can access it through the metrics endpoint. For example, if we incremented the custom metric in our application, we can check its value:
Access: http://localhost:8080/actuator/metrics/custom.metric.name
Example Response:
1{
2 "name": "custom.metric.name",
3 "measurements": [
4 {
5 "statistic": "COUNT",
6 "value": 5.0
7 }
8 ],
9 "availableTags": []
10}
When exposing actuator endpoints, it’s crucial to secure them, especially in production environments. You can use Spring Security to restrict access. Here’s a simple configuration:
1import org.springframework.context.annotation.Configuration;
2import org.springframework.security.config.annotation.web.builders.HttpSecurity;
3import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
4import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
5
6@Configuration
7@EnableWebSecurity
8public class SecurityConfig extends WebSecurityConfigurerAdapter {
9 @Override
10 protected void configure(HttpSecurity http) throws Exception {
11 http
12 .authorizeRequests()
13 .antMatchers("/actuator/**").authenticated()
14 .and()
15 .httpBasic(); // Use basic authentication
16 }
17}
Spring Boot Actuator is an invaluable tool for performance monitoring in your applications. By exposing various endpoints, it allows us to gain insights into the health and metrics of our application, enabling us to detect issues early and optimize performance. With the ability to create custom metrics, we can tailor monitoring to your specific needs.
However we need to make sure to secure application endpoints and leverage the full potential of Spring Boot Actuator for a robust monitoring solution!
Spring Boot applications are known for their ease of development and rapid deployment, but as your application scales, performance optimization becomes crucial.
2024-05-08
Java Records significantly reduce boilerplate code. With records, we don’t need to write getters, toString(), equals(), or hashCode() methods.
2024-10-09
When it comes to building high-performance, scalable TCP servers, the combination of Reactor Netty and Spring Boot offers a powerful solution. Reactor Netty provides a non-blocking and reactive approach to network programming, while Spring Boot simplifies the setup and configuration process
2024-01-02
Understanding how to optimize memory configuration can dramatically improve your application's throughput, reduce latency, and prevent dreaded OutOfMemoryError exceptions
2024-07-17