From b91dbbc706006cbcb3e1aea17dddbdedf1a19eba Mon Sep 17 00:00:00 2001 From: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Date: Tue, 9 Jul 2024 15:20:37 +0200 Subject: [PATCH 1/2] properly handle missing metrics with non-wildcards --- .../apm/agent/jmx/JmxMetricTracker.java | 48 +++++++++++-------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/apm-agent-plugins/apm-jmx-plugin/src/main/java/co/elastic/apm/agent/jmx/JmxMetricTracker.java b/apm-agent-plugins/apm-jmx-plugin/src/main/java/co/elastic/apm/agent/jmx/JmxMetricTracker.java index 5c3733a639..6518f4f03e 100644 --- a/apm-agent-plugins/apm-jmx-plugin/src/main/java/co/elastic/apm/agent/jmx/JmxMetricTracker.java +++ b/apm-agent-plugins/apm-jmx-plugin/src/main/java/co/elastic/apm/agent/jmx/JmxMetricTracker.java @@ -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; @@ -367,31 +369,39 @@ private void addJmxMetricRegistration(final JmxMetric jmxMetric, List 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()); } @@ -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); } From 9c40721c563df86c13b0469e683a4b9a5b814db1 Mon Sep 17 00:00:00 2001 From: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Date: Tue, 9 Jul 2024 15:31:08 +0200 Subject: [PATCH 2/2] update changelog --- CHANGELOG.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index ca3c30bcd8..0e8e5d8a83 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -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] [float] ===== Features