Read the original
1, the introduction of
When an application is running in production, it is necessary to monitor its health. By knowing the health of your application in real time, you can be warned of problems before they occur and resolve them before customers notice.
In this article, we will create a Spring Boot application that monitors the system with the help of Spring Actuator, Micrometer, Prometheus, and Grafana. Among them, Spring Actuator and Micrometer are part of Spring Boot App.
The purpose of the different components is briefly explained:
-
Spring Actuator: Provides multiple Web interfaces in apps that learn what’s going on inside the application while it’s running. For more information, see Spring Boot Actuator in Spring Boot 2.0.
-
Micrometer: It provides a common API for collecting performance data on the Java platform. It provides multiple metric types (Timers, Guauges, Counters, etc.) and supports access to different monitoring systems, such as Influxdb, Graphite, and Prometheus. Spring Boot Actuator supports this.
-
Prometheus: A time series database for collecting metrics.
-
Grafana: Dashboard for displaying metrics.
Below, we’ll look at each component separately. The code used in this article is archived on GitHub.
2. Create a sample application
The first thing to do is create an application that you can monitor. Using Spring Initializr and adding Spring Boot Actuator, Prometheus, and Spring Web dependencies, we created a Spring MVC application as shown below.
Spring Boot basic will not introduce, recommend the actual combat tutorial:
Github.com/javastacks/…
@RestController public class MetricsController { @GetMapping("/endPoint1") public String endPoint1() { return "Metrics for endPoint1"; } @GetMapping("/endPoint2") public String endPoint2() { return "Metrics for endPoint2"; }}Copy the code
Start the application:
$ mvn spring-boot:run
Copy the code
Verify that the interface is normal:
$ curl http://localhost:8080/endPoint1Metrics for endPoint1$ curl http://localhost:8080/endPoint2Metrics for endPoint2
Copy the code
Verify the Spring Actuator interface. To make the response message readable, we format the message using python-mjson. tool.
$ curl http://localhost:8080/actuator | python -mjson.tool
...
{
"_links":{
"self":{
"href":"http://localhost:8080/actuator",
"templated":false
},
"health":{
"href":"http://localhost:8080/actuator/health",
"templated":false
},
"health-path":{
"href":"http://localhost:8080/actuator/health/{*path}",
"templated":true
},
"info":{
"href":"http://localhost:8080/actuator/info",
"templated":false
}
}
}
Copy the code
By default, the preceding information is displayed. In addition, Spring Actuator provides more information, but you need to enable it. To enable Prometheus, you need to add the following information to the application.properties file.
management.endpoints.web.exposure.include=health,info,prometheus
Copy the code
Restart the application, visit http://localhost:8080/actuator/prometheus from Prometheus pull data, return a large amount of information available indicators. We show only a small portion of the output here because it is a long list.
$ curl http://localhost:8080/actuator/prometheus # HELP jvm_gc_pause_seconds Time spent in GC pause # TYPE Jvm_gc_pause_seconds summary jVM_gc_PAuse_seconds_count {action="end of minor GC",cause="G1 Evacuation Pause",} 2.0 Jvm_gc_pause_seconds_sum {action="end of minor GC",cause="G1 Evacuation Pause",} 0.009... jVM_gc_PAuse_seconds_sum {action="end of minor GC",cause="G1 Evacuation Pause",} 0.009...Copy the code
As mentioned earlier, micrometers are also required. Micrometer provides a simple dashboard for the most popular monitoring systems, allowing instrumentation of JVM applications regardless of which vendor provides the metrics. It works like SLF4J, except instead of Logging, it focuses on Application metrics. In short, it is SLF4J for the application monitoring world.
Spring Boot Actuator provides automatic configuration for micrometers. Spring Boot2 introduces micrometer in spring-boot-Actuator and reconstructs the metrics of 1.x. In addition, there are more monitoring systems that support docking (Atlas, Datadog, Ganglia, Graphite, Influx, JMX, NewRelic, Prometheus, SignalFx, StatsD, Wavefront).
The updated application.properties file looks like this:
management.endpoints.web.exposure.include=health,info,metrics,prometheus
Copy the code
Restart the application, and retrieve data from http://localhost:8080/actuator/metrics.
$ curl http://localhost:8080/actuator/metrics | python -mjson.tool
...
{
"names": [
"http.server.requests",
"jvm.buffer.count",
"jvm.buffer.memory.used",
...
Copy the code
Specific information can be retrieved directly from the indicator name. For example, if you were querying the HTTP.server. requests metric, you could retrieve it as follows:
$ curl http://localhost:8080/actuator/metrics/http.server.requests | python -mjson.tool
...
{
"name": "http.server.requests",
"description": null,
"baseUnit": "seconds",
"measurements": [
{
"statistic": "COUNT",
"value": 3.0
},
{
"statistic": "TOTAL_TIME",
"value": 0.08918682
},
...
Copy the code
3. Add Prometheus
Prometheus is an open source monitoring system from the Cloud Native Computing Foundation. Because our app has a /actuator/Prometheus endpoint for Prometheus to grab data, you can now configure Prometheus to monitor your Spring Boot application.
There are several ways to install Prometheus, and in this article, we will run Prometheus in a Docker container.
You need to create a prometheus.yml file to add to the Docker container.
global:
scrape_interval:15s
scrape_configs:
- job_name: 'myspringmetricsplanet'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['HOST:8080']
Copy the code
-
Scrape_interval: Metrics for how often Prometheus polls an application
-
Job_name: indicates the name of a polling task
-
Metrics_path: indicates the URL path of an indicator
-
Targets: indicates the host name and port number. When used, replace HOST with the IP address of the HOST
If you have trouble finding an IP address on Linux, you can use the following command:
$ ip -f inet -o addr show docker0 | awk '{print $4}' | cut -d '/' -f 1
Copy the code
Start the Docker container and map the local promethes. yml file to the file in the Docker container.
$ docker run \
-p 9090:9090 \
-v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus
Copy the code
After successful start-up Docker container, the first test whether Prometheus to data collected through http://localhost:9090/targets.
As shown in the figure above, we encountered the Context Deadline Exceeded error, causing Prometheus to fail to access the Spring Boot application running on the host. How to solve it?
This error can be resolved by adding a Docker container to your host network, which will give Prometheus access to the Spring Boot application.
$ docker run \
--name prometheus \
--network host \
-v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
-d \
prom/prometheus
Copy the code
Verify again that the status indicator is UP.
Prometheus metrics can now be displayed. By visiting http://localhost:9090/graph, input http_server_requests_seconds_max in the search box and click “perform” button, will provide you with the maximum execution time during the request.
4. Add Grafana
The last component added was Grafana. While Prometheus can display metrics, Grafana can help you display metrics in a more elegant dashboard. Grafana also supports several installations, and in this article, we’ll run it in a Docker container as well.
$ docker run --name grafana -d -p 3000:3000 grafana/grafana
Copy the code
You can visit Grafana by clicking http://localhost:3000/.
The default user name and password are admin and admin. After clicking the “Login” button, you will need to change the default password.
The next thing to do is add a data source. Click the Configuration icon in the left sidebar, and then select Data Sources.
Click the Add Data Source button.
Prometheus at the top of the list, select Prometheus.
Fill in the URL to Access Prometheus, set HTTP Access to Browser, and click the Save&Test button at the bottom of the page.
Pay attention to the public account, learn more Java dry goods!
When all is well, a green notification banner is displayed indicating that the data source is working.
Now it’s time to create the dashboard. You can customize one, but you can also use open source dashboards. A common dashboard for displaying Spring Boot metrics is the JVM dashboard.
In the left sidebar, click the + sign, and then select Import.
Enter the URL for the JVM dashboard grafana.com/grafana/das…
Enter a meaningful name for the dashboard (for example, MySpringMonitoringPlanet), select Prometheus as the data source, and click the Import button.
So far, you can use a cool Grafana dashboard.
Custom panels can also be added to the dashboard. On the top of the dashboard, click the Add Panel icon.
Click Add New Panel.
In the Metrics field, enter http_server_requests_SECONds_max, and in the Panel Title field in the right pane, enter the Panel name.
Finally, click the Apply button in the upper right and your panel will be added to the dashboard. Don’t forget to save the dashboard.
Set up some load for the application and see what happens to the HTTP_server_REQUESTs_SECONds_max metric on the dashboard.
$ watch -n 5 curl http://localhost:8080/endPoint1$ watch -n 10 curl http://localhost:8080/endPoint2
Copy the code
5, conclusion
In this article, you learned how to add some basic monitoring to your Spring Boot application. This is easy to do by combining Spring Actuator, Micrometer, Prometheus and Grafana.
Of course, this is just a starting point, but from here you can extend and configure more, more specific metrics for Spring Boot applications.