Cloud Computing, DevOps

4 Mins Read

Visualizing Java Metrics in Grafana Using Prometheus JMX Exporter

Voiced by Amazon Polly

Introduction

Java applications expose various performance and operational metrics through Java Management Extensions (JMX), such as memory usage, garbage collection statistics, thread count, etc. However, JMX alone isn’t ideal for the modern observability of pipelines that use Prometheus and Grafana.

This blog explores how to bridge the gap between Java-based applications and the Prometheus-Grafana ecosystem by integrating JMX metrics using the Prometheus JMX Exporter. With this setup, you can monitor your Java application’s performance, visualize trends, and detect real-time anomalies.

Pioneers in Cloud Consulting & Migration Services

  • Reduced infrastructural costs
  • Accelerated application deployment
Get Started

Key Components and Parameters

  1. JMX Exporter
  • A Java Agent or HTTP Server that exposes JMX metrics in Prometheus format.
  • Usage Modes:
    • Java Agent Mode: Attach it to your application at startup using the -javaagent JVM argument.
    • Standalone HTTP Server Mode: Run the exporter as a separate process that scrapes metrics from a remote JVM.
  1. JMX Exporter Configuration

There should be a jmx config file in the application source code repository, and the content of the file as below:

jmx-config.yaml

Note: The pattern “.*” means “match all MBeans” – all available metrics exposed by the JVM will be made available to Prometheus.

Below is the Docker file, which includes the JMX config file copied to the Docker container and exposed on port 9404.

Docker file snippet related to JMX

Integrating the Java Agent in the deployment/Rollouts manifest file

Once the image is built successfully with the above JMX configurations in the source code, you can update your cmd/args section on the deployments/Rollouts of the applications.

To use the exporter, modify your JVM startup command as follows:

This tells the JVM to:

  • Load the agent from the specified JAR path
  • Expose metrics on port 9404,
  • Use the specified YAML config file to filter and structure metrics.

Prometheus Configuration

jmx

The above image is described below with detailed information.

  • Job: jmx-metrics – A dedicated job for scraping metrics exposed by the JMX Exporter.
  • metrics_path: /metrics – Specifies the HTTP path where the JMX Exporter exposes the metrics.
  • kubernetes_sd_configs with role: endpoints – Uses Kubernetes service discovery to find endpoints (typically services or pods) exposing metrics.

Relabeling Configuration:

  • First Rule:
    • Extracts the value of the Kubernetes service label app and assigns it to the app label in Prometheus.
  • Second Rule:
    • Maps the kubernetes.io/name pod label to a new Prometheus label called app_name.
  • Third Rule:
    • Filters target by checking if the container port name is jmxexporter. Only targets with this port name will be scrapped.
    • action: keep – Ensures only matching targets are retained.

And on the service of the application, make sure the label consists with
app: jmx-enabled

jmx2

jmx3

and the port section is exposed on the right port, and the name is jmxexporter as we have mapped the label and name of the port.

jmx4

Visualizing with Grafana

Once the data is retrieved, choose the right dashboard for your Grafana for the JMX integration and observe the heap metrics and other details.

Benefits of JMX Integration with Prometheus and Grafana

  • Real-Time JVM Monitoring
    In real-time, gain visibility into memory usage, garbage collection, thread counts, and other JVM-level metrics.
  • Custom Application Metrics
    Expose custom MBeans from your Java application for specific business or operational insights.
  • Powerful Visualization with Grafana
    Use Grafana’s rich dashboarding capabilities to visualize JVM metrics with historical trends, thresholds, and alerts.
  • Early Detection of Performance Issues
    Identify memory leaks, thread deadlocks, or CPU spikes early through continuous metric scraping and alerting.
  • Centralized Monitoring Across Services
    Prometheus enables you to monitor multiple Java services consistently from a single source.
  • Lightweight & Non-Intrusive
    The JMX Exporter runs as a Java Agent and doesn’t require modifying the application codebase.
  • Alerting and Automation
    Set up alerts for critical thresholds (e.g., high GC time, heap usage) to proactively handle application issues.

Conclusion

Integrating JMX with Prometheus and Grafana enables real-time, visual monitoring of Java applications. Developers and DevOps teams gain powerful insights into application performance, system health, and resource usage using the Prometheus JMX Exporter.

This setup helps in proactive alerting and troubleshooting, ultimately improving application reliability and performance.

Drop a query if you have any questions regarding JMX, Prometheus, Grafana and we will get back to you quickly.

Empowering organizations to become ‘data driven’ enterprises with our Cloud experts.

  • Reduced infrastructure costs
  • Timely data-driven decisions
Get Started

About CloudThat

CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.

CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 850k+ professionals in 600+ cloud certifications and completed 500+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training PartnerAWS Migration PartnerAWS Data and Analytics PartnerAWS DevOps Competency PartnerAWS GenAI Competency PartnerAmazon QuickSight Service Delivery PartnerAmazon EKS Service Delivery Partner AWS Microsoft Workload PartnersAmazon EC2 Service Delivery PartnerAmazon ECS Service Delivery PartnerAWS Glue Service Delivery PartnerAmazon Redshift Service Delivery PartnerAWS Control Tower Service Delivery PartnerAWS WAF Service Delivery PartnerAmazon CloudFront Service Delivery PartnerAmazon OpenSearch Service Delivery PartnerAWS DMS Service Delivery PartnerAWS Systems Manager Service Delivery PartnerAmazon RDS Service Delivery PartnerAWS CloudFormation Service Delivery PartnerAWS ConfigAmazon EMR and many more.

FAQs

1. What port should I expose for JMX Exporter in a containerized environment like Kubernetes?

ANS: – Expose the port defined for the JMX Exporter (e.g., 9404) in your container spec, and ensure the Prometheus scrape job can access it, either via service discovery or static IP.

2. Can I monitor custom MBeans using the JMX Exporter?

ANS: – Yes, you can configure the rules section in the JMX Exporter YAML to match your custom MBeans and extract their attributes as Prometheus metrics.

WRITTEN BY Ravikumar Eranna Murali

Ravikumar works as a Sr. Research Associate at CloudThat. His expertise lies in AWS Services and pursuing DevOps technologies like Kubernetes, Docker, and Jenkins. Ravi enjoys learning and working on new challenges to give the best solution.

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!