DEV Community

Franz Wong
Franz Wong

Posted on

5 1

Monitor Java application with prometheus

Prometheus provides JMX exporter which can export JVM information.

  1. Create JMX exporter configuration. Let's call it jmx_exporter_config.yml.


---
rules:
  - pattern: "java.lang.*"


Enter fullscreen mode Exit fullscreen mode

2. Download JMX exporter

You can find the URL of JMX exporter jar file in Github repository.

https://github.com/prometheus/jmx_exporter

Here is the URL for 0.16.1

https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.16.1/jmx_prometheus_javaagent-0.16.1.jar

3. Start your Java application

For example, your application is packaged as a jar file myapp_1.jar. jmx_prometheus_javaagent-0.16.1.jar and jmx_exporter_config.yml are saved in /app. We expose port 8080 to export metrics.



java -javaagent:/app/jmx_prometheus_javaagent-0.16.1.jar=8080:/app/jmx_exporter_config.yml -jar myapp_1.jar


Enter fullscreen mode Exit fullscreen mode

4. Download Prometheus

You can find the URL of Prometheus in its official site.

https://prometheus.io/download/

Here is the URL of 2.33.0 (MacOS).

https://github.com/prometheus/prometheus/releases/download/v2.33.0/prometheus-2.33.0.darwin-amd64.tar.gz

5. Unzip Prometheus

After it is downloaded, unzip it and go to its directory.



tar xvfz prometheus-*.tar.gz
cd prometheus-*


Enter fullscreen mode Exit fullscreen mode

6. Update prometheus configuration

Add the following to the prometheus.yml (under scrape_configs). We adds label to help querying.



  - job_name: "apps"

    static_configs:
      - targets: ["localhost:8080"]
        labels:
          instance: 'myapp_1'


Enter fullscreen mode Exit fullscreen mode

The whole file should look like this.



# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]

  - job_name: "apps"

    static_configs:
      - targets: ["localhost:8080"]
        labels:
          instance: 'myapp_1'


Enter fullscreen mode Exit fullscreen mode

7. Start Prometheus



./prometheus --config.file=prometheus.yml


Enter fullscreen mode Exit fullscreen mode

8. Query JVM information

http://localhost:9090/

Query jvm_memory_bytes_used{area="heap",instance="myapp_1"}

Here is the query result.

Query result

Or you prefer a graph (You may need to refresh the browser if you want to see the latest data)

Query result in graph

Here are other metrics you can try.

  • jvm_gc_collection_seconds_count

  • jvm_gc_collection_seconds_sum

  • jvm_memory_bytes_used -

  • jvm_memory_bytes_committed

  • jvm_memory_bytes_max

  • jvm_memory_bytes_init

  • process_cpu_seconds_total

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay