From 9d47952d4a9864607bc58bc69131358c31d23c62 Mon Sep 17 00:00:00 2001 From: Alex Kats Date: Mon, 27 Nov 2023 22:12:17 -0500 Subject: [PATCH] Add error handling for closure params --- .../contrib/jmxmetrics/InstrumentHelper.groovy | 12 ++++++++++-- .../contrib/jmxmetrics/MBeanHelper.groovy | 12 ++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/jmx-metrics/src/main/groovy/io/opentelemetry/contrib/jmxmetrics/InstrumentHelper.groovy b/jmx-metrics/src/main/groovy/io/opentelemetry/contrib/jmxmetrics/InstrumentHelper.groovy index 369a4a6b1..0f69f5aea 100644 --- a/jmx-metrics/src/main/groovy/io/opentelemetry/contrib/jmxmetrics/InstrumentHelper.groovy +++ b/jmx-metrics/src/main/groovy/io/opentelemetry/contrib/jmxmetrics/InstrumentHelper.groovy @@ -165,10 +165,18 @@ class InstrumentHelper { private static Map getLabels(GroovyMBean mbean, Map labelFuncs, Map additionalLabels) { def labels = [:] labelFuncs.each { label, labelFunc -> - labels[label] = labelFunc(mbean) as String + try { + labels[label] = labelFunc(mbean) as String + } catch(AttributeNotFoundException e) { + logger.warning("Attribute missing for label:${label}, label was not applied") + } } additionalLabels.each { label, labelFunc -> - labels[label] = labelFunc(mbean) as String + try { + labels[label] = labelFunc(mbean) as String + } catch(AttributeNotFoundException e) { + logger.warning("Attribute missing for label:${label}, label was not applied") + } } return labels } diff --git a/jmx-metrics/src/main/groovy/io/opentelemetry/contrib/jmxmetrics/MBeanHelper.groovy b/jmx-metrics/src/main/groovy/io/opentelemetry/contrib/jmxmetrics/MBeanHelper.groovy index 9cabdecf2..2c05fb484 100644 --- a/jmx-metrics/src/main/groovy/io/opentelemetry/contrib/jmxmetrics/MBeanHelper.groovy +++ b/jmx-metrics/src/main/groovy/io/opentelemetry/contrib/jmxmetrics/MBeanHelper.groovy @@ -122,8 +122,16 @@ class MBeanHelper { } Object getBeanAttributeWithTransform(GroovyMBean bean, String attribute){ - def transformationClosure = attributeTransformation.get(attribute); - return transformationClosure != null ? transformationClosure(bean) : getBeanAttribute(bean, attribute) + def transformationClosure = attributeTransformation.get(attribute); + if (transformationClosure == null) { + return getBeanAttribute(bean, attribute) + } + try { + return transformationClosure(bean) + } catch(AttributeNotFoundException e) { + logger.warning("Transformed attribute not found in ${bean.name()}") + return null + } } static Object getBeanAttribute(GroovyMBean bean, String attribute) {