Skip to content

Commit 8be7e3b

Browse files
committed
Fix GCPerformanceCounter not working
1 parent a4437aa commit 8be7e3b

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

core/src/main/java/com/microsoft/applicationinsights/internal/perfcounter/JvmPerformanceCountersFactory.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import com.microsoft.applicationinsights.internal.logger.InternalLogger;
2929
import com.microsoft.applicationinsights.internal.perfcounter.jvm.DeadLockDetectorPerformanceCounter;
30+
import com.microsoft.applicationinsights.internal.perfcounter.jvm.GCPerformanceCounter;
3031
import com.microsoft.applicationinsights.internal.perfcounter.jvm.JvmHeapMemoryUsedPerformanceCounter;
3132
import org.apache.commons.lang3.exception.ExceptionUtils;
3233

@@ -45,6 +46,7 @@ public Collection<PerformanceCounter> getPerformanceCounters() {
4546
if (isEnabled) {
4647
addDeadLockDetector(pcs);
4748
addJvmMemoryPerformanceCounter(pcs);
49+
addGCPerformanceCounter(pcs);
4850
} else {
4951
InternalLogger.INSTANCE.trace("JvmPerformanceCountersFactory is disabled");
5052
}
@@ -99,7 +101,28 @@ private void addJvmMemoryPerformanceCounter(ArrayList<PerformanceCounter> pcs) {
99101
} catch (Throwable t2) {
100102
// chomp
101103
}
104+
}
105+
}
102106

107+
private void addGCPerformanceCounter(ArrayList<PerformanceCounter> pcs) {
108+
try {
109+
if (disabledJvmPCs.contains(GCPerformanceCounter.NAME)) {
110+
return;
111+
}
112+
113+
GCPerformanceCounter mpc = new GCPerformanceCounter();
114+
pcs.add(mpc);
115+
} catch (ThreadDeath td) {
116+
throw td;
117+
} catch (Throwable t) {
118+
try {
119+
InternalLogger.INSTANCE.error("Failed to create GCPerformanceCounter, exception: %s",
120+
ExceptionUtils.getStackTrace(t));
121+
} catch (ThreadDeath td) {
122+
throw td;
123+
} catch (Throwable t2) {
124+
// chomp
125+
}
103126
}
104127
}
105128

core/src/main/java/com/microsoft/applicationinsights/internal/perfcounter/jvm/GCPerformanceCounter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void report(TelemetryClient telemetryClient) {
8080
MetricTelemetry mtTotalTime = new MetricTelemetry(GC_TOTAL_TIME, timeToReport);
8181

8282
mtTotalCount.markAsCustomPerfCounter();
83-
mtTotalCount.markAsCustomPerfCounter();
83+
mtTotalTime.markAsCustomPerfCounter();
8484

8585
telemetryClient.track(mtTotalCount);
8686
telemetryClient.track(mtTotalTime);

test/smoke/testApps/AutoPerfCounters/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/PerfCountersDataTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ public void testPerformanceCounterData() throws Exception {
2626

2727
// we should get these envelopes:
2828
// MetricData, metrics.name="Suspected Deadlocked Threads"
29-
// PerformanceCounterData, categoryName=Memory, counterName=Available Bytes
3029
// MetricData, metrics.name="Heap Memory Used (MB)"
30+
// MetricData, metrics.name="GC Total Count"
31+
// MetricData, metrics.name="GC Total Time"
32+
// PerformanceCounterData, categoryName=Memory, counterName=Available Bytes
3133
// PerformanceCounterData, category=Process, counter="IO Data Bytes/sec"
3234
// PerformanceCounterData, category=Processor, counter="% Processor Time", instance="_Total"
3335
// PerformanceCounterData, category=Process, counter=Private Bytes
@@ -59,6 +61,10 @@ public void testPerformanceCounterData() throws Exception {
5961
System.out.println("Waiting for metric data...");
6062
Envelope deadlocks = mockedIngestion.waitForItem(getPerfMetricPredicate("Suspected Deadlocked Threads"), 150, TimeUnit.SECONDS);
6163
Envelope heapUsed = mockedIngestion.waitForItem(getPerfMetricPredicate("Heap Memory Used (MB)"), 150, TimeUnit.SECONDS);
64+
Envelope gcTotalCount = mockedIngestion.waitForItem(getPerfMetricPredicate("GC Total Count"), 150,
65+
TimeUnit.SECONDS);
66+
Envelope gcTotalTime = mockedIngestion.waitForItem(getPerfMetricPredicate("GC Total Time"), 150,
67+
TimeUnit.SECONDS);
6268
System.out.println("MetricData are good: " + (System.currentTimeMillis() - start));
6369

6470
MetricData mdDeadlocks = getBaseData(deadlocks);
@@ -68,6 +74,12 @@ public void testPerformanceCounterData() throws Exception {
6874
MetricData mdHeapUsed = getBaseData(heapUsed);
6975
assertPerfMetric(mdHeapUsed);
7076
assertTrue(mdHeapUsed.getMetrics().get(0).getValue() > 0.0);
77+
78+
MetricData mdGcTotalCount = getBaseData(gcTotalCount);
79+
assertPerfMetric(mdGcTotalCount);
80+
81+
MetricData mdGcTotalTime = getBaseData(gcTotalTime);
82+
assertPerfMetric(mdGcTotalTime);
7183
}
7284

7385
private void assertSameInstanceName(Envelope... envelopes) {

0 commit comments

Comments
 (0)