This topic describes how to use Managed Service for Prometheus to monitor a Java application that uses Micrometer.
Prerequisites
You have created a Java application.
Step 1: Instrument your Java application
This topic uses a popular Spring Boot 2.x application as an example.
-
To add Spring Boot Actuator to a Java application, add the Actuator dependency to the
pom.xmlfile of the project.<dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <version>x.y.z</version> </dependency>NoteYou can find the latest version in the Alibaba Cloud repository.
-
Add the following properties to the
application.propertiesfile to expose the port and endpoint for Prometheus monitoring data.management.server.port=8080 management.endpoints.web.exposure.include=xxx, prometheus Add the MicroMeter dependency to the project's
pom.xmlfile.<dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <version>x.y.z</version> </dependency>NoteFind the latest version in the Alibaba Cloud repository.
-
Add the Micrometer JVM Extras dependency to your project's
pom.xmlfile.<dependency> <groupId>io.github.mweirauch</groupId> <artifactId>micrometer-jvm-extras</artifactId> <version>0.2.2</version> </dependency>NoteYou can find the latest version in the Alibaba Cloud repository.
-
Configure Micrometer JVM Extras by defining the following two Spring beans to expose memory and thread-related metrics.
/* With Spring */ @Bean public MeterBinder processMemoryMetrics() { return new ProcessMemoryMetrics(); } @Bean public MeterBinder processThreadMetrics() { return new ProcessThreadMetrics(); } -
Deploy the Java application to Container Service or an ECS instance.
Deploy your Java application to Container Service or an ECS instance based on your business scenario. After deploying the application, verify that metrics are exposed correctly by accessing the application's listening address at
http://localhost:8080/actuator/prometheus. The port8080is specified in yourapplication.propertiesfile. If you use a different port, update the URL accordingly.A successful request returns Prometheus-formatted metric data, similar to the following example:
# HELP jvm_memory_used_bytes The amount of used memory # TYPE jvm_memory_used_bytes gauge jvm_memory_used_bytes{area="heap",id="G1 Survivor Space",} 2964360.0 jvm_memory_used_bytes{area="heap",id="G1 Old Gen",} 1.285324E7 jvm_memory_used_bytes{area="nonheap",id="Metaspace",} 3.6128128E7 jvm_memory_used_bytes{area="nonheap",id="CodeCache",} 9673088.0 jvm_memory_used_bytes{area="heap",id="G1 Eden Space",} 6.7108864E7 jvm_memory_used_bytes{area="nonheap",id="Compressed Class Space",} 4999168.0 # HELP jvm_threads_live_threads The current number of live threads including both daemon and non-daemon threads # TYPE jvm_threads_live_threads gauge jvm_threads_live_threads 28.0 # HELP jvm_memory_committed_bytes The amount of memory in bytes that is committed for the Java virtual machine to use # TYPE jvm_memory_committed_bytes gauge jvm_memory_committed_bytes{area="heap",id="G1 Survivor Space",} 4194304.0 jvm_memory_committed_bytes{area="heap",id="G1 Old Gen",} 5.8720256E7 jvm_memory_committed_bytes{area="nonheap",id="Metaspace",} 3.6569088E7 jvm_memory_committed_bytes{area="nonheap",id="CodeCache",} 1.0682368E7 jvm_memory_committed_bytes{area="heap",id="G1 Eden Space",} 9.646899E7 jvm_memory_committed_bytes{area="nonheap",id="Compressed Class Space",} 5177344.0 # HELP executor_pool_max_threads The maximum allowed number of threads in the pool # TYPE executor_pool_max_threads gauge executor_pool_max_threads{name="applicationTaskExecutor",} 2.147483647E9 ... # TYPE jvm_buffer_count_buffers gauge jvm_buffer_count_buffers{id="mapped - 'non-volatile memory'",} 0.0 jvm_buffer_count_buffers{id="mapped",} 0.0 jvm_buffer_count_buffers{id="direct",} 7.0 # HELP jvm_buffer_total_capacity_bytes An estimate of the total capacity of the buffers in this pool # TYPE jvm_buffer_total_capacity_bytes gauge jvm_buffer_total_capacity_bytes{id="mapped - 'non-volatile memory'",} 0.0 jvm_buffer_total_capacity_bytes{id="mapped",} 0.0 jvm_buffer_total_capacity_bytes{id="direct",} 57344.0 # HELP jvm_threads_peak_threads The peak live thread count since the Java virtual machine started or peak was reset # TYPE jvm_threads_peak_threads gauge jvm_threads_peak_threads 30.0 # HELP jvm_threads_states_threads The current number of threads # TYPE jvm_threads_states_threads gauge jvm_threads_states_threads{state="runnable",} 11.0 jvm_threads_states_threads{state="blocked",} 0.0
Step 2: Integrate Micrometer
-
In the left-side navigation pane, choose Integration Center. Then, search for and click Micrometer.
-
On the Configuration tab of the Micrometer panel, select the Environment Type, configure the required settings, and then click OK.
Parameter
Description
Select environment type
The environment where your application is deployed.
-
Container Service environment
-
ECS (VPC)
Select cluster
The cluster where your application is deployed.
Pod label
A unique pod label to identify your Micrometer service.
Service port
The service port of the Java application. The default value is entered automatically.
Metrics path
The path where Micrometer exposes metrics. The default path is entered automatically.
Scrape over HTTPS
Enable this option to scrape metrics over HTTPS. By default, metrics are scraped over HTTP.
Metric scrape interval (seconds)
The interval at which Managed Service for Prometheus scrapes metric data. The default value is 30 seconds.
-
Step 3: View the Micrometer dashboard
-
In the left-side navigation pane, click Integration Center. On the Integration Center page, on the Installed tab, select the target environment and click its name to view the container environment details page.
-
On the Components tab, in the Component Type section, click Micrometer, and then click the Dashboards tab to view the names of all dashboards.
The dashboard list displays the JVM Micrometer dashboard, tagged with
java,jvm, andmicrometer. -
Click the name of a dashboard to view the corresponding Grafana dashboard.
The JVM Micrometer dashboard is displayed. It includes drop-down filters for job and Instance, a Restart Detection switch, a Quick Facts section (Uptime and Start time), and an I/O Overview section (Rate and Errors). Select a target instance to view its JVM monitoring data.
Step 4: Configure alerts for Micrometer
-
On the Components tab, in the Component Type section, click Micrometer, and then click the Alert tab to view the default alert rules.
The default alert rules include JVM CPU Usage Alert, JVM Memory Usage Alert, JVM Restart Alert, Logback Error Count Alert, and Major GC Time Ratio Alert. All rules are active and have P2 priority.
-
You can also add custom alert rules. For more information about how to create a Prometheus alert rule, see Create alert rules for a Prometheus instance.