Instrument @JmsListener session for response messages
Prior to this commit, the observation instrumentation for `@JmsListener` annotated methods (implemented in `AbstractMessageListenerContainer` would not instrument the JMS session using the Micrometer JMS support. This means that response messages returned from the listener method would be sent but no observation would be recorded. As a result, tracing message properties would be also missing. This commit ensures that the session provided to the listener method is instrumented beforehand, if Micrometer is on the classpath and an observation registry has been configured. Fixes gh-33221
This commit is contained in:
@@ -23,7 +23,9 @@ import java.util.stream.Stream;
|
||||
|
||||
import io.micrometer.observation.Observation;
|
||||
import io.micrometer.observation.tck.TestObservationRegistry;
|
||||
import jakarta.jms.Message;
|
||||
import jakarta.jms.MessageListener;
|
||||
import jakarta.jms.TextMessage;
|
||||
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
|
||||
import org.apache.activemq.artemis.junit.EmbeddedActiveMQExtension;
|
||||
import org.assertj.core.api.Assertions;
|
||||
@@ -81,6 +83,33 @@ class MessageListenerContainerObservationTests {
|
||||
listenerContainer.shutdown();
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "[{index}] {0}")
|
||||
@MethodSource("listenerContainers")
|
||||
void shouldRecordJmsPublishObservations(AbstractMessageListenerContainer listenerContainer) throws Exception {
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
listenerContainer.setConnectionFactory(connectionFactory);
|
||||
listenerContainer.setObservationRegistry(registry);
|
||||
listenerContainer.setDestinationName("spring.test.observation");
|
||||
listenerContainer.setMessageListener((SessionAwareMessageListener) (message, session) -> {
|
||||
Message response = session.createTextMessage("test response");
|
||||
session.createProducer(message.getJMSReplyTo()).send(response);
|
||||
});
|
||||
listenerContainer.afterPropertiesSet();
|
||||
listenerContainer.start();
|
||||
JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
|
||||
TextMessage response = (TextMessage) jmsTemplate.sendAndReceive("spring.test.observation",
|
||||
session -> session.createTextMessage("test request"));
|
||||
|
||||
// request received by listener and response received by template
|
||||
assertThat(registry).hasNumberOfObservationsWithNameEqualTo("jms.message.process", 2);
|
||||
// response sent to the template
|
||||
assertThat(registry).hasNumberOfObservationsWithNameEqualTo("jms.message.publish", 1);
|
||||
|
||||
Assertions.assertThat(response.getText()).isEqualTo("test response");
|
||||
listenerContainer.stop();
|
||||
listenerContainer.shutdown();
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "[{index}] {0}")
|
||||
@MethodSource("listenerContainers")
|
||||
void shouldHaveObservationScopeInErrorHandler(AbstractMessageListenerContainer listenerContainer) throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user