Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
In enterprise applications, monitoring is often critical to help prevent failures, predict trends, alert when thresholds are reached, and provide more information for troubleshooting production problems. If we do not know the operation of our program, it will take more time to troubleshoot the online system when there is an accident. If we can monitor it in advance, we can make early preparations, so as to avoid the mess after the accident. Of course, it will not prevent the system from producing some accidents, but it can reduce the occurrence of system accidents. At the same time, we can also see system problems and optimize them early to avoid bigger accidents.
1. Spring Boot Actuator
According to the official website, Spring Boot includes many additional functions to help you monitor and manage your applications. You can use HTTP or JMX to obtain application health status and collect other metrics through an endpoint.
The Spring Boot Actuator module is provided by Spring Boot and integrates the monitoring and management functions described above. Like other Spring Boot modules, it’s very convenient and out of the box, using the Actuator to monitor our applications using HTTP or JMX.
JMX(Java Management Extensions) : Management and monitoring interface of the Java platform. Any program that accesses this interface according to the JMX specification can obtain all Management and monitoring information.
The following is a brief introduction to how Spring Boot Actuator is used. See the official documentation for specific usage methods, which is always worth believing.
1.1 Adding a Dependency
If using Maven management, it is:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
Copy the code
If you use Gradle, it looks like this:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
}
Copy the code
1.2 Enabling An Endpoint
Spring Boot monitors and interacts with applications through endpoints. Spring Boot provides many native endpoints, such as Health, to help you monitor the status (availability) of your applications, and you can add custom endpoints. Each endpoint can be individually turned on or off and exposed to external systems via HTTP or JMX. If the HTTP mode is used, the URL prefix is /actuator/health. For example, the URL of health is /actuator/health.
By default, all endpoints are enabled by default, except for shutdown (allowing the system to gracefully shutdown). Management.endpoint.
. Enabled Allows you to set the status of a port, such as the shutdown port.
management.endpoint.shutdown.enabled=true
Copy the code
1.3 Exposed endpoints
However, the port may contain some sensitive data. Therefore, the native port of Spring Boot supports only HTTP or JMX by default. For example, the shutdown port supports only JMX by default, and the health port supports only JMX. HTTP is also supported.
ID | JMX | Web |
---|---|---|
auditevents |
Yes | No |
beans |
Yes | No |
caches |
Yes | No |
conditions |
Yes | No |
configprops |
Yes | No |
env |
Yes | No |
flyway |
Yes | No |
health |
Yes | Yes |
heapdump |
N/A | No |
httptrace |
Yes | No |
info |
Yes | No |
integrationgraph |
Yes | No |
jolokia |
N/A | No |
logfile |
N/A | No |
loggers |
Yes | No |
liquibase |
Yes | No |
metrics |
Yes | No |
mappings |
Yes | No |
prometheus |
N/A | No |
quartz |
Yes | No |
scheduledtasks |
Yes | No |
sessions |
Yes | No |
shutdown |
Yes | No |
startup |
Yes | No |
threaddump |
Yes | No |
To change how a port is exposed, use the include or exclude attribute, as in:
management.endpoints.jmx.exposure.include=*
management.endpoints.web.exposure.include=health,info,prometheus
Copy the code
So far, Spring Boot Actuator is configured. In addition to the opening and exposing methods of endpoints mentioned above, it also has functions such as HTTP, JMX, logging, Metrics, permissions, HTTP tracking, and process monitoring. If you want to learn more, you can go to the official website to learn more.
2. Prometheus
Prometheus (Chinese name Prometheus) is a new-generation monitoring system, which has many functions compared with other monitoring systems, such as easy management, internal running status of monitoring services, powerful data model, powerful query language PromQL, high efficiency, scalability, easy integration, visualization, and openness. The details can be found on the official website and are not detailed here.
In Spring Boot, Prometheus ports are natively supported, and the following configuration is required to integrate Prometheus exposure to HTTP.
management:
endpoints:
web:
exposure:
include: "prometheus"
Copy the code
In addition to the above configuration, you also need to configure metrics, because without this parameter, many reports will not display properly. (No further research here, apologies…)
management:
endpoints:
web:
exposure:
include: "prometheus"
metrics:
tags:
application: ${spring.application.name}
Copy the code
This completes the configuration of the Prometheus client, and then the server, using the Docker method, first requires the configuration file prometheus.yml
scrape_configs:
# Arbitrary write, recommended English, do not contain special characters
- job_name: 'jobName'
# Collection interval time
scrape_interval: 15s
# Timeout time during collection
scrape_timeout: 10s
# Collection path
metrics_path: '/actuator/prometheus'
The address of the collection service is the address of our application
static_configs:
- targets: ['localhost:8080']
Copy the code
Docker-comemess. yml = docker-comemess. yml = docker-comemess. yml = docker-comemess. yml Volumes is – ‘. / Prometheus. Yml: / etc/Prometheus/config. Yml ‘.
version: '3.3'
services:
prometheus:
image: 'a PROM/Prometheus: v2.14.0'
ports:
- '9090:9090'
command: '--config.file=/etc/prometheus/config.yml'
volumes:
- './prometheus.yml:/etc/prometheus/config.yml'
Copy the code
Docker compose up -d: docker compose compose up -d: docker compose compose
Once started, go to http://localhost:9090 in your browser
You can then view the monitoring data for different metrics, such as JVM_MEMORY_USed_bytes
Since Prometheus already has access to Spring Boot monitoring data for various metrics, why do we need Grafana? It is ok not to integrate Grafana, but with Grafana we can easily and visually view monitoring data. The final result is as follows:
Keep reading if you’re interested.
3. Grafana
3.1 introduction
Grafana is a visual panel that displays beautiful ICONS and layouts with support for Prometheus, SQL(MySQL, PostgreSQL) and other data sources.
It has the following characteristics:
- Visualization: A great selection of components, such as charts, text, reminders, and flexible layouts to customize your visual palette.
- Data source: Support for Prometheus, Graphite, Loki, Elasticsearch, MySQL, PostgreSQL, Stackdriver, TestData DB, etc.
- Notification notification: You can visually configure notification rules. When the data reaches the threshold, the configured information is sent to the specified administrator.
- Mixed data Sources: Mix different data sources in the same diagram, specifying data sources based on each query.
3.2 installation
Install or use the docker compose way, docker – compose. Yml as follows:
version: '3.3'
services:
grafana:
image: 'grafana/grafana: 6.5.0'
ports:
- '3000:3000'
Copy the code
Docker compose can be merged with Prometheus into a Docker compose.
version: '3.3'
services:
prometheus:
image: 'a PROM/Prometheus: v2.14.0'
ports:
- '9090:9090'
command: '--config.file=/etc/prometheus/config.yml'
volumes:
- './prometheus.yml:/etc/prometheus/config.yml'
grafana:
image: 'grafana/grafana: 6.5.0'
ports:
- '3000:3000'
Copy the code
Then run the docker-compose up -d command to start up, access http://localhost:3000 in the browser, and log in using the initial account admin:admin.
3.3 configuration
3.3.1 Adding a data source
After logging in, select Add Data source and select Prometheus.
Enter the data source name (any), the URL for Prometheus, and click Add Save.
3.3.2 Creating a Dashboard
The next step is to create the dashboard. There are two options: one is to create a new dashboard with different components and layout, and the other is to choose the official grafana or community provided dashboard, and they are beautifully styled and can be imported directly. We chose to import the dashboard here, because we are a Java application and are definitely focused on JVM-related metrics, so we searched JVMS and sorted them by number of installs.
Click on the first one, this dashboard contains JVM, threads, CPU, etc. We’ll import this one, of course you can choose another dashboard or build your own.
Type 4701 in the Grafana.com Dashboard.
Select the data source and click Import.
The resulting dashboard looks like this:
4. To summarize
This article mainly starts from Spring Boot Actuator, introduces the endpoints and exposure methods of Spring Boot application monitoring, and then takes Prometheus as an example to introduce the basic concept of Prometheus and how to use it. Spring Boot Actuator + Prometheus can already use visual monitoring, but the visualization of Prometheus is still a bit rough, and Grafana was introduced. With Grafana and Prometheus, the perfect visual dashboard is available.
In addition to monitoring applications, we also use databases, so how do we monitor metrics for database instances through Grafana and Prometheus? See you in the next article.