The original address

The scripts mentioned in this article can be downloaded here.

Abstract

Tools used:

  • Docker. This paper makes extensive use of Docker to start various applications.
  • Prometheus, which is responsible for fetching/storing metric information and providing query function.
  • Grafana, for data visualization.
  • JMX Exporter, providing JMX and JVM-related metrics.
  • Tomcat, used to simulate a Java application.

Here are the general steps:

  1. Using JMX Exporter, start a small HTTP Server inside a Java process
  2. Configure ProMetheus to grab the metrics provided by that HTTP Server.
  3. Configure Grafana to connect to Prometheus, configure Dashboard.

Step 1: Launch a few Java applications

1) Create a new directory called PROM-JVM-DEMO.

2) Download JMX Exporter to this directory

3) Create a new file simple-config.yml with the following contents:

--- lowercaseOutputLabelNames: true lowercaseOutputName: true whitelistObjectNames: ["java.lang:type=OperatingSystem"] rules: - pattern: 'java.lang<type=OperatingSystem><>((? ! process_cpu_time)\w+):' name: os_$1 type: GAUGE attrNameSnakeCase: true

4) Run the following command to start the three tomcats, and remember to replace with the correct path:

docker run -d \ --name tomcat-1 \ -v <path-to-prom-jvm-demo>:/jmx-exporter \ -e CATALINA_OPTS="-Xms64m -Xmx128m -javaAgent :/ JMX-Exporter/jmx_prometheus_javaAgent -0.3.1.jar=6060:/ JMX-Exporter /simple-config.yml" \ -p 6060:6060 \ -p 8080:8080 \ Tomcat: 8.5-Alpine Docker run-d \ --name Tomcat -2 \ -v <path-to-prom-jvm-demo>:/ JMX-Exporter \ -e CATALINA_OPTS="-Xms64m -Xmx128m -javaAgent :/ JMX-Exporter/jmx_prometheus_javaAgent -0.3.1.jar=6060:/ JMX-Exporter /simple-config.yml" \ -p 6061:6060 \ -p 8081:8080 \ Tomcat: 8.5-Alpine docker run-d \ --name tomcat-3 \ -v <path-to-prom-jvm-demo>:/ JMX-Exporter \ -e CATALINA_OPTS="-Xms64m -Xmx128m -javaAgent :/ JMX-Exporter/jmx_prometheus_javaAgent -0.3.1.jar=6060:/ JMX-Exporter /simple-config.yml" \ -p 6062:6060 \ -p 8.5 alpine \ tomcat: 8082:8080

5) to http://localhost:8080 | 8081 | 8082 see if Tomcat startup success.

6) to access the corresponding http://localhost:6060 | 6061 | 6062 look at JMX exporter provide metrics.

Note: The simple-config.yml provided here only provides information about the JVM; refer to the JMX Exporter documentation for more complex configurations.

Step 2: Launch Prometheus

1) Create a PROM-JMX.YML file in PROM-JMX.YML with the following contents:

scrape_configs:
  - job_name: 'java'
    scrape_interval: 30s
    static_configs:
    - targets:
      - '<host-ip>:6060'
      - '<host-ip>:6061'
      - '<host-ip>:6062'

2) Start Prometheus:

docker run -d \
  --name=prometheus \
  -p 9090:9090 \
  -v <path-to-prom-jvm-demo>:/prometheus-config \
  prom/prometheus --config.file=/prometheus-config/prom-jmx.yml

3) Visit http://localhost:9090 to see if Prometheus started successfully. Enter JVM_INFO in the input box and execute it. You should see the following result:

If you don’t see three instances, wait a while and try again.

Step 3: Configure Grafana

1) Launch Grafana:

docker run -d --name=grafana -p 3000:3000 grafana/grafana

2) Go to http://localhost:3000 and log in using admin/admin.

3) Add Prometheus Data Source, as shown in the figure below, to the Add Data Source page:

4) Configure data source information:

  • Name: Whatever you want
  • Type: Prometheus
  • URL:http://<host-ip>:9090
  • Unset the rest, clickSave & Test, should return a successful result

5) Import Dashboard. We don’t need to make Dashboard all over again, just use the existing one and enter the import page as shown in the image below

6) Using the JVM Dashboard I made, the ID number appearing on the right side of the page is 8563. Remember this number and fill it in as shown below:

7) Then click elsewhere with the mouse and wait for a moment. The figure below will appear and you can select the data source

8) Finally, open the Dashboard just imported, as shown below: