All Products
Search
Document Center

Managed Service for Prometheus:Monitor the Java applications in ECS instances in VPC networks

Last Updated:Apr 29, 2024

This topic describes how to use Application Real-Time Monitoring Service (ARMS) Managed Service for Prometheus to monitor Java applications in an Elastic Compute Service (ECS) instance that is deployed in a VPC network.

Prerequisites

  • A Java application is created in your ECS instance.

    Spring Boot is one of the most popular Java web frameworks and provides a comprehensive infrastructure support for developing Java applications. You can connect Spring Boot Actuator and Micrometer to ARMS Prometheus, and use it to monitor applications. This topic takes Java applications that are developed with Spring Boot as an example. If you have not created a Java project, you can create a Maven project by using a scaffold tool for Java.

  • An ECS instance that is deployed in a VPC network is monitored by Managed Service for Prometheus. For more information, see Create a Prometheus instance to monitor an ECS instance.
Note The parameter values in this topic are provided for reference. You can modify them based on your business requirements.

Procedure

  1. Add the following dependency to the pom.xml file in the Maven project that you created:
    <dependency>
        <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
       <version>2.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>io.micrometer</groupId>
      <artifactId>micrometer-registry-prometheus</artifactId>
      <version>1.5.1</version>
    </dependency>
    Note
    • Spring Boot Actuator provides a variety of production-ready features, such as application monitoring, metadata collection, and database status query. For more information, see Production-ready Features.
    • Micrometer is an application metrics facade that supports numerous monitoring systems, including Prometheus. Micrometer plays a role in instrumenting application code as the role Simple Logging Facade for Java (SLF4J) plays in log frameworks.
  2. Modify the Spring Boot configuration file
    • If your Spring Boot configuration file is in the application.properties format, run the following command to modify related configurations:
      # Specify the application name.
      spring.application.name=demo
      
      # Specify the application server port.
      server.port=8080
      
      # Specify the endpoints.
      #management.endpoints.enabled-by-default=true
      #management.endpoints.web.base-path=/actuator
      
      # Set the Prometheus endpoint that the actuator will expose, which is /actuator/prometheus.
      management.endpoints.web.exposure.include=prometheus
      # Add the application label to the exposed Prometheus metrics.
      management.metrics.tags.application=demo
    • If your Spring Boot configuration file is in the application.yml format, run the following command to modify related configurations:
      server:
        port: 8080
      spring:
        application:
          name: spring-demo
      management:
        endpoints:
          web:
            exposure:
              include: 'prometheus' # Expose /actuator/prometheus
        metrics:
          tags:
            application: demo
  3. Check whether the Spring Boot configuration takes effect.
    Start the application as normal and enter the following URL in the browser:
    http://localhost:8080/actuator/prometheus
    Note
    • Replace the localhost and port in the command based on your business requirements.
    • We recommend that you try to access http:// [ECS IP]:port /actuator/prometheus from other ECS instances to check whether network connectivity is blocked by a security group rule.

    If the Spring Boot configuration takes effect, the following command output is returned:

    # 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{application="demo",area="heap",id="G1 Eden Space",} 1.30023424E8
    jvm_memory_committed_bytes{application="demo",area="heap",id="G1 Old Gen",} 1.28974848E8
    jvm_memory_committed_bytes{application="demo",area="nonheap",id="Metaspace",} 4.9627136E7
    jvm_memory_committed_bytes{application="demo",area="heap",id="G1 Survivor Space",} 9437184.0
    jvm_memory_committed_bytes{application="demo",area="nonheap",id="CodeHeap 'non-profiled nmethods'",} 7077888.0
    jvm_memory_committed_bytes{application="demo",area="nonheap",id="Compressed Class Space",} 6680576.0
    jvm_memory_committed_bytes{application="demo",area="nonheap",id="CodeHeap 'non-nmethods'",} 2555904.0
    # HELP jvm_threads_states_threads The current number of threads having NEW state
    # TYPE jvm_threads_states_threads gauge
    jvm_threads_states_threads{application="demo",state="waiting",} 11.0
    jvm_threads_states_threads{application="demo",state="blocked",} 0.0
    jvm_threads_states_threads{application="demo",state="timed-waiting",} 7.0
    jvm_threads_states_threads{application="demo",state="runnable",} 14.0
    jvm_threads_states_threads{application="demo",state="new",} 0.0
    jvm_threads_states_threads{application="demo",state="terminated",} 0.0
  4. Create a service discovery.
    1. Log on to the Managed Service for Prometheus console.

    2. In the left-side navigation pane, click Instances.

    3. Click the name of the Prometheus instance.
    4. In the left-side navigation pane, click Service Discovery. Then, click the Configurations tab.
    5. On the Configurations tab, you can create a service discovery by using one of the following methods:

      Method 1: Modify a default service discovery

      1. On the Default Service Discovery tab, find vpc-ecs-service-discovery and click Details.
      2. In the dialog box that appears, enter the following information and click OK.
        • Change the default port 8888 to a port based on your business requirements. Example: port 8080.
        • Change the default value of the metrics_path field to a value based on your business requirements. Example: /actuator/prometheus.

        The metric data of all ECS instances at the /actuator/prometheus endpoint in the current VPC network is collected over port 8080.

        If you want to collect the metrics of only specific ECS instances, you can configure tag_filters to filter ECS instances by tag.
        # Specify the key-value pair   
           tag_filters: 
               - key: 'testKey'
                 values: ['testValue']
        Then, you need to add a tag to the ECS instance in the ECS console. For more information, see Modify the tags of an instance.

      Method 2: Create a custom service discovery

      1. On the Custom Service Discovery tab, click Add in the upper-right corner.
      2. In the dialog box that appears, enter the configurations and click OK.
  5. Create a Grafana dashboard
    1. In the left-side navigation pane, click Dashboards.
    2. In the upper-right corner of the Dashboards page, click Create Dashboard in Grafana Service Pro Edition.
    3. In the left-side navigation pane, choose + > Import.
    4. On the Import page, enter 4701 (the ID of the JVM Grafana template provided by Prometheus) in the Import via grafna.com field and click Load next to the field.
      Note To obtain other Grafana dashboard templates, visit Grafana Labs.
    5. In the Prometheus drop-down list, select a data source in your VPC network, and then click Import.

      The name of the data source in the VPC network is in the vpc-**** format.

      After the data source is imported, you can view the metrics of the data source on the Grafana dashboard. Grafana dashboard

Other operations

To monitor the number of requests handled by an API in your Java application, perform the following operations:

  1. Create a controller with the following Java code:
    @RestController
    @RequestMapping("/v1")
    public class IndexController {
    
        @Autowired
        MeterRegistry registry;
    
        private Counter counter_index;
    
        @PostConstruct
        private void init(){
            counter_index = registry.counter("demo_app_requests_method_count", "method", "IndexController.index");
        }
    
        @RequestMapping(value = "/index")
        public Object index(){
            try{
                counter_index.increment();
            } catch (Exception e) {
                return e;
            }
            return counter_index.count();
        }
     
    }
  2. Run the following command to test whether the Java code runs normally:

    If the URL is accessible, the Java code runs as expected.

    curl http://127.0.0.1:8080/v1/index
  3. Go to the Grafana dashboard created in Step 5.
  4. Click the Add a new panel in the Grafana dashboard icon in the upper-right corner of the page, and then click +Add new panel.
  5. On the Edit Panel page, select an ECS instance in a VPC network as the data source from the drop-down list on the Query tab. In the A collapsible section, enter the following PromQL statement in the text box on the right of the Metrics drop-down list.
    demo_app_requests_method_count_total{application="demo",method="IndexController.index"}
  6. On the right-side Panel tab, enter the name of the panel. Example: index count.
  7. Click Save in the upper-right corner. In the dialog box that appears, click Save.
    After the configuration takes effect, you can view the number of API requests in the index count panel in the Grafana dashboard. New panel