To monitor a Java virtual machine (JVM) application by using Alibaba Cloud Managed Service for Prometheus, you can instrument the JVM application to expose data, use Managed Service for Prometheus to collect data, configure a Grafana dashboard to display the data, and then create an alert rule. This topic describes how to use Managed Service for Prometheus to monitor a JVM application. In this example, Container Service for Kubernetes (ACK) and Container Registry are used.
Prerequisites
- An ACK cluster is monitored by Managed Service for Prometheus. For more information, see Create a Prometheus instance to monitor an ACK cluster.
- An image repository is created in Container Registry. For more information, see Step 2: Create an image repository.
Demo
You can also use a demo project to learn how to monitor a JVM application by using Managed Service for Prometheus. For more information about the demo project, visit GitHub.
Procedure
The following figure describes how to use Managed Service for Prometheus to monitor a JVM application.
Step 1: Instrument a JVM application
To instrument a JVM application to expose data, perform the following steps:
- Add Maven dependencies to the pom.xml file.
<dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_hotspot</artifactId> <version>0.6.0</version> </dependency>
- Add the method to initialize the JVM exporter to the application code.
@PostConstruct public void initJvmExporter() { io.prometheus.client.hotspot.DefaultExports.initialize(); }
For more information, see the /src/main/java/com/monitise/prometheus_demo/DemoController.java file of the demo project.
- Configure the port and endpoint that are used to connect to Managed Service for Prometheus in the application.properties file.
management.port: 8081 endpoints.prometheus.path: prometheus-metrics
For more information, see the /src/main/resources/application.properties file of the demo project.
- Add the method to enable the HTTP port to the application code.
@SpringBootApplication // sets up the prometheus endpoint /prometheus-metrics @EnablePrometheusEndpoint // exports the data at /metrics at a prometheus endpoint @EnableSpringBootMetricsCollector public class PrometheusDemoApplication { public static void main(String[] args) { SpringApplication.run(PrometheusDemoApplication.class, args); } }
For more information, see the /src/main/java/com/monitise/prometheus_demo/PrometheusDemoApplication.java file of the demo project.
Step 2: Upload the JVM application
To build an image for the instrumented JVM application and upload the image to an image repository of Container Registry, perform the following steps:
- Run the following command to recompile a module:
mvn clean install -DskipTests
- Run the following command to build an image:
docker build -t <Name of the local temporary Docker image>:<Version number of the local temporary Docker image> . --no-cache
Example:docker build -t promethues-demo:v0 . --no-cache
- Run the following command to tag the image:
sudo docker tag <Name of the local temporary Docker image>:<Version number of the local temporary Docker image> <Domain name of the image repository>/<Namespace>/<Image name>:<Image version number>
Example:sudo docker tag promethues-demo:v0 registry.cn-hangzhou.aliyuncs.com/testnamespace/promethues-demo:v0
- Run the following command to upload the image to the image repository:
sudo docker push <Domain name of the image repository>/<Namespace>/<Image name>:<Image version number>
Example:sudo docker push registry.cn-hangzhou.aliyuncs.com/testnamespace/promethues-demo:v0
You can view the information about the uploaded application image on the Tags page of the Container Registry console.
Step 3: Deploy the JVM application
To deploy the JVM application to an ACK cluster, perform the following steps:
- Log on to the ACK console.
- In the left-side navigation pane, click Clusters.
- On the Clusters page, find the cluster to which you want to deploy the application and click Applications in the Actions column.
- Create a container group. The newly created container group is displayed on the Deployments page.
- Create a Service. The newly created Service is displayed on the Services page.
Step 4: Configure a service discovery task
Configure a service discovery task on the ServiceMonitor tab in Managed Service for Prometheus to collect data from the JVM application. For more information, see Use ServiceMonitors to discover and monitor Services.