Prometheus and Grafana Integration With Java Applications
In this article, we will learn how to monitor Java-based applications and solutions with Prometheus and Grafana.
Join the DZone community and get the full member experience.Join For Free
Prometheus is an open-source monitoring and alerting tool. Prometheus can aggregate or gather data from different sources. However, Prometheus has limited visualization capability.
Grafana is also an open-source project mainly used for reporting. Grafana has strong visualization capability but it can't gather data.
Hence Prometheus and Grafana are mostly used together. Prometheus for gathering data and Grafana for visualization.
- Download Prometheus, you can download the latest version. Extract it.
- Within the Prometheus installation folder, create a monitor.yml file with the following content.
- scrape_interval is set to 10 sec, which means Prometheus will scrape/refresh data/metrics in every 10 seconds.
- targets are set to localhost:8180, which means that Prometheus will gather data/metrics from localhost:8180 which we have defined later in java agent JVM property for our Red Hat Fuse which is Java-based Enterprise solution.
- Download Grafana, you can download the latest version. Extract it.
- Download jmx_prometheus_javaagent jar. Version 0.13.0 of jmx_prometheus_javaagent can be download quickly from here.
- Create a config.YAML file with the following content.
Let Us First Setup Red Hat Fuse 7.6 in Karaf Mode
2. Unzip fuse-karaf-7.6.0.zip. Once extracted modify fuse-karaf-7.6.0.fuse-760025-redhat-00001/etc/user.properties with following content.
3. Once extracted, modify fuse-karaf-7.6.0.fuse-760025-redhat-00001/bin/karaf script following way. Note we just need to add javaagent at line number 339 and 360. No need to modify existing configurations. I have set javaagent twice because of the if-else clause in karaf script, to make sure all conditions expose javaagent. Most of the time you need to set javaagent only once as JVM property.
4. Here we have provided an absolute disk path of jmx_prometheus_javaagent-0.13.0.jar and config.yaml as its option. More details of javaagent can be found in Java Documentation.
5. Start Red Hat Fuse with fuse-karaf-7.6.0.fuse-760025-redhat-00001/bin/start script.
6. After the start script waits for a few seconds. With fuse-karaf-7.6.0.fuse-760025-redhat-00001/bin/client we can access Karaf terminal. This will ensure Red Hat Fuse is up and running.
Now Start and Setup Prometheus Tool
1. Start Prometheus with Prometheus-2.19.2.linux-amd64/prometheus script.
2. Access http://localhost:9090/graph from browser. We should get the following Prometheus page.
3. In Prometheus Query page(here we can search/query metrics), select Status -> Targets. We should find here that State of service is UP and Endpoint listed is http://localhost:8180/metrics. This is the URL from where Prometheus will scrape for Red Hat Fuse metrics.
5. Prometheus data store and Retention can be configured following Prometheus Doc. Important properties are:
--storage.tsdb.path: This determines where Prometheus writes its database. Defaults to data/.
--storage.tsdb.retention.time: This determines when to remove old data. Defaults to 15d.
We can run Prometheus with these configurations as:
Now Start and Setup Grafana
1. Start Grafana from script grafana-7.1.0/bin/grafana-server.
2. Access Grafana GUI from browser with URL http://localhost:3000/. The default username/password would be admin/admin.
3. We will now add datasource to Grafana. So that Grafana can start getting data from Prometheus. Follow Step1 and Step 2 as mentioned in the screen-shot.
5. Now record details as mentioned in the following screen-shot. Finally, click on "Save & Test".
6. Now we will add a new Dashboard and create a panel in it. Check scree-shot.
7. Create a visualization in a recently added Panel.
8. If we note in screen-shot "Grafana Visualization", we have set the following. Once we set all these we will find dynamic visualization of heap memory utilization by the Red Hat Fuse process.
- (1) Metrics: java_lang_Memory_HeapMemoryUsage_used.
- (2) Last 15 minutes: Grafana will show data for the last 15 minutes.
- (3) 10s: Refresh every 10 seconds.
- (4) Unit: bytes.
9. Similarly, we can add different panels for different metrics exposed in Prometheus(http://localhost:8180/metrics). For example CPU utilization, thread-pool, Application-specific.
That's it guys, I have used Red Hat Fuse as an example here. Any Java application can be similarly configured with Prometheus, Grafana, and jmx_prometheus_javaagent. I believe it will help you to have a basic setup and then you can explore more with all the metrics available.
Opinions expressed by DZone contributors are their own.