From 9eb04b4f8031eae00cea1d55a97c89d952e24474 Mon Sep 17 00:00:00 2001 From: Olivier Vielpeau Date: Fri, 1 Mar 2019 12:04:21 +0100 Subject: [PATCH] Only query the JMX value for alias if `$value` is configured (#213) Querying the value potentially has to query the remote JMX, so only do it when necessary. --- .../org/datadog/jmxfetch/JmxAttribute.java | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/JmxAttribute.java b/src/main/java/org/datadog/jmxfetch/JmxAttribute.java index 15d41bae5..b433589a8 100644 --- a/src/main/java/org/datadog/jmxfetch/JmxAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JmxAttribute.java @@ -258,6 +258,7 @@ public int getMetricsCount() { } } + /** Gets the JMX Attribute info value. Makes a call through the connection */ Object getJmxValue() throws AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException, IOException { @@ -545,14 +546,18 @@ private String getUserAlias( // Attribute & domain alias = alias.replace("$attribute", fullAttributeName); alias = alias.replace("$domain", domain); - try { - alias = alias.replace("$value", getJmxValue().toString()); - } catch (JMException e) { - // If we haven't been able to get the value, it wasn't replaced. - LOGGER.warn("Unable to replace $value for attribute " + fullAttributeName, e); - } catch (IOException e) { - // Same as above - LOGGER.warn("Unable to replace $value for attribute " + fullAttributeName, e); + if (alias.contains("$value")) { + // getJmxValue() hits the JMX target (potentially through remote network connection), + // so only call it if needed. + try { + alias = alias.replace("$value", getJmxValue().toString()); + } catch (JMException e) { + // If we haven't been able to get the value, it wasn't replaced. + LOGGER.warn("Unable to replace $value for attribute " + fullAttributeName, e); + } catch (IOException e) { + // Same as above + LOGGER.warn("Unable to replace $value for attribute " + fullAttributeName, e); + } } return alias;