Skip to content

Commit

Permalink
Guard for Micrometer Core usage in JMS instrumentation
Browse files Browse the repository at this point in the history
This commit ensures that the Observability instrumentation for
`@JmsListener` does not assume that micrometer-core is on the classpath
and guards against direct usage.

Closes gh-30335
  • Loading branch information
bclozel committed Aug 14, 2023
1 parent 20d27e4 commit 94fe1f4
Showing 1 changed file with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.springframework.jms.support.QosSettings;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.ErrorHandler;

/**
Expand Down Expand Up @@ -149,7 +150,8 @@
public abstract class AbstractMessageListenerContainer extends AbstractJmsListeningContainer
implements MessageListenerContainer {

private static final JmsProcessObservationConvention DEFAULT_CONVENTION = new DefaultJmsProcessObservationConvention();
private static final boolean micrometerCorePresent = ClassUtils.isPresent(
"io.micrometer.core.instrument.binder.jms.JmsInstrumentation", AbstractMessageListenerContainer.class.getClassLoader());

@Nullable
private volatile Object destination;
Expand Down Expand Up @@ -703,8 +705,7 @@ protected void doExecuteListener(Session session, Message message) throws JMSExc
}

try {
Observation observation = JmsObservationDocumentation.JMS_MESSAGE_PROCESS
.observation(null, DEFAULT_CONVENTION, () -> new JmsProcessObservationContext(message), this.observationRegistry);
Observation observation = createObservation(message);
observation.observeChecked(() -> invokeListener(session, message));
}
catch (JMSException | RuntimeException | Error ex) {
Expand All @@ -714,6 +715,15 @@ protected void doExecuteListener(Session session, Message message) throws JMSExc
commitIfNecessary(session, message);
}

private Observation createObservation(Message message) {
if (micrometerCorePresent) {
return ObservationFactory.create(this.observationRegistry, message);
}
else {
return Observation.NOOP;
}
}

/**
* Invoke the specified listener: either as standard JMS MessageListener
* or (preferably) as Spring SessionAwareMessageListener.
Expand Down Expand Up @@ -758,8 +768,7 @@ protected void doInvokeListener(SessionAwareMessageListener listener, Session se

Connection conToClose = null;
Session sessionToClose = null;
Observation observation = JmsObservationDocumentation.JMS_MESSAGE_PROCESS
.observation(null, DEFAULT_CONVENTION, () -> new JmsProcessObservationContext(message), this.observationRegistry);
Observation observation = createObservation(message);
try {
Session sessionToUse = session;
if (!isExposeListenerSession()) {
Expand Down Expand Up @@ -982,4 +991,14 @@ protected void invokeErrorHandler(Throwable ex) {
private static class MessageRejectedWhileStoppingException extends RuntimeException {
}

private static abstract class ObservationFactory {

private static final JmsProcessObservationConvention DEFAULT_CONVENTION = new DefaultJmsProcessObservationConvention();

static Observation create(@Nullable ObservationRegistry registry, Message message) {
return JmsObservationDocumentation.JMS_MESSAGE_PROCESS
.observation(null, DEFAULT_CONVENTION, () -> new JmsProcessObservationContext(message), registry);
}
}

}

0 comments on commit 94fe1f4

Please sign in to comment.