{{announcement.body}}
{{announcement.title}}

Prometheus and Grafana Integration With Java Applications

DZone 's Guide to

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.

· Integration Zone ·
Free Resource

In this article, we will learn how to monitor Java-based applications and solutions with Prometheus and Grafana.

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.

Prerequisite:

  • 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.
Plain Text
 




x
10


 
1
global:
2
  scrape_interval:     10s
3
  external_labels:
4
    monitor: 'fuse-karaf'
5
 
          
6
scrape_configs:
7
  - job_name: 'fuse-karaf'
8
    scrape_interval: 10s
9
    static_configs:
10
      - targets: [localhost:8180]



  • 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.
Plain Text
 




xxxxxxxxxx
1


 
1
startDelaySeconds: 0
2
ssl: false
3
lowercaseOutputName: false
4
lowercaseOutputLabelNames: false
5
 
          



Let Us First Setup Red Hat Fuse 7.6 in Karaf Mode

1. Download Red Hat Fuse 7.6 Karaf Installer. It can be downloaded quickly from Red Hat Developer Forum.

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.

Plain Text
 




xxxxxxxxxx
1


 
1
admin = admin,_g_:admingroup
2
_g_\:admingroup = group,admin,manager,viewer,systembundles,ssh
3
 
          



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.

Shell
 




xxxxxxxxxx
1
10


 
1
338 -Dkaraf.restart.jvm.supported=true \
2
339 -javaagent:/home/cpandey/Desktop/prometheus-grafana/jmx_prometheus_javaagent-0.13.0.jar=8180:/home/cpandey/Desktop/prometheus-grafana/config.yaml \
3
-----
4
347 else
5
348 ${KARAF_EXEC} "${JAVA}" ${JAVA_OPTS} \
6
349 -Djava.endorsed.dirs="${JAVA_ENDORSED_DIRS}" \
7
-----
8
360 -javaagent:/home/cpandey/Desktop/prometheus-grafana/jmx_prometheus_javaagent-0.13.0.jar=8180:/home/cpandey/Desktop/prometheus-grafana/config.yaml \
9
${KARAF_SYSTEM_OPTS} \
10
-----



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.

Shell
 




xxxxxxxxxx
1
22


 
1
[cpandey@cpandey bin]$ ./start
2
[cpandey@cpandey bin]$ ./client -u admin -p admin
3
client: JAVA_HOME not set; results may vary
4
Logging in as admin
5
 
          
6
 ____          _   _   _       _     _____                
7
|  _ \ ___  __| | | | | | __ _| |_  |  ___|   _ ___  ___  
8
| |_) / _ \/ _` | | |_| |/ _` | __| | |_ | | | / __|/ _ \ 
9
|  _ <  __/ (_| | |  _  | (_| | |_  |  _|| |_| \__ \  __/ 
10
|_| \_\___|\__,_| |_| |_|\__,_|\__| |_|   \__,_|___/___| 
11
 
          
12
  Red Hat Fuse (7.6.0.fuse-760025-redhat-00001)
13
  http://www.redhat.com/products/jbossenterprisemiddleware/fuse/
14
 
          
15
Hit '<tab>' for a list of available commands
16
and '[cmd] --help' for help on a specific command.
17
 
          
18
Open a browser to http://localhost:8181/hawtio to access the management console
19
 
          
20
Hit '<ctrl-d>' or 'shutdown' to shutdown Red Hat Fuse.
21
 
          
22
 
          



Now Start and Setup Prometheus Tool

1. Start Prometheus with Prometheus-2.19.2.linux-amd64/prometheus script.

Shell
 




xxxxxxxxxx
1
10


 
1
[cpandey@cpandey prometheus-2.19.2.linux-amd64]$ ./prometheus --config.file=monitor.yml
2
level=info ts=2020-07-24T20:24:57.368Z caller=main.go:302 msg="No time or size retention was set so using the default time retention" duration=15d
3
level=info ts=2020-07-24T20:24:57.368Z caller=main.go:337 msg="Starting Prometheus" version="(version=2.19.2, branch=HEAD, revision=c448ada63d83002e9c1d2c9f84e09f55a61f0ff7)"
4
level=info ts=2020-07-24T20:24:57.368Z caller=main.go:338 build_context="(go=go1.14.4, user=root@dd72efe1549d, date=20200626-09:02:20)"
5
level=info ts=2020-07-24T20:24:57.368Z caller=main.go:339 host_details="(Linux 3.10.0-514.16.1.el7.x86_64 #1 SMP Fri Mar 10 13:12:32 EST 2017 x86_64 cpandey.pnq.csb (none))"
6
level=info ts=2020-07-24T20:24:57.368Z caller=main.go:340 fd_limits="(soft=50000, hard=50000)"
7
level=info ts=2020-07-24T20:24:57.368Z caller=main.go:341 vm_limits="(soft=unlimited, hard=unlimited)"
8
level=info ts=2020-07-24T20:24:57.370Z caller=main.go:678 msg="Starting TSDB ..."
9
level=info ts=2020-07-24T20:24:57.370Z caller=web.go:524 component=web msg="Start listening for connections" address=0.0.0.0:9090
10
 
          



2. Access http://localhost:9090/graph from browser. We should get the following Prometheus page.

prometheus

Prometheus Query

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.

targets

Prometheus Targets
4. Now if we browse http://localhost:8180/metrics URL, we will get a list of all metrics. We have highlighted java_lang_Memory_HeapMemoryUsage_used because we will be using these metrics in Grafana Panel.

prometheus metrics
Prometheus 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:

Plain Text
 




x


 
1
./prometheus --config.file=mbean-monitor.yml --storage.tsdb.path=/path/to/data-store



Now Start and Setup Grafana

1. Start Grafana from script grafana-7.1.0/bin/grafana-server.

Shell
 




xxxxxxxxxx
1
15


 
1
[cpandey@cpandey bin]$ ./grafana-server 
2
INFO[07-25|02:20:36] Starting Grafana                         logger=server version=7.1.0 commit=8101355285 branch=HEAD compiled=2020-07-16T16:34:17+0530
3
INFO[07-25|02:20:36] Config loaded from                       logger=settings file=/home/cpandey/Desktop/prometheus-grafana/grafana-7.1.0/conf/defaults.ini
4
INFO[07-25|02:20:36] Path Home                                logger=settings path=/home/cpandey/Desktop/prometheus-grafana/grafana-7.1.0
5
INFO[07-25|02:20:36] Path Data                                logger=settings path=/home/cpandey/Desktop/prometheus-grafana/grafana-7.1.0/data
6
INFO[07-25|02:20:36] Path Logs                                logger=settings path=/home/cpandey/Desktop/prometheus-grafana/grafana-7.1.0/data/log
7
INFO[07-25|02:20:36] Path Plugins                             logger=settings path=/home/cpandey/Desktop/prometheus-grafana/grafana-7.1.0/data/plugins
8
INFO[07-25|02:20:36] Path Provisioning                        logger=settings path=/home/cpandey/Desktop/prometheus-grafana/grafana-7.1.0/conf/provisioning
9
INFO[07-25|02:20:36] App mode production                      logger=settings
10
INFO[07-25|02:20:36] Connecting to DB                         logger=sqlstore dbtype=sqlite3
11
INFO[07-25|02:20:36] Starting DB migration                    logger=migrator
12
INFO[07-25|02:20:36] Starting plugin search                   logger=plugins
13
INFO[07-25|02:20:36] Registering plugin                       logger=plugins name="Direct Input"
14
INFO[07-25|02:20:36] HTTP Server Listen                       logger=http.server address=[::]:3000 protocol=http subUrl= socket=
15
 
          



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.

Grafana Add DataSource

Grafana Add DataSource

4. Select "Prometheus" as datasource.

5. Now record details as mentioned in the following screen-shot. Finally, click on "Save & Test".

Grafana Datasource Settings

6. Now we will add a new Dashboard and create a panel in it. Check scree-shot.

Grafana Dashboard and Panel

7. Create a visualization in a recently added Panel.

Grafana Visualization

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.






Topics:
artemis, broker, grafana, integration, java, prometheus

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}