Running jmx2graphite as a java agent to push the JMX metrics into Graphite

Reading Time: 2 minutes

In my previous blog, we discussed how to monitor a Kafka stream application using Grafana and Graphite. In this solution, we used jmx2graphite as a metrics exporter which takes the metrics from the Jolokia URL where Jolokia exposes the JMX metrics and pushes those metrics to Graphite.

But, there is a problem with this solution that we need to deploy one jmx2graphite per service. So for example, consider the following situations:

  • Having 3 instances of the same service
  • or a single instance of 3 difference services

In both the above scenarios, we need to deploy 3 jmx2graphite instances. So if we will be having 10 services, each having 2 instances then we would need to deploy a total of 20 jmx2graphite. This will create an overhead to the deployment and the maintenance will be very complex.

So, we looked for some other ways to find out the optimal approach as my use case was to monitor multiple applications and I was not ready to do so much amount of deployment.

Earlier, we were running jmx2graohite as a docker instance but now we have got a new way of running jmx2graphite which is as a java agent in a new release. In this approach, the deployment overhead will be removed as it will be run as a java agent inside the service container only.

Run jmx2graphite as java agent:

  1. First of all, need to download the jmx2graphte java agent jar.
  2. Then, need to add the following arguments to the java command: -javaagent:/jmx2graphite-1.2.5-javaagent.jar
  3. Need to provide some environment variables which are as follows:
  • SERVICE_NAME: The name of the service.
  • GRAPHITE_HOST: The hostname/IP of graphite
  • GRAPHITE_PORT:  Protocol port of graphite. Defaults to 2004.
  • SERVICE_HOST:  The host on which service is running

The full command:

exec java -cp “$APP_LIB_DIR/*” -javaagent:/jmx2graphite-jvm-1.2.5-javaagent.jar=GRAPHITE_HOSTNAME=$GRAPHITE_HOST,GRAPHITE_PORT=$GRAPHITE_PORT,SERVICE_HOST=$SERVICE_HOST,SERVICE_NAME=$SERVICE_NAME com.knoldus.monitoring.StreamsExecutor

docker run -d –name monitoring_app –network=host -e GRAPHITE_HOST= -e GRAPHITE_PORT=2004 -e SERVICE_HOST=local -e SERVICE_NAME=monitoring-app stream-app:latest

I would like to give a high-level diagram of 2 different scenarios here:

Multiple instances of one service:


Multiple instances of different services:


Hope, it will help you to use jmx2graphite to monitor your application.

Happy reading !!!



Written by 

Rishi is a tech enthusiast with having around 10 years of experience who loves to solve complex problems with pure quality. He is a functional programmer and loves to learn new trending technologies. His leadership skill is well prooven and has delivered multiple distributed applications with high scalability and availability by keeping the Reactive principles in mind. He is well versed with Scala, Akka, Akka HTTP, Akka Streams, Java8, Reactive principles, Microservice architecture, Async programming, functional programming, distributed systems, AWS, docker.