Skip to content

Commit 213ccb5

Browse files
committed
Feedback and put the code at the right place
1 parent 46d2ed5 commit 213ccb5

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/PerformanceCounterInitializer.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.util.HashMap;
3232
import java.util.List;
3333
import java.util.Map;
34+
import java.util.Set;
35+
import java.util.concurrent.ConcurrentHashMap;
3436
import org.slf4j.Logger;
3537
import org.slf4j.LoggerFactory;
3638
import org.slf4j.MDC;
@@ -41,6 +43,8 @@ public class PerformanceCounterInitializer {
4143
private static final String METRIC_NAME_REGEXP = "[a-zA-Z0-9_.-/]+";
4244
private static final String INVALID_CHARACTER_REGEXP = "[^a-zA-Z0-9_.-/]";
4345

46+
private static final Set<String> invalidJmxMetrics = ConcurrentHashMap.newKeySet();
47+
4448
public static void initialize(Configuration configuration) {
4549

4650
PerformanceCounterContainer.INSTANCE.setCollectionFrequencyInSec(
@@ -209,6 +213,13 @@ private static void calculateAndRecordValueForAttribute(
209213
value += Double.parseDouble(String.valueOf(obj));
210214
}
211215
} catch (RuntimeException e) {
216+
if (invalidJmxMetrics.add(jmxAttributeData.metricName)) {
217+
try (MDC.MDCCloseable ignored = CUSTOM_JMX_METRIC_ERROR.makeActive()) {
218+
logger.warn(
219+
"{} JMX metric is invalid because only numeric and boolean JMX metric values are supported.",
220+
jmxAttributeData.metricName);
221+
}
222+
}
212223
ok = false;
213224
break;
214225
}

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

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

88
import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient;
99
import java.util.Collection;
10-
import java.util.HashSet;
1110
import java.util.Map;
12-
import java.util.Set;
1311
import org.slf4j.Logger;
1412
import org.slf4j.LoggerFactory;
1513
import org.slf4j.MDC;
@@ -26,8 +24,6 @@ public abstract class AbstractJmxPerformanceCounter implements PerformanceCounte
2624
private final Collection<JmxAttributeData> attributes;
2725
private boolean alreadyLogged = false;
2826

29-
private final Set<String> invalidJmxMetrics = new HashSet<>();
30-
3127
/**
3228
* The main method. The method will fetch the data and send it. The method will not do anything if
3329
* there was a major problem accessing the needed counter.
@@ -42,7 +38,6 @@ public synchronized void report(TelemetryClient telemetryClient) {
4238
for (Map.Entry<String, Collection<Object>> displayAndValues : result.entrySet()) {
4339
boolean ok = true;
4440
double value = 0.0;
45-
String metricName = displayAndValues.getKey();
4641
for (Object obj : displayAndValues.getValue()) {
4742
try {
4843
if (obj instanceof Boolean) {
@@ -51,22 +46,14 @@ public synchronized void report(TelemetryClient telemetryClient) {
5146
value += Double.parseDouble(String.valueOf(obj));
5247
}
5348
} 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-
}
6249
ok = false;
6350
break;
6451
}
6552
}
6653

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

0 commit comments

Comments
 (0)