Azure Application Insights - Java Performance Monitoring


Configure and monitor JMX metrics for your Java application using Azure Application Insights.


               
JMX (Java Management Extensions) metrics are a set of performance and resource utilization data points that can be collected from a Java application. JMX provides a standardized way to manage and monitor applications, system objects, devices, and service-oriented networks.

JMX metrics typically include:

Memory Usage: Information about heap and non-heap memory usage.
Garbage Collection: Data on the frequency and duration of garbage collection.
Thread Usage: Details on the number of active, idle, and blocked threads.
Class Loading: Metrics on the number of loaded, unloaded, and total classes in the JVM.
CPU Usage: Information on the CPU usage by the JVM process.
Custom Metrics: Application-specific metrics that can be defined by the developer, such as request counts, error rates, and transaction times.

These metrics are valuable for monitoring the health and performance of a Java application, identifying bottlenecks, and ensuring that the application is running efficiently

We can effectively monitor Java application running in a Azure Linux VMs by enabling JMX on your Java application, configuring the Application Insights Java Agent to collect JMX metrics, and setting up visualization and alerts in Azure Monitor, 

This approach leverages the powerful monitoring and analytics capabilities of Azure Application Insights to provide comprehensive insights into your Java application's performance and health.

Below are the steps to set up JMX monitoring, integrate with Azure Application Insights, and visualize the collected metrics.

Set Up Application Insights

1.     Create an Application Insights Resource: In the Azure portal, create an Application Insights resource for your Java application if you haven’t already done so.

2.     Get the Instrumentation Key: From the Application Insights resource overview page, copy the Instrumentation Key. You will need this to configure your application.


Enable JMX on Your Java Application

1.     Modify JVM Options: Add the following options to the JVM startup parameters of your Java application to enable JMX remote monitoring:

-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=12345
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=<VM_IP_ADDRESS>

Replace <VM_IP_ADDRESS> with the IP address of your Linux VM.

2.     Restart your Java application with the updated JVM options.


Install and Configure Application Insights Java Agent

     1.  Download the Java Agent:

Download the Application Insights Java Agent from the Azure Application Insights Java GitHub repository.

 
wget https://github.com/microsoft/ApplicationInsights-Java/releases/download/3.2.5/applicationinsights-agent-3.2.5.jar

2.     Configure the Java Agent:

Add the Java Agent to your Java application startup script. Update the JVM_OPTS to include the Application Insights agent:

JAVA_AGENT_PATH=/path/to/applicationinsights-agent-3.x.x.jar
JAVA_AGENT_CONFIG_PATH=/path/to/applicationinsights.json

JVM_OPTS="$JVM_OPTS -javaagent:$JAVA_AGENT_PATH -Dapplicationinsights.configuration.file=$JAVA_AGENT_CONFIG_PATH"


3.     Configure Application Insights:  

The applicationinsights.json file is not included by default. You need to create this configuration file in your application's directory Create a new applicationinsights.json file in your application's root directory. This file will contain the configuration settings for the Application Insights Java Agent, including JMX metrics.

 {

  "instrumentationKey": "YOUR_INSTRUMENTATION_KEY",
  "jmxMetrics": {
    "enabled": true,
    "url": "service:jmx:rmi:///jndi/rmi://<VM_IP_ADDRESS>:12345/jmxrmi",
    "objectNames": ["java.lang:type=Memory", "java.lang:type=Threading"],
    "attributes": ["HeapMemoryUsage", "NonHeapMemoryUsage", "ThreadCount"]
  }
}

Replace YOUR_INSTRUMENTATION_KEY with the Instrumentation Key you copied earlier and <VM_IP_ADDRESS> with the IP address of your Linux VM.

4.     Restart your Java application with the updated JVM options to apply the changes.


Applying the Configuration

  1. Set the Application Insights Agent:

    To use the Application Insights Java Agent with your Java application, you need to specify the agent at the time of running your application. This is done using the
    -javaagent parameter.

java -javaagent:/path/to/applicationinsights-agent-3.2.5.jar -jar your-application.jar

  1. Ensure the applicationinsights.json File is Found:

    Place the
    applicationinsights.json file in the same directory as your application's JAR file, or specify the path to the configuration file using the APPLICATIONINSIGHTS_CONFIGURATION_FILE environment variable.

export APPLICATIONINSIGHTS_CONFIGURATION_FILE=/path/to/applicationinsights.json

Sample applicationinsights.json Configuration


The applicationinsights.json file is typically used to configure Azure Application Insights for Java applications. This file is part of the Application Insights Java Agent, which collects telemetry data from your Java application.

Here is an example of what your applicationinsights.json file might look like, including configuration for JMX metrics:

{

    "instrumentationSettings": {
     "connectionString": "<Your-Connection-String>"
    },
    "preview": {

    
   "jmxMetrics":
[

            {
                "name": "HeapMemoryUsage",
                "objectName": "java.lang:type=Memory",
                "attribute": "HeapMemoryUsage",
                "type": "gauge",
                "aggregation": "average"
            },

            {
                "name": "NonHeapMemoryUsage",
                "objectName": "java.lang:type=Memory",
                "attribute": "NonHeapMemoryUsage",
                "type": "gauge",
                "aggregation": "average"
            },

            {
                "name": "ThreadCount",
                "objectName": "java.lang:type=Threading",
                "attribute": "ThreadCount",
                "type": "gauge",
                "aggregation": "average"
            },

            {
                "name": "PeakThreadCount",
                "objectName": "java.lang:type=Threading",
                "attribute": "PeakThreadCount",
                "type": "gauge",
                "aggregation": "average"
            },

            {
                "name": "CollectionCount",
                "objectName": "java.lang:type=GarbageCollector,name=PS Scavenge",
                "attribute": "CollectionCount",
                "type": "counter"
            },

            {
                "name": "CollectionTime",
                "objectName": "java.lang:type=GarbageCollector,name=PS Scavenge",
                "attribute": "CollectionTime",
                "type": "counter"
           }
        ]
    }
} 


 
JMX Metrics:

                JMX metrics collection can be configured by adding a "jmxMetrics" section to the applicationinsights.json file. Enter a name for the metric as you want it to appear in the Azure portal in the application insights resource. Object name and attributes are required for each of the metrics you want collected. Below are a few examples Metrics

Memory

   java.lang:type=Memory
HeapMemoryUsage: Current heap memory usage.
 NonHeapMemoryUsage: Current non-heap memory usage.
ObjectPendingFinalizationCount: Number of objects pending finalization.


Garbage Collector

§ Java.lang:type=GarbageCollector,name=*

§  CollectionCount: Number of garbage collections that have occurred.

§  CollectionTime: Total time spent on garbage collection.


Class Loading

§  java.lang:type=ClassLoading

§  LoadedClassCount: Number of classes currently loaded in the JVM.

§  TotalLoadedClassCount: Total number of classes loaded since the JVM started.

§  UnloadedClassCount: Total number of classes unloaded since the JVM started.

Threads

§  java.lang:type=Threading

§  ThreadCount: Current number of live threads including both daemon and non-daemon threads.

§  PeakThreadCount: Peak live thread count since the JVM started or since the peak was reset.

§  DaemonThreadCount: Current number of live daemon threads.

§  TotalStartedThreadCount: Total number of threads started since the JVM started.

 

Compilation

§  java.lang:type=Compilation
TotalCompilationTime: Approximate accumulated elapsed time spent in compilation.

Operating System

§  java.lang:type=OperatingSystem

§  Name: Operating system name.

§  Arch: Operating system architecture.

§  Version: Operating system version.

§  AvailableProcessors: Number of available processors.

§  SystemLoadAverage: System load average for the last minute.


·        Buffer Pool Metrics

      • java.nio:type=BufferPool,name=*
        Count: Number of buffers in the pool.
      • TotalCapacity: Total capacity of the buffers in the pool.
      • MemoryUsed: Memory used by the buffers in the pool.

MBean Metrics

Custom MBeans registered in the application can provide additional metrics specific to the application, such as:

Application-specific Metrics

      • com.example:type=YourCustomMBean
      • Custom attributes and operations as defined in your MBean.

 

Verifying the Configuration

  1. Check Logs:
     Ensure that the Application Insights agent is running by checking the logs of your Java application. Look for messages indicating that the agent has started and is collecting telemetry data.

  2. Monitor in Azure Portal:
    Verify that telemetry data, including JMX metrics, is being sent to Application Insights by checking the metrics and logs in the Azure portal.

 Visualize and Monitor Metrics in Azure

1.     Access Application Insights:
 In the Azure portal, go to your Application Insights resource.

2.     View Metrics:
Navigate to the "Metrics" section to view the JMX metrics collected by the Application Insights Java Agent.





3.     Create Dashboards and Alerts:
Use Azure Monitor to create custom dashboards and set up alerts based on the metrics collected. This helps you monitor the performance and health of your Java application effectively.

Example of Metrics Query

            Use the following example query to visualize JMX metrics in Application Insights:

        customMetrics
        | where name == "HeapMemoryUsage" or name == "ThreadCount"
        | summarize avg(value) by bin(timestamp, 1m), name
        | render timechart
 
 
 Reference Microsoft Links:-  

  How to configure JMX metrics - Azure Monitor application insights for Java - Azure Monitor | Microsoft Learn

 

Comments

Popular posts from this blog

Convert Azure VM Security Type - Trusted Launch to Standard to Enable ASR on existing workloads.

Azure Storage Mover