From be6f96225e92267b5c62a93919fa1d13c7bfeb11 Mon Sep 17 00:00:00 2001 From: cfredri4 Date: Thu, 19 Dec 2024 08:48:42 +0100 Subject: [PATCH] Handle all exceptions when getting/setting JMS headers Some JMS providers will throw RuntimeException instead of JMSException when failing to get/set JMS headers. Closes gh-5746 Co-authored-by: Jonatan Ivanov --- .../jms/JmsProcessObservationContext.java | 8 ++++++-- .../jms/JmsPublishObservationContext.java | 15 +++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/micrometer-jakarta9/src/main/java/io/micrometer/jakarta9/instrument/jms/JmsProcessObservationContext.java b/micrometer-jakarta9/src/main/java/io/micrometer/jakarta9/instrument/jms/JmsProcessObservationContext.java index 4d8d29a892..3ded504ec1 100644 --- a/micrometer-jakarta9/src/main/java/io/micrometer/jakarta9/instrument/jms/JmsProcessObservationContext.java +++ b/micrometer-jakarta9/src/main/java/io/micrometer/jakarta9/instrument/jms/JmsProcessObservationContext.java @@ -16,8 +16,8 @@ package io.micrometer.jakarta9.instrument.jms; +import io.micrometer.common.util.internal.logging.WarnThenDebugLogger; import io.micrometer.observation.transport.ReceiverContext; -import jakarta.jms.JMSException; import jakarta.jms.Message; /** @@ -33,12 +33,16 @@ */ public class JmsProcessObservationContext extends ReceiverContext { + private static final WarnThenDebugLogger logger = new WarnThenDebugLogger(JmsProcessObservationContext.class); + public JmsProcessObservationContext(Message receivedMessage) { super((message, key) -> { try { return message.getStringProperty(key); } - catch (JMSException exc) { + // Some JMS providers throw exceptions other than JMSException + catch (Exception exc) { + logger.log("Failed to get message property.", exc); return null; } }); diff --git a/micrometer-jakarta9/src/main/java/io/micrometer/jakarta9/instrument/jms/JmsPublishObservationContext.java b/micrometer-jakarta9/src/main/java/io/micrometer/jakarta9/instrument/jms/JmsPublishObservationContext.java index f16d2154d9..5883c08c62 100644 --- a/micrometer-jakarta9/src/main/java/io/micrometer/jakarta9/instrument/jms/JmsPublishObservationContext.java +++ b/micrometer-jakarta9/src/main/java/io/micrometer/jakarta9/instrument/jms/JmsPublishObservationContext.java @@ -17,8 +17,8 @@ package io.micrometer.jakarta9.instrument.jms; import io.micrometer.common.lang.Nullable; +import io.micrometer.common.util.internal.logging.WarnThenDebugLogger; import io.micrometer.observation.transport.SenderContext; -import jakarta.jms.JMSException; import jakarta.jms.Message; /** @@ -33,15 +33,18 @@ */ public class JmsPublishObservationContext extends SenderContext { + private static final WarnThenDebugLogger logger = new WarnThenDebugLogger(JmsPublishObservationContext.class); + public JmsPublishObservationContext(@Nullable Message sendMessage) { super((message, key, value) -> { - try { - if (message != null) { + if (message != null) { + try { message.setStringProperty(key, value); } - } - catch (JMSException exc) { - // ignore + // Some JMS providers throw exceptions other than JMSException + catch (Exception exc) { + logger.log("Failed to set message property.", exc); + } } }); setCarrier(sendMessage);