[JAVA] JVM performance counter settings for ApplicationInsights.xml

Nobody put it together, so I made it by hitting ManagementFactory.getPlatformMBeanServer (). For the time being, I'm taking the CPU, memory, GC times and time.

I think it can be used when creating a JVM application that is supposed to collect performance counters with Application Insights.

Since it will be either PS / CMS / G1 with the GC option, we are sorting it out. However, at the moment, it seems that counters that do not exist are not collected in some cases, so I think that you can throw in with this setting.

displayName will be the name when searching with Analytics, so you can rewrite it appropriately.

<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights
	xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
	<PerformanceCounters>
		<UseBuiltIn>False</UseBuiltIn>
		<Jmx>
			<!-- CPU -->
			<Add objectName="java.lang:type=OperatingSystem"
				attribute="ProcessCpuLoad" displayName="CPU Process Load" />
			<Add objectName="java.lang:type=OperatingSystem"
				attribute="SystemCpuLoad" displayName="CPU System Load" />
			<!-- Thread -->
			<Add objectName="java.lang:type=Threading" attribute="ThreadCount"
				displayName="Thread Count" />

			<!-- Memory -->
			<Add objectName="java.lang:type=Memory" attribute="HeapMemoryUsage.used"
				displayName="MEM Heap Used" type="composite" />
			<Add objectName="java.lang:type=Memory" attribute="HeapMemoryUsage.committed"
				displayName="MEM Heap Committed" type="composite" />

			<Add objectName="java.lang:type=Memory" attribute="NonHeapMemoryUsage.used"
				displayName="MEM Non Heap Used" type="composite" />
			<Add objectName="java.lang:type=Memory" attribute="NonHeapMemoryUsage.committed"
				displayName="MEM Non Heap Committed" type="composite" />

			<!-- Memory(Metaspace) JVM 8 over -->
			<Add objectName="java.lang:type=MemoryPool,name=Metaspace" attribute="Usage.used"
				displayName="MEM Metaspace Used" type="composite" />
			<Add objectName="java.lang:type=MemoryPool,name=Metaspace" attribute="Usage.committed"
				displayName="MEM Metaspace Committed" type="composite" />

			<!-- Memory(PS) -->
			<!-- Not works... -->
			<Add objectName="java.lang:type=MemoryPool,name=PS Eden Space" attribute="Usage.used"
				displayName="MEM(PS) New Used" type="composite" />
			<Add objectName="java.lang:type=MemoryPool,name=PS Eden Space" attribute="Usage.committed"
				displayName="MEM(PS) New Committed" type="composite" />
			<Add objectName="java.lang:type=MemoryPool,name=PS Survivor Space" attribute="Usage.used"
				displayName="MEM(PS) Survivor Used" type="composite" />
			<Add objectName="java.lang:type=MemoryPool,name=PS Survivor Space" attribute="Usage.committed"
				displayName="MEM(PS) Survivor Committed" type="composite" />
			<Add objectName="java.lang:type=MemoryPool,name=PS Old Gen" attribute="Usage.used"
				displayName="MEM(PS) Old Used" type="composite" />
			<Add objectName="java.lang:type=MemoryPool,name=PS Old Gen" attribute="Usage.committed"
				displayName="MEM(PS) Old Committed" type="composite" />
			<!-- GC(PS) -->
			<Add objectName="java.lang:type=GarbageCollector,name=PS MarkSweep"
				attribute="CollectionCount" displayName="FGC - Count" />
			<Add objectName="java.lang:type=GarbageCollector,name=PS MarkSweep"
				attribute="CollectionTime" displayName="FGC - Time" />
			<Add objectName="java.lang:type=GarbageCollector,name=PS Scavenge"
				attribute="CollectionTime" displayName="YGC - Time" />
			<Add objectName="java.lang:type=GarbageCollector,name=PS Scavenge"
				attribute="CollectionCount" displayName="YGC - Count" />

			<!-- Memory(CMS) -XX:+UseConcMarkSweepGC -->
			<Add objectName="java.lang:type=MemoryPool,name=Par Eden Space" attribute="Usage.used"
				displayName="MEM(CMS) New Used" type="composite" />
			<Add objectName="java.lang:type=MemoryPool,name=Par Eden Space" attribute="Usage.committed"
				displayName="MEM(CMS) New Committed" type="composite" />
			<Add objectName="java.lang:type=MemoryPool,name=Par Survivor Space" attribute="Usage.used"
				displayName="MEM(CMS) Survivor Used" type="composite" />
			<Add objectName="java.lang:type=MemoryPool,name=Par Survivor Space" attribute="Usage.committed"
				displayName="MEM(CMS) Survivor Committed" type="composite" />
			<Add objectName="java.lang:type=MemoryPool,name=CMS Old Gen" attribute="Usage.used"
				displayName="MEM(CMS) Old Used" type="composite" />
			<Add objectName="java.lang:type=MemoryPool,name=CMS Old Gen" attribute="Usage.committed"
				displayName="MEM(CMS) Old Committed" type="composite" />
			<!-- GC(CMS) -->
			<Add objectName="java.lang:type=GarbageCollector,name=ConcurrentMarkSweep"
				attribute="CollectionCount" displayName="FGC(CMS) Count" />
			<Add objectName="java.lang:type=GarbageCollector,name=ConcurrentMarkSweep"
				attribute="CollectionTime" displayName="FGC(CMS) Time" />
			<Add objectName="java.lang:type=GarbageCollector,name=ParNew"
				attribute="CollectionTime" displayName="YGC(CMS) Time" />
			<Add objectName="java.lang:type=GarbageCollector,name=ParNew"
				attribute="CollectionCount" displayName="YGC(CMS) Count" />

			<!-- Memory(G1GC) -XX:+UseG1GC -->
			<Add objectName="java.lang:type=MemoryPool,name=G1 Eden Space" attribute="Usage.used"
				displayName="MEM(G1) New Used" type="composite" />
			<Add objectName="java.lang:type=MemoryPool,name=G1 Eden Space" attribute="Usage.committed"
				displayName="MEM(G1) New Committed" type="composite" />
			<Add objectName="java.lang:type=MemoryPool,name=G1 Survivor Space" attribute="Usage.used"
				displayName="MEM(G1) Survivor Used" type="composite" />
			<Add objectName="java.lang:type=MemoryPool,name=G1 Survivor Space" attribute="Usage.committed"
				displayName="MEM(G1) Survivor Committed" type="composite" />
			<Add objectName="java.lang:type=MemoryPool,name=G1 Old Gen" attribute="Usage.used"
				displayName="MEM(G1) Old Used" type="composite" />
			<Add objectName="java.lang:type=MemoryPool,name=G1 Old Gen" attribute="Usage.committed"
				displayName="MEM(G1) Old Committed" type="composite" />
			<!-- GC(G1GC) -->
			<Add objectName="java.lang:type=GarbageCollector,name=G1 Young Generation"
				attribute="CollectionCount" displayName="YGC(G1) Count" />
			<Add objectName="java.lang:type=GarbageCollector,name=G1 Young Generation"
				attribute="CollectionTime" displayName="YGC(G1) Time" />
			<Add objectName="java.lang:type=GarbageCollector,name=G1 Old Generation"
				attribute="CollectionTime" displayName="FGC(G1) Time" />
			<Add objectName="java.lang:type=GarbageCollector,name=G1 Old Generation"
				attribute="CollectionCount" displayName="FGC(G1) Count" />
		</Jmx>
	</PerformanceCounters>
</ApplicationInsights>

(Added on 2019/07/09)

By the way, if useBuiltIn in XML is set to False, Metrics around here will not take the ones with BuiltIn. (Processing location )

The following is a good idea for detailed XML settings. The default interval is 60 seconds, so if you want to make it shorter, you need to write the setting.

https://github.com/microsoft/ApplicationInsights-Java/wiki/ApplicationInsights.XML

Also, some values such as java.lang: type = GarbageCollector, name = PS Mark Sweep cannot be acquired. The following is the issue. Cannot fetch jmx counter for GC with Java SDK agent #952

(Addition so far)

Code when hitting ManagementFactory.getPlatformMBeanServer () to make the above list

import java.lang.management.ManagementFactory;
import java.util.Set;

import javax.management.Attribute;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.ReflectionException;

public class Test {

	public static void main(String[] s)
			throws ReflectionException, IntrospectionException, InstanceNotFoundException, AttributeNotFoundException,
			MBeanException {
		MBeanServer server = ManagementFactory.getPlatformMBeanServer();
		Set<ObjectName> names = server.queryNames(null, null);
		for (ObjectName name : names) {
			System.out.println(name);

			MBeanInfo beanInfo = server.getMBeanInfo(name);
			for (MBeanAttributeInfo attrInfo : beanInfo.getAttributes()) {
				try {
					Object attrObject = server.getAttribute(name, attrInfo.getName());
					if (attrObject instanceof Attribute) {
						Attribute attr = (Attribute) attrObject;
						Object attrValueObject = ((Attribute) attrObject).getValue();
						System.out.printf("  attribute: %s = %s\n", attrInfo.getName(), attrValueObject);
					} else {
						System.out.printf("  attribute: %s = %s\n", attrInfo.getName(), attrObject);
					}
				} catch (Exception e) {
					System.out.printf("  attribute: %s = %s\n", attrInfo.getName(), "???");
				}
			}
		}
	}
}

The composite attribute is like taking the key-value of contents. (I didn't know how to get the key value, but I can see it with toString) For example, memory is the case, and the output is as follows.

java.lang:type=MemoryPool,name=Metaspace
~ Omitted ~
  attribute: Usage = javax.management.openmbean.CompositeDataSupport(compositeType=javax.management.openmbean.CompositeType(name=java.lang.management.MemoryUsage,items=((itemName=committed,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=init,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=max,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=used,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)))),contents={committed=6029312, init=0, max=-1, used=5663360})

You can see the contents at the back.

In this case, the Attribute name is "Usage" in JMX and "used" in contents, but when writing in XML of Application Insights, it is like adding type =" composite " attribute to attribute as ʻUsage.used` Become.

Recommended Posts

JVM performance counter settings for ApplicationInsights.xml
Gradle settings for using JUnit 5