Skip to content

JMX monitoring for Java applications

JMX monitoring is a technique used to observe and manage the internal state of Java applications, specifically focusing on Metrics related to the Java Virtual Machine (JVM) and application performance^[400-devops__06-Kubernetes__k8s-paas__07.Promtheus监控k8s企业级应用.md].

In containerized and cloud-native environments, JMX monitoring is typically implemented by running a JMX Exporter (Java Agent) alongside the application. This agent collects JMX data—such as memory usage, thread counts, and garbage collection statistics—and exposes it in a format that monitoring tools like Prometheus can scrape^[400-devops__06-Kubernetes__k8s-paas__07.Promtheus监控k8s企业级应用.md].

Implementation

To enable JMX monitoring, a Java Agent JAR (commonly jmx_prometheus_javaagent) is included in the application's deployment^[400-devops__06-Kubernetes__k8s-paas__07.Promtheus监控k8s企业级应用.md]. This agent is configured to run as part of the application's startup process.

Configuration with Tomcat

In environments using Apache Tomcat, the JMX agent is integrated into the container's startup sequence^[400-devops__06-Kubernetes__k8s-paas__07.Promtheus监控k8s企业级应用.md].

  1. Agent Placement: The jmx_prometheus_javaagent JAR and a configuration file (e.g., config.yml) are added to the container image, typically placed in a directory like /opt/prom/^[400-devops__06-Kubernetes__k8s-paas__07.Promtheus监控k8s企业级应用.md].
  2. Startup Arguments: The Tomcat startup script (catalina.sh) is modified to include the Java Agent option. This usually involves adding a -javaagent parameter to the JAVA_OPTS environment variable^[400-devops__06-Kubernetes__k8s-paas__07.Promtheus监控k8s企业级应用.md].

    Example startup option logic: -javaagent:/opt/prom/jmx_javaagent-0.3.1.jar=$(hostname -i):12346:/opt/prom/config.yml

  3. Port Exposure: The agent is configured to listen on a specific port (e.g., 12346) to serve the Metrics in an HTTP format compatible with Prometheus^[400-devops__06-Kubernetes__k8s-paas__07.Promtheus监控k8s企业级应用.md].

Integration with Prometheus

Once the application is running with the JMX agent, it exposes an HTTP endpoint on the configured port^[400-devops__06-Kubernetes__k8s-paas__07.Promtheus监控k8s企业级应用.md].

To scrape these Metrics, the application's deployment (e.g., a Pod in Kubernetes) must expose this port and include specific annotations. These annotations inform the Prometheus service discovery mechanism to scrape the target^[400-devops__06-Kubernetes__k8s-paas__07.Promtheus监控k8s企业级应用.md].

Example annotations required on the Pod: * prometheus_io_scrape: "true" * prometheus_io_port: "12346" * prometheus_io_path: "/"

After configuration, the Metrics appear in Prometheus and can be visualized in Grafana using JMX-specific dashboards^[400-devops__06-Kubernetes__k8s-paas__07.Promtheus监控k8s企业级应用.md].

Sources

  • 400-devops__06-Kubernetes__k8s-paas__07.Promtheus监控k8s企业级应用.md