From 43c6494a7682e174b3eb3dcd8ec24ded3bb5756d Mon Sep 17 00:00:00 2001 From: Scott Opell Date: Thu, 19 Oct 2023 14:44:23 -0400 Subject: [PATCH] Improves telemetry bean exception handling (#488) * Improves telemetry bean exception handling * Updates log lines for lint length and clarity --- .../java/org/datadog/jmxfetch/Instance.java | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index 54b1274f0..56ef416da 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -279,7 +279,7 @@ public Instance( log.info("collect_default_jvm_metrics is false - not collecting default JVM metrics"); } - instanceTelemetryBean = createJmxBean(); + instanceTelemetryBean = createInstanceTelemetryBean(); } private ObjectName getObjName(String domain,String instance) @@ -287,7 +287,7 @@ private ObjectName getObjName(String domain,String instance) return new ObjectName(domain + ":target_instance=" + ObjectName.quote(instance)); } - private InstanceTelemetry createJmxBean() { + private InstanceTelemetry createInstanceTelemetryBean() { mbs = ManagementFactory.getPlatformMBeanServer(); InstanceTelemetry bean = new InstanceTelemetry(); log.debug("Created jmx bean for instance: " + this.getCheckName()); @@ -295,13 +295,22 @@ private InstanceTelemetry createJmxBean() { try { instanceTelemetryBeanName = getObjName(appConfig.getJmxfetchTelemetryDomain(), this.getName()); - mbs.registerMBean(bean,instanceTelemetryBeanName); - log.debug("Succesfully registered jmx bean for instance: " + this.getCheckName() - + " with ObjectName = " + instanceTelemetryBeanName); + } catch (MalformedObjectNameException e) { + log.warn( + "Could not construct bean name for jmxfetch_telemetry_domain '{}' and name '{}'", + appConfig.getJmxfetchTelemetryDomain(), this.getName()); + return bean; + } - } catch (MalformedObjectNameException | InstanceAlreadyExistsException - | MBeanRegistrationException | NotCompliantMBeanException e) { - log.warn("Could not register bean for instance: " + this.getCheckName(),e); + try { + mbs.registerMBean(bean,instanceTelemetryBeanName); + log.debug("Succesfully registered jmx bean for instance {} with ObjectName = {}", + this.getName(), instanceTelemetryBeanName); + } catch (InstanceAlreadyExistsException + | MBeanRegistrationException + | NotCompliantMBeanException e) { + log.warn("Could not register bean named '{}' for instance: ", + instanceTelemetryBeanName.toString(), e); } return bean;