forked from micrometer-metrics/micrometer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Escape/Unescape JMS String properties in contexts
Prior to this commit, both `JmsPublishObservationContext` and `JmsProcessObservationContext` would set propagation headers as String properties on the JMS `Message`. This would cause issues with JMS implementations, as the JMS specification only allows valid Java identifiers as keys. Typically, a `X-B3-TraceId` header fails this requirement as it contains "-" characters. This commit ensures that the following characters are escaped when publishing messages, and unescaped when receiving messages: * "-" becomes "_HYPHEN_" * "." becomes "_DOT_" Closes micrometer-metricsgh-4202
- Loading branch information
Showing
4 changed files
with
129 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...rc/test/java/io/micrometer/jakarta9/instrument/jms/JmsProcessObservationContextTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> argumentCaptor = ArgumentCaptor.forClass(String.class); | ||
verify(sendMessage).getStringProperty(argumentCaptor.capture()); | ||
|
||
assertThat(argumentCaptor.getValue()).isEqualTo(escaped); | ||
} | ||
|
||
static Stream<Arguments> headerValues() { | ||
return Stream.of(Arguments.of("valid", "valid"), Arguments.of("with-hyphen", "with_HYPHEN_hyphen"), | ||
Arguments.of("with.dot", "with_DOT_dot")); | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
...rc/test/java/io/micrometer/jakarta9/instrument/jms/JmsPublishObservationContextTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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.*; | ||
|
||
/** | ||
* Tests for {@link JmsPublishObservationContext}. | ||
* | ||
* @author Brian Clozel | ||
*/ | ||
class JmsPublishObservationContextTests { | ||
|
||
@ParameterizedTest | ||
@MethodSource("headerValues") | ||
void propagatorShouldEscapeHeader(String value, String escaped) throws Exception { | ||
|
||
Message sendMessage = mock(Message.class); | ||
JmsPublishObservationContext context = new JmsPublishObservationContext(sendMessage); | ||
context.getSetter().set(sendMessage, value, "testValue"); | ||
|
||
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class); | ||
verify(sendMessage).setStringProperty(argumentCaptor.capture(), anyString()); | ||
|
||
assertThat(argumentCaptor.getValue()).isEqualTo(escaped); | ||
} | ||
|
||
static Stream<Arguments> headerValues() { | ||
return Stream.of(Arguments.of("valid", "valid"), Arguments.of("with-hyphen", "with_HYPHEN_hyphen"), | ||
Arguments.of("with.dot", "with_DOT_dot")); | ||
} | ||
|
||
} |