Skip to content

Commit

Permalink
Merge pull request #1017 from microsoft/fix-gc-performance-counters
Browse files Browse the repository at this point in the history
Fix GCPerformanceCounter not working
  • Loading branch information
trask authored Aug 20, 2019
2 parents a811128 + 8be7e3b commit 1b72094
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import com.microsoft.applicationinsights.internal.logger.InternalLogger;
import com.microsoft.applicationinsights.internal.perfcounter.jvm.DeadLockDetectorPerformanceCounter;
import com.microsoft.applicationinsights.internal.perfcounter.jvm.GCPerformanceCounter;
import com.microsoft.applicationinsights.internal.perfcounter.jvm.JvmHeapMemoryUsedPerformanceCounter;
import org.apache.commons.lang3.exception.ExceptionUtils;

Expand All @@ -45,6 +46,7 @@ public Collection<PerformanceCounter> getPerformanceCounters() {
if (isEnabled) {
addDeadLockDetector(pcs);
addJvmMemoryPerformanceCounter(pcs);
addGCPerformanceCounter(pcs);
} else {
InternalLogger.INSTANCE.trace("JvmPerformanceCountersFactory is disabled");
}
Expand Down Expand Up @@ -99,7 +101,28 @@ private void addJvmMemoryPerformanceCounter(ArrayList<PerformanceCounter> pcs) {
} catch (Throwable t2) {
// chomp
}
}
}

private void addGCPerformanceCounter(ArrayList<PerformanceCounter> pcs) {
try {
if (disabledJvmPCs.contains(GCPerformanceCounter.NAME)) {
return;
}

GCPerformanceCounter mpc = new GCPerformanceCounter();
pcs.add(mpc);
} catch (ThreadDeath td) {
throw td;
} catch (Throwable t) {
try {
InternalLogger.INSTANCE.error("Failed to create GCPerformanceCounter, exception: %s",
ExceptionUtils.getStackTrace(t));
} catch (ThreadDeath td) {
throw td;
} catch (Throwable t2) {
// chomp
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void report(TelemetryClient telemetryClient) {
MetricTelemetry mtTotalTime = new MetricTelemetry(GC_TOTAL_TIME, timeToReport);

mtTotalCount.markAsCustomPerfCounter();
mtTotalCount.markAsCustomPerfCounter();
mtTotalTime.markAsCustomPerfCounter();

telemetryClient.track(mtTotalCount);
telemetryClient.track(mtTotalTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ public void testPerformanceCounterData() throws Exception {

// we should get these envelopes:
// MetricData, metrics.name="Suspected Deadlocked Threads"
// PerformanceCounterData, categoryName=Memory, counterName=Available Bytes
// MetricData, metrics.name="Heap Memory Used (MB)"
// MetricData, metrics.name="GC Total Count"
// MetricData, metrics.name="GC Total Time"
// PerformanceCounterData, categoryName=Memory, counterName=Available Bytes
// PerformanceCounterData, category=Process, counter="IO Data Bytes/sec"
// PerformanceCounterData, category=Processor, counter="% Processor Time", instance="_Total"
// PerformanceCounterData, category=Process, counter=Private Bytes
Expand Down Expand Up @@ -59,6 +61,10 @@ public void testPerformanceCounterData() throws Exception {
System.out.println("Waiting for metric data...");
Envelope deadlocks = mockedIngestion.waitForItem(getPerfMetricPredicate("Suspected Deadlocked Threads"), 150, TimeUnit.SECONDS);
Envelope heapUsed = mockedIngestion.waitForItem(getPerfMetricPredicate("Heap Memory Used (MB)"), 150, TimeUnit.SECONDS);
Envelope gcTotalCount = mockedIngestion.waitForItem(getPerfMetricPredicate("GC Total Count"), 150,
TimeUnit.SECONDS);
Envelope gcTotalTime = mockedIngestion.waitForItem(getPerfMetricPredicate("GC Total Time"), 150,
TimeUnit.SECONDS);
System.out.println("MetricData are good: " + (System.currentTimeMillis() - start));

MetricData mdDeadlocks = getBaseData(deadlocks);
Expand All @@ -68,6 +74,12 @@ public void testPerformanceCounterData() throws Exception {
MetricData mdHeapUsed = getBaseData(heapUsed);
assertPerfMetric(mdHeapUsed);
assertTrue(mdHeapUsed.getMetrics().get(0).getValue() > 0.0);

MetricData mdGcTotalCount = getBaseData(gcTotalCount);
assertPerfMetric(mdGcTotalCount);

MetricData mdGcTotalTime = getBaseData(gcTotalTime);
assertPerfMetric(mdGcTotalTime);
}

private void assertSameInstanceName(Envelope... envelopes) {
Expand Down

0 comments on commit 1b72094

Please sign in to comment.