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..4353699ccc 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 @@ -26,7 +26,10 @@ * messages}. *
* The inbound tracing information is propagated by looking it up in
- * {@link Message#getStringProperty(String) incoming JMS message headers}.
+ * {@link Message#getStringProperty(String) incoming JMS message headers}. As the JMS spec
+ * only allows valid Java identifiers as String properties, we must escape "-" and "."
+ * characters, assuming that they are escaped as {@code "_HYPHEN_"} and {@code "_DOT_"} on
+ * the sending side.
*
* @author Brian Clozel
* @since 1.12.0
@@ -36,7 +39,7 @@ public class JmsProcessObservationContext extends ReceiverContext
* This propagates the tracing information with the message sent by
- * {@link Message#setStringProperty(String, String) setting a message header}.
+ * {@link Message#setStringProperty(String, String) setting a message header}. As the JMS
+ * spec only allows valid Java identifiers as String properties, we must escape "-" and
+ * "." characters as {@code "_HYPHEN_"} and {@code "_DOT_"}, assuming that they are
+ * escaped on the receiving side.
*
* @author Brian Clozel
* @since 1.12.0
@@ -37,7 +40,7 @@ public JmsPublishObservationContext(@Nullable Message sendMessage) {
super((message, key, value) -> {
try {
if (message != null) {
- message.setStringProperty(key, value);
+ message.setStringProperty(escapeKey(key), value);
}
}
catch (JMSException exc) {
@@ -47,4 +50,8 @@ public JmsPublishObservationContext(@Nullable Message sendMessage) {
setCarrier(sendMessage);
}
+ private static String escapeKey(String key) {
+ return key.replace("-", "_HYPHEN_").replace(".", "_DOT_");
+ }
+
}
diff --git a/micrometer-jakarta9/src/test/java/io/micrometer/jakarta9/instrument/jms/JmsProcessObservationContextTests.java b/micrometer-jakarta9/src/test/java/io/micrometer/jakarta9/instrument/jms/JmsProcessObservationContextTests.java
new file mode 100644
index 0000000000..9c85f6b1ab
--- /dev/null
+++ b/micrometer-jakarta9/src/test/java/io/micrometer/jakarta9/instrument/jms/JmsProcessObservationContextTests.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2002-2023 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.micrometer.jakarta9.instrument.jms;
+
+import jakarta.jms.Message;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.ArgumentCaptor;
+
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+/**
+ * Tests for {@link JmsProcessObservationContext}.
+ *
+ * @author Brian Clozel
+ */
+class JmsProcessObservationContextTests {
+
+ @ParameterizedTest
+ @MethodSource("headerValues")
+ void propagatorShouldEscapeHeader(String value, String escaped) throws Exception {
+
+ Message sendMessage = mock(Message.class);
+ JmsProcessObservationContext context = new JmsProcessObservationContext(sendMessage);
+ context.getGetter().get(sendMessage, value);
+
+ ArgumentCaptor