Skip to content

Commit 46d2ed5

Browse files
committed
Log as warn invalid JMX metrics
1 parent be2bd04 commit 46d2ed5

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/perfcounter/AbstractJmxPerformanceCounter.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient;
99
import java.util.Collection;
10+
import java.util.HashSet;
1011
import java.util.Map;
12+
import java.util.Set;
1113
import org.slf4j.Logger;
1214
import org.slf4j.LoggerFactory;
1315
import org.slf4j.MDC;
@@ -24,6 +26,8 @@ public abstract class AbstractJmxPerformanceCounter implements PerformanceCounte
2426
private final Collection<JmxAttributeData> attributes;
2527
private boolean alreadyLogged = false;
2628

29+
private final Set<String> invalidJmxMetrics = new HashSet<>();
30+
2731
/**
2832
* The main method. The method will fetch the data and send it. The method will not do anything if
2933
* there was a major problem accessing the needed counter.
@@ -38,6 +42,7 @@ public synchronized void report(TelemetryClient telemetryClient) {
3842
for (Map.Entry<String, Collection<Object>> displayAndValues : result.entrySet()) {
3943
boolean ok = true;
4044
double value = 0.0;
45+
String metricName = displayAndValues.getKey();
4146
for (Object obj : displayAndValues.getValue()) {
4247
try {
4348
if (obj instanceof Boolean) {
@@ -46,14 +51,22 @@ public synchronized void report(TelemetryClient telemetryClient) {
4651
value += Double.parseDouble(String.valueOf(obj));
4752
}
4853
} catch (RuntimeException e) {
54+
if (!invalidJmxMetrics.contains(metricName)) {
55+
invalidJmxMetrics.add(metricName);
56+
try (MDC.MDCCloseable ignored = CUSTOM_JMX_METRIC_ERROR.makeActive()) {
57+
logger.warn(
58+
"{} JMX metric is invalid because only numeric and boolean JMX metric values are supported.",
59+
metricName);
60+
}
61+
}
4962
ok = false;
5063
break;
5164
}
5265
}
5366

5467
if (ok) {
5568
try {
56-
send(telemetryClient, displayAndValues.getKey(), value);
69+
send(telemetryClient, metricName, value);
5770
} catch (RuntimeException e) {
5871
try (MDC.MDCCloseable ignored = CUSTOM_JMX_METRIC_ERROR.makeActive()) {
5972
logger.error("Error while sending JMX data: '{}'", e.toString());

0 commit comments

Comments
 (0)