Monitoring Java Applications Prometheus
January 9, 2019

How to Use Prometheus Monitoring With Java to Gather Data


Prometheus monitoring can be tricky. In this blog, we share how to use JMX exporters and Prometheus exporters for successful Prometheus monitoring.

What Is Prometheus Monitoring?

Prometheus is a monitoring tool. Prometheus is a powerful and popular open source time series tool and database, with a focus on storing and exposing metrics and statistics. It was originally conceived as a way to monitor containers running within Kubernetes but it has quickly evolved into a flexible, enterprise-ready monitoring solution.

How Do I Export My Data From Prometheus?

In order to take full advantage of Prometheus, and its visualization counterpart Grafana, we’ll need to be able to gather metrics that aren’t generated within a Kubernetes clustering environment.

To understand how Prometheus can gather these metrics, let’s take a quick look at Prometheus’ architecture, with respect to monitoring Java applications:

Gathering Java metrics inside of Prometheus

Note from the diagram that Prometheus can gather metrics in several different ways. Applications such as batch jobs and ephemeral workloads can push metrics into Prometheus using its Pushgateway. Prometheus can discover new metrics running inside of supported environments like Kubernetes by using its Service Discovery mechanisms.

It can also pull metrics from Prometheus exporters, which is what we’ll be doing. Alerts can be triggered via the AlertManager, which can interact with downstream systems like PagerDuty. And visualization engines like Grafana can query data out of Prometheus and display it, which are concepts we will visit in the next blog.

What Are Prometheus Exporters?

Prometheus Exporters are used to export data metrics from Prometheus. You'll need to download and install a Prometheus exporter in order to pull metrics.

There are different types of Prometheus exporters you can use. Here, we'll focus on JMX Exporter.

What Is JMX Exporter?

JMX Exporter is a Java-native specification that allows developers to expose a Java application’s metrics, statistics, and basic operations in a standard way from Prometheus.

In this blog series, we’ll use a special type of Prometheus exporter called a JMX (Java Management Extensions) Exporter. The prometheus JMX exporter exposes a Java application’s JMX objects in a way that is friendly for Prometheus to consume.

An overview of JMX can be found here.

For this post, just understand that a Java application can create JMX objects (called mBeans or Management Beans) that can store data or give access to application operations. Some of these objects are standard to the JVM, while others are specific to a particular application.

For instance, here’s a common JMX introspection tool called JConsole. This tool ships with most variants of the Java JDK. Here, it is browsing objects in a running instance of ActiveMQ:


Well-constructed Java applications will expose lots of relevant and helpful metrics, meant to be consumed by monitoring solutions like Prometheus. Most enterprise-class monitoring solutions will support gathering JMX metrics, as it is considered a standard and practical way to gather data about running Java applications. JMX can also provide useful insight into the health of the JVM, by inspecting the mBeans inside of java.lang:

JMX Exporter

So, as you can see, there’s plenty of delicious monitoring data just waiting to be consumed!

How to Bring Data Into Prometheus Monitoring

Enough theory — let’s get right into how to bring this data into Prometheus!

How to Install Prometheus Exporter and Configure the JMX Exporter 

Step 1: Download the Java JMX Exporter jar

There are two distributions available. I find that the native JMX Java Agent is the easiest to work with, but there is also a “standalone” HTTP JMX Exporter available. We’ll use the Java Agent in this post. Head here and click the “Download Jar” link:

Configuring the JMX Exporter

Step 2: Get the .jar File Accessible to Java

Place the downloaded .jar file somewhere accessible to the Java application. If you want to reuse the agent for multiple Java applications running on the same instance, you can place it in a common area. In my case, I’ve just dropped it in the bin directory of ActiveMQ, but it really doesn’t matter where you place it. For this demo (on a test machine – don’t do this with root in real life), I’ve just dropped it into its own directory called /root/prometheus-jmx:

Configuring the JMX Exporter

Step 3: Configure the config.yml file for our Application

In our case, we’ll use a very basic configuration that will expose all metrics, but, this file can be used to provide fine-grained control over which mBeans are exported, as well as configure HTTPS:

[[email protected] prometheus-jmx]# cat config.yml
startDelaySeconds: 0
ssl: false
lowercaseOutputName: false
lowercaseOutputLabelNames: false

Step 4: Save the config.yml file

Save the config.yml file somewhere convenient, I’ve just put it in the same directory as the .jar as you can see above.

Step 5: Add a JVM Parameter

Now you’ll need to add a JVM parameter to the Java application itself, requesting that it load the agent on application startup. Normally, a Java application will have a configuration file for specifying JVM parameters. In the case of ActiveMQ, that is the /bin/env file. Add the following parameter:

-javaagent:/path/to/jmx_exporter_jar/jmx_prometheus_javaagent-0.3.1.jar=[HTTP PORT]:/path/to/jmx_exporter_config/config.yml

For instance, in ActiveMQ’s env file, I’ve updated the variable that will be passed into the app as a JVM parameter:

ACTIVEMQ_OPTS="$ACTIVEMQ_OPTS_MEMORY -javaagent:/root/prometheus-jmx/jmx_prometheus_javaagent-0.3.1.jar=8080:/root/prometheus-jmx/config.yml$ACTIVEMQ_CONF/login.config"

That’s it! Fire up your Java application as normal, and verify that you can see metrics being exposed in a simple text format when you visit http://[HOSTNAME]:[HTTP_PORT]/metrics:

Configuring the JMX Exporter

How Does Prometheus Monitoring Work?

Here's how Prometheus monitoring works.

Step 1: Download and Extract Prometheus

If you haven’t already downloaded Prometheus, do so and extract it.

Step 2: Create New config file

We’ll need to create a new config file (or add new tasks to an existing one). We’ll create a file called prom-amq.yml and place it in Prometheus’s root directory (though you can place yours anywhere):

Consuming metrics with Prometheus

Here’s a sample configuration file:

  scrape_interval:     15s
    monitor: 'activemq'

  - job_name: 'activemq'
    scrape_interval: 5s
      - targets: [localhost:8080']

Step 3: Set a Global scrape_interval

 Under our global settings, we set a global scrape_interval (a global amount of time between Prometheus gathering data from this source), and we configure a label called “monitor” named “activemq”. Under “scrape_configs”, we create a job_name called “activemq”, override the global “scrape_interval” for this job to 5 seconds, and tell Prometheus to hit “[hostname]:8080” for the data, which will correspond to the hostname and port that you configured in the JMX Exporter config. 5) Launch Prometheus, passing in the configuration file as a parameter, by running the following command:

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

How to Validate Data Gathering

Step 1: Validate the State is Up

 At this point, you should have a functional Prometheus instance gathering data from the JMX Exporter. Open Prometheus in a web browser and click Status -> Targets, and validate that the State is Up:

Validate data gathering

Step 2: Query Data

Query some data and ensure that it matches what you see in the JMX Exporter Output. Click the Prometheus section to return to the main screen, and use the pull-down next to the “Execute” button to select a metric and view its contents:

Validate data gathering

What's Next?

Although Prometheus is great at grabbing and maintaining this data, its data visualization capabilities are quite limited compared to a solution like Grafana. So, read Part 2 of this blog series, where I’ll show you how to query this data from Grafana and start building sophisticated dashboards and visualization.

In the meantime, OpenLogic architects are available to assist you with this and other popular open source solutions!