Skip to content

Commit

Permalink
jmx-metrics-fix (#3713)
Browse files Browse the repository at this point in the history
* properly handle missing metrics with non-wildcards

* update changelog

---------

Co-authored-by: jackshirazi <[email protected]>
  • Loading branch information
SylvainJuge and jackshirazi authored Jul 23, 2024
1 parent cf3d0fc commit 6dde355
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Use subheadings with the "=====" level for adding notes for unreleased changes:
* Restore compatibility with Java 7 - {pull}3657[#3657]
* Avoid `ClassCastException` and issue warning when trying to use otel span links - {pull}3672[#3672]
* Avoid `NullPointerException` with runtime attach API and invalid map entries - {pull}3712[#3712]
* Enhance invalid state JMX metrics handling - {pull}3713[#3713]
* Skips using NOFOLLOW_LINKS file open option when running on z/OS as it's unsupported there - {pull}3722[#3722]
[float]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.RuntimeMBeanException;
import javax.management.MBeanException;
import javax.management.ReflectionException;
import javax.management.openmbean.CompositeData;
import javax.management.relation.MBeanServerNotificationFilter;
import java.lang.management.ManagementFactory;
Expand Down Expand Up @@ -367,31 +369,39 @@ private void addJmxMetricRegistration(final JmxMetric jmxMetric, List<JmxMetricR
MBeanInfo info = server.getMBeanInfo(objectName);
MBeanAttributeInfo[] attrInfo = info.getAttributes();
for (MBeanAttributeInfo attr : attrInfo) {
try {
final Object value = server.getAttribute(objectName, attr.getName());
addJmxMetricRegistration(jmxMetric, registrations, objectName, value, attribute, attr.getName(), metricPrepend);
} catch (AttributeNotFoundException e) {
logger.warn("Can't create metric '{}' because attribute '{}' could not be found", jmxMetric, attribute.getJmxAttributeName());
} catch (RuntimeMBeanException e) {
if (e.getCause() instanceof UnsupportedOperationException) {
//ignore this attribute
} else {
throw e;
}
}
String attributeName = attr.getName();
tryAddJmxMetric(jmxMetric, registrations, server, attribute, objectName, attributeName, metricPrepend);
}
} else {
final Object value = server.getAttribute(objectName, attribute.getJmxAttributeName());
try {
addJmxMetricRegistration(jmxMetric, registrations, objectName, value, attribute, attribute.getJmxAttributeName(), null);
} catch (AttributeNotFoundException e) {
logger.warn("Can't create metric '{}' because attribute '{}' could not be found", jmxMetric, attribute.getJmxAttributeName());
}
String attributeName = attribute.getJmxAttributeName();
tryAddJmxMetric(jmxMetric, registrations, server, attribute, objectName, attributeName, null);
}
}
}
}

private void tryAddJmxMetric(JmxMetric jmxMetric,
List<JmxMetricRegistration> registrations,
MBeanServer server,
JmxMetric.Attribute attribute,
ObjectName objectName,
String attributeName,
@Nullable String metricPrepend) throws MBeanException, InstanceNotFoundException, ReflectionException {

try {
Object value = server.getAttribute(objectName, attributeName);
addJmxMetricRegistration(jmxMetric, registrations, objectName, value, attribute, attributeName, metricPrepend);
} catch (AttributeNotFoundException e) {
logger.warn("Can't create metric '{}' because attribute '{}' could not be found", jmxMetric, attributeName);
} catch (RuntimeMBeanException e) {
if (e.getCause() instanceof UnsupportedOperationException) {
// silently ignore this attribute, won't retry as it's not a transient runtime exception
} else {
throw e;
}
}
}

private static boolean isWildcard(JmxMetric.Attribute attribute) {
return "*".equals(attribute.getJmxAttributeName());
}
Expand Down Expand Up @@ -486,7 +496,7 @@ public double get() {
value = ((Number) ((CompositeData) server.getAttribute(objectName, jmxAttribute)).get(compositeDataKey)).doubleValue();
}
return value;
} catch (InstanceNotFoundException | AttributeNotFoundException e) {
} catch (InstanceNotFoundException | AttributeNotFoundException | RuntimeMBeanException e) {
if (unsubscribeOnError) {
unregister(tracer);
}
Expand Down

0 comments on commit 6dde355

Please sign in to comment.