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) {