--Various information can be easily obtained by using SpringBoot Actuator. --Only add and configure dependent libraries to use the minimum functionality --Be careful about security as you will be able to refer to and change system operation information.
It is not the end of creating a system, but it is also important to operate it stably and how much it is used. Even with DevOps and recent agile, it's important to get feedback after the release and take the next strategy. Feedback is not only "I want you to do this" and "I want this function, I don't need it" from the user, but also "I don't use this function" or "This function suddenly started to be used" from the system operator. It is also important to notice from the provider side.
Spring Boot has a function called Spring Boot Actuator, which is a function to obtain necessary information in the latter current system, so we would like to introduce it.
With the extension function of SpringBoot, you can easily get various information. Typical ones
-Is the app running? (Health check) --JVM settings --Application settings
It is information such as. Details can be found at the link below.
https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html
How to Get Started It's easy, so I'll use Spring Initializr. The version of Spring Boot uses 2.3.0 M1 this time. Select Spring Web and Spring Boot Actuator for Dependencies.
After completing the settings, press Genarate to download the source code template. When you're done, unzip it and open it in your preferred IDE.
If you look at pom.xml, you can see that the dependent libraries for SpringBoot Actuator are listed.
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
The information that can be learned with this function can be useful information for attackers who exploit vulnerabilities, so the function is turned off by default. Change the application settings to enable it.
application.properties
management.endpoints.enabled-by-default=true
management.endpoints.web.exposure.include=*
management.endpoints.enabled-by-default sets information acquisition availability by default, management.endpoints.web.exposure.include is a setting that enables REST endpoints. The former is set to true and can be obtained by default, and the latter is set to * in the sense that it is enabled for all endpoints.
After setting, build & start. You can follow the normal Spring Boot app.
Build with mvn package
and start with java -jar target / demo-0.0.1-SNAPSHOT.jar
.
Once started, try accessing http: // localhost: 8080 / actuator /
.
Then, you can get the endpoint list in JSON.
curl http://localhost:8080/actuator | jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1659 0 1659 0 0 162k 0 --:--:-- --:--:-- --:--:-- 162k
{
"_links": {
"self": {
"href": "http://localhost:8080/actuator",
"templated": false
},
"beans": {
"href": "http://localhost:8080/actuator/beans",
"templated": false
},
"caches-cache": {
"href": "http://localhost:8080/actuator/caches/{cache}",
"templated": true
},
"caches": {
"href": "http://localhost:8080/actuator/caches",
"templated": false
},
"health-path": {
"href": "http://localhost:8080/actuator/health/{*path}",
"templated": true
},
"health": {
"href": "http://localhost:8080/actuator/health",
"templated": false
},
"info": {
"href": "http://localhost:8080/actuator/info",
"templated": false
},
"conditions": {
"href": "http://localhost:8080/actuator/conditions",
"templated": false
},
"shutdown": {
"href": "http://localhost:8080/actuator/shutdown",
"templated": false
},
"configprops": {
"href": "http://localhost:8080/actuator/configprops",
"templated": false
},
"env": {
"href": "http://localhost:8080/actuator/env",
"templated": false
},
"env-toMatch": {
"href": "http://localhost:8080/actuator/env/{toMatch}",
"templated": true
},
"loggers-name": {
"href": "http://localhost:8080/actuator/loggers/{name}",
"templated": true
},
"loggers": {
"href": "http://localhost:8080/actuator/loggers",
"templated": false
},
"heapdump": {
"href": "http://localhost:8080/actuator/heapdump",
"templated": false
},
"threaddump": {
"href": "http://localhost:8080/actuator/threaddump",
"templated": false
},
"metrics-requiredMetricName": {
"href": "http://localhost:8080/actuator/metrics/{requiredMetricName}",
"templated": true
},
"metrics": {
"href": "http://localhost:8080/actuator/metrics",
"templated": false
},
"scheduledtasks": {
"href": "http://localhost:8080/actuator/scheduledtasks",
"templated": false
},
"mappings": {
"href": "http://localhost:8080/actuator/mappings",
"templated": false
}
}
}
Since the amount is large, I will not explain everything, but for example, if it is health, it shows that the life and death state of the application can be acquired with this endpoint as a health check function.
curl http://localhost:8080/actuator/health | jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 15 0 15 0 0 35 0 --:--:-- --:--:-- --:--:-- 35
{
"status": "UP"
}
Another way to get environmental information is to publish it on an endpoint called env.
curl http://localhost:8080/actuator/env | jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 12493 0 12493 0 0 154k 0 --:--:-- --:--:-- --:--:-- 154k
{
"activeProfiles": [],
"propertySources": [
{
"name": "server.ports",
"properties": {
"local.server.port": {
"value": 8080
}
}
},
{
"name": "servletContextInitParams",
"properties": {}
},
{
"name": "systemProperties",
"properties": {
"java.runtime.name": {
"value": "OpenJDK Runtime Environment"
},
"java.protocol.handler.pkgs": {
"value": "org.springframework.boot.loader"
},
"sun.boot.library.path": {
"value": "/Users/user/.sdkman/candidates/java/8.0.212-amzn/jre/lib"
},
"java.vm.version": {
"value": "25.212-b04"
},
"gopherProxySet": {
"value": "false"
},
"java.vm.vendor": {
"value": "Amazon.com Inc."
},
"java.vendor.url": {
"value": "https://aws.amazon.com/corretto/"
},
"path.separator": {
"value": ":"
},
"java.vm.name": {
"value": "OpenJDK 64-Bit Server VM"
},
"file.encoding.pkg": {
"value": "sun.io"
},
"user.country": {
"value": "JP"
},
"sun.java.launcher": {
"value": "SUN_STANDARD"
},
"sun.os.patch.level": {
"value": "unknown"
},
"PID": {
"value": "59317"
},
"java.vm.specification.name": {
"value": "Java Virtual Machine Specification"
},
"user.dir": {
"value": "/Users/user/oper/spring/spring-docker"
},
"java.runtime.version": {
"value": "1.8.0_212-b04"
},
"java.awt.graphicsenv": {
"value": "sun.awt.CGraphicsEnvironment"
},
"java.endorsed.dirs": {
"value": "/Users/user/.sdkman/candidates/java/8.0.212-amzn/jre/lib/endorsed"
},
"os.arch": {
"value": "x86_64"
},
"java.io.tmpdir": {
"value": "/var/folders/1m/v_pnk3kd3hj0558d1z3nsnzh0000gn/T/"
},
"line.separator": {
"value": "\n"
},
"java.vm.specification.vendor": {
"value": "Oracle Corporation"
},
"os.name": {
"value": "Mac OS X"
},
"sun.jnu.encoding": {
"value": "UTF-8"
},
Omitted below.
If you look here, what is the OS, what is the Java implementation, version, and character encoding? You can get information such as.
In addition, Beans that can get a list of registered beans and configprops that can get the set contents are convenient for checking the environment at the time of usual troubleshooting.
So far, we have only introduced Endpoint, which is provided by default, but you can customize it. If there is something you want to return specially in the application, you can create an endpoint and easily return it in JSON. I will omit the example here, so if you want to do it, please see the official guide.
https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-endpoints-custom
What did you think? This time, I couldn't find out how to get the operation statistics information on how much functions such as necessary are used in DevOps, but I hope you can think that it seems easy to get the environmental information. If you haven't installed the (?) Spring Boot app, Spring Boot Actuator, please try it.
Recommended Posts