Spring Boot Actuator : : Monitor Health of Application


Once Spring Boot application deployed into production environment, you always want to monitor the application. This is because you want to ensure that the application must be always up and running and in case of any issues you want to fix it quickly. Therefore, an application monitoring tool is required so that you can analyze the health of your application.

Spring Boot has an in-built mechanism for monitoring application called as Actuator. It is a sub-project of Spring Boot. It offers several production grades features to monitor the application. Once you enable actuator in your Spring Boot application, a set of endpoints are exposed using which you can monitor and manage your application.  You can also integrate these endpoints with other application monitoring tools such Prometheus, Graphite etc.

Now let us see how you can enable Actuator in your Spring Boot application.

Spring Boot Actuator Configuration

Actuators can be easily enabled in your application by adding following spring-boot-actuator dependency in pom.xml file:

<dependency>
	  <groupId>org.springframework.boot</groupId>
	  <artifactId>spring-boot-starter-actuator</artifactId>  
</dependency> 

See More : Spring Boot: Custom Actuator EndPoints Creation

Actuator Endpoints

Once Actuator is enabled in your application, using actuator endpoints you can monitor and manage your application. These endpoints are exposed over HTTP in Spring MVC application using the ‘id’ of endpoints as the URL path along with /actuator as prefix. The following table shows some important actuator endpoints:

Actuator EndpointsDescription
/beansProvides list of all Spring beans available in the application
/configpropsProvides a collated list of all @ConfigurationProperties
/envExposes all the properties from Spring’s Configurable Environment
/infoDisplays arbitrary application information
/metricsDisplays metric information for the current application
/mappingsDisplays a collated list of all request mapping paths
/shutdownAllows the application to shutdown
/traceDisplays trace information, by default latest 100 HTTP requests
/healthProvides applications health information
Spring Boot Actuator Endpoints

Note : By default spring boot actuator only only allow the access of /health and /info end points because other end points provide application sensitive information.

You can also access the other specific end points by making below changes in your application.properties / application.yml file.

Access actuator all endpoints

You can add below properties to access all the end points:

#Access all actuator endpoints
management.endpoints.web.exposure.include=* 

Restrict specific actuator endpoints

You can add below exclude property to restrict the specific urls:

#Access all actuator endpoints
management.endpoints.web.exposure.include=*
#Retrict env endpoints to access
management.endpoints.web.exposure.exclude=env 

If you want to restrict access for more than one endpoints the specify as comma separated:

#Access all actuator endpoints
management.endpoints.web.exposure.include=*
#Retrict env endpoints to access
management.endpoints.web.exposure.exclude=env,beans

We will discuss on all the spring boot actuator end points in further sections.

If your application is running application on port number 8090, you can use below end point to get list of all the actuator endpoints and it will give you following response:

http://localhost:8090/actuator

Endpoint /actuator

By default actuator endpoint return only endpoints for /info and /health only as below.

Response:

{
	"_links": {
		"self": {
			"href": "http://localhost:8090/actuator",
			"templated": false
		},
		"health": {
			"href": "http://localhost:8090/actuator/health",
			"templated": false
		},
		"health-path": {
			"href": "http://localhost:8090/actuator/health/{*path}",
			"templated": true
		}
	}
}

If your actuator is enabled for all the endpoints as below then you will get complete list of end points of actuator:

#Access all actuator endpoints
management.endpoints.web.exposure.include=* 

Response:

{
	"_links": {
		"self": {
			"href": "http://localhost:8090/actuator",
			"templated": false
		},
		"beans": {
			"href": "http://localhost:8090/actuator/beans",
			"templated": false
		},
		"caches": {
			"href": "http://localhost:8090/actuator/caches",
			"templated": false
		},
		"caches-cache": {
			"href": "http://localhost:8090/actuator/caches/{cache}",
			"templated": true
		},
		"health": {
			"href": "http://localhost:8090/actuator/health",
			"templated": false
		},
		"health-path": {
			"href": "http://localhost:8090/actuator/health/{*path}",
			"templated": true
		},
		"info": {
			"href": "http://localhost:8090/actuator/info",
			"templated": false
		},
		"conditions": {
			"href": "http://localhost:8090/actuator/conditions",
			"templated": false
		},
		"configprops": {
			"href": "http://localhost:8090/actuator/configprops",
			"templated": false
		},
		"configprops-prefix": {
			"href": "http://localhost:8090/actuator/configprops/{prefix}",
			"templated": true
		},
		"env-toMatch": {
			"href": "http://localhost:8090/actuator/env/{toMatch}",
			"templated": true
		},
		"env": {
			"href": "http://localhost:8090/actuator/env",
			"templated": false
		},
		"loggers": {
			"href": "http://localhost:8090/actuator/loggers",
			"templated": false
		},
		"loggers-name": {
			"href": "http://localhost:8090/actuator/loggers/{name}",
			"templated": true
		},
		"heapdump": {
			"href": "http://localhost:8090/actuator/heapdump",
			"templated": false
		},
		"threaddump": {
			"href": "http://localhost:8090/actuator/threaddump",
			"templated": false
		},
		"metrics-requiredMetricName": {
			"href": "http://localhost:8090/actuator/metrics/{requiredMetricName}",
			"templated": true
		},
		"metrics": {
			"href": "http://localhost:8090/actuator/metrics",
			"templated": false
		},
		"scheduledtasks": {
			"href": "http://localhost:8090/actuator/scheduledtasks",
			"templated": false
		},
		"mappings": {
			"href": "http://localhost:8090/actuator/mappings",
			"templated": false
		}
	}
}

Now in further section will use the above endpoints of actuator and show the response of each API’s call.

Endpoint /health

This actuator health endpoint gives you the information about health of application. You can access this endpoint using the URL http://localhost:8090/actuator/health. It will give you following response:

{"status":"UP"}

The status will be UP if application is running and healthy. If application has some issues such as database is down etc. then you will get following response:

{"status":"DOWN"}

It tells only whether the status of application is UP or DOWN.  If you want to get details information about health of application then add the following property in the application.properties file:

management.endpoint.health.show-details=always
{
	"status": "UP",
	"components": {
		"db": {
			"status": "UP",
			"details": {
				"database": "H2",
				"validationQuery": "isValid()"
			}
		},
		"diskSpace": {
			"status": "UP",
			"details": {
				"total": 300662386688,
				"free": 277733244928,
				"threshold": 10485760,
				"exists": true
			}
		},
		"ping": {
			"status": "UP"
		}
	}
}

If you noticed from above response, it’s connected with database H2 and application consuming data disc spaces.

Endpoint /metrics

This actuator metrics endpoint displays various metrics options that can be checked for your application. You can access this endpoint using the URL http://localhost:8090/actuator/metrics. It will give you following response :

{
	"names": [
		"hikaricp.connections",
		"hikaricp.connections.acquire",
		"hikaricp.connections.active",
		"hikaricp.connections.creation",
		"hikaricp.connections.idle",
		"hikaricp.connections.max",
		"hikaricp.connections.min",
		"hikaricp.connections.pending",
		"hikaricp.connections.timeout",
		"hikaricp.connections.usage",
		"http.server.requests",
		"jdbc.connections.active",
		"jdbc.connections.idle",
		"jdbc.connections.max",
		"jdbc.connections.min",
		"jvm.buffer.count",
		"jvm.buffer.memory.used",
		"jvm.buffer.total.capacity",
		"jvm.classes.loaded",
		"jvm.classes.unloaded",
		"jvm.gc.live.data.size",
		"jvm.gc.max.data.size",
		"jvm.gc.memory.allocated",
		"jvm.gc.memory.promoted",
		"jvm.gc.pause",
		"jvm.memory.committed",
		"jvm.memory.max",
		"jvm.memory.used",
		"jvm.threads.daemon",
		"jvm.threads.live",
		"jvm.threads.peak",
		"jvm.threads.states",
		"logback.events",
		"process.cpu.usage",
		"process.start.time",
		"process.uptime",
		"system.cpu.count",
		"system.cpu.usage",
		"tomcat.sessions.active.current",
		"tomcat.sessions.active.max",
		"tomcat.sessions.alive.max",
		"tomcat.sessions.created",
		"tomcat.sessions.expired",
		"tomcat.sessions.rejected"
	]
}

The above metrics endpoint response has the name of individual metric. To get more information about these metrics you need to append the metric name to the URL. For example if you want to know more about jvm.memory.used metric then the URL will be http://localhost:8090/actuator/metrics/jvm.memory.used. This URL will give the following response:

{
	"name": "jvm.memory.used",
	"description": "The amount of used memory",
	"baseUnit": "bytes",
	"measurements": [
		{
			"statistic": "VALUE",
			"value": 1.67400072E8
		}
	],
	"availableTags": [
		{
			"tag": "area",
			"values": [
				"heap",
				"nonheap"
			]
		},
		{
			"tag": "id",
			"values": [
				"G1 Old Gen",
				"CodeHeap 'non-profiled nmethods'",
				"G1 Survivor Space",
				"Compressed Class Space",
				"Metaspace",
				"G1 Eden Space",
				"CodeHeap 'non-nmethods'"
			]
		}
	]
}

According to above response, you can get you application JVM memory space consumption, Garbage collectors configure etc.

Same way you can explore more about the application configuration, http requests hits, beans creation information by using actuators other endpoints.

You can also create your own Actuator endpoints. In next post you will learn about the the custom actuator end points creation.

See More : Spring Boot: Custom Actuator EndPoints Creation

Conclusion

In this post you learn about these points for Spring Boot actuators:

  • Spring Boot Actuator configuration and enabling.
  • Spring Boot Actuator endpoints
  • Spring Boot actuator specific endpoints enabling for end users.
  • Spring Boot Actuator health and metrics information retrieval.

Let me know your thoughts on this post.

Happy Learning !!!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s