Skip to content

Commit

Permalink
More JMX debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
trask committed Mar 3, 2022
1 parent 745e508 commit bb80b30
Showing 1 changed file with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;

import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -119,31 +119,33 @@ private static Map<String, Set<String>> getAvailableJmxAttributes() {
Map<String, Set<String>> availableJmxMetrics = new HashMap<>();
for (ObjectName objectName : objectNames) {
String name = objectName.toString();
Set<String> attrs;
try {
Set<String> attrs = getJmxAttributes(server, objectName);
if (!attrs.isEmpty()) {
availableJmxMetrics.put(name, attrs);
attrs = getNumericAttributeNames(server, objectName);
if (attrs.isEmpty()) {
attrs.add("<no numeric attributes found>");
}
} catch (Exception e) {
attrs = singleton("<error getting attributes: " + e);
// log exception at trace level since this is expected in several cases, e.g.
// "java.lang.UnsupportedOperationException: CollectionUsage threshold is not supported"
// and available jmx metrics are already only logged at debug
logger.trace(e.getMessage(), e);
availableJmxMetrics.put(name, Collections.singleton("<error getting attributes: " + e));
}
availableJmxMetrics.put(name, attrs);
}
return availableJmxMetrics;
}

private static Set<String> getJmxAttributes(MBeanServer server, ObjectName objectName)
private static Set<String> getNumericAttributeNames(MBeanServer server, ObjectName objectName)
throws Exception {
MBeanInfo mbeanInfo = server.getMBeanInfo(objectName);
Set<String> attributeNames = new HashSet<>();
Set<String> numericAttributeNames = new HashSet<>();
for (MBeanAttributeInfo attribute : mbeanInfo.getAttributes()) {
if (attribute.isReadable()) {
try {
Object value = server.getAttribute(objectName, attribute.getName());
attributeNames.addAll(getNumericAttributes(attribute, value));
numericAttributeNames.addAll(getNumericAttributeNames(attribute, value));
} catch (Exception e) {
// log exception at trace level since this is expected in several cases, e.g.
// "java.lang.UnsupportedOperationException: CollectionUsage threshold is not supported"
Expand All @@ -152,10 +154,10 @@ private static Set<String> getJmxAttributes(MBeanServer server, ObjectName objec
}
}
}
return attributeNames;
return numericAttributeNames;
}

private static List<String> getNumericAttributes(MBeanAttributeInfo attribute, Object value) {
private static List<String> getNumericAttributeNames(MBeanAttributeInfo attribute, Object value) {
String attributeType = attribute.getType();
if (NUMERIC_ATTRIBUTE_TYPES.contains(attributeType) && value instanceof Number) {
return singletonList(attribute.getName());
Expand Down

0 comments on commit bb80b30

Please sign in to comment.