Auto-configure ObservationRegistry on JmsTemplate
Spring Boot auto-configures both a `JmsTemplate` and a `JmsMessagingTemplate`. As of Spring Framework 6.2, JMS has observability support when publishing messages. This commit creates a bean post-processor that configures an `ObservationRegistry` on the template, if the registry is present. Closes gh-37388pull/37434/head
parent
d6daf87074
commit
0fc97e9315
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2012-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 org.springframework.boot.actuate.autoconfigure.observation.jms;
|
||||
|
||||
import io.micrometer.core.instrument.binder.jms.JmsPublishObservationContext;
|
||||
import io.micrometer.observation.Observation;
|
||||
import io.micrometer.observation.ObservationRegistry;
|
||||
import jakarta.jms.Message;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.jms.core.JmsTemplate;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for instrumenting
|
||||
* {@link JmsTemplate} beans for Observability.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @since 3.2.0
|
||||
*/
|
||||
@AutoConfiguration(after = { JmsAutoConfiguration.class, ObservationAutoConfiguration.class })
|
||||
@ConditionalOnBean({ ObservationRegistry.class, JmsTemplate.class })
|
||||
@ConditionalOnClass({ Observation.class, Message.class, JmsTemplate.class, JmsPublishObservationContext.class })
|
||||
public class JmsTemplateObservationAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
static JmsTemplateObservationPostProcessor jmsTemplateObservationPostProcessor(
|
||||
ObjectProvider<ObservationRegistry> observationRegistry) {
|
||||
return new JmsTemplateObservationPostProcessor(observationRegistry);
|
||||
}
|
||||
|
||||
static class JmsTemplateObservationPostProcessor implements BeanPostProcessor {
|
||||
|
||||
private final ObjectProvider<ObservationRegistry> observationRegistry;
|
||||
|
||||
JmsTemplateObservationPostProcessor(ObjectProvider<ObservationRegistry> observationRegistry) {
|
||||
this.observationRegistry = observationRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
if (bean instanceof JmsTemplate jmsTemplate) {
|
||||
this.observationRegistry.ifAvailable(jmsTemplate::setObservationRegistry);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright 2012-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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Auto-configuration for JMS observations.
|
||||
*/
|
||||
package org.springframework.boot.actuate.autoconfigure.observation.jms;
|
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2012-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 org.springframework.boot.actuate.autoconfigure.observation.jms;
|
||||
|
||||
import jakarta.jms.ConnectionFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration;
|
||||
import org.springframework.boot.test.context.FilteredClassLoader;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.jms.core.JmsTemplate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link JmsTemplateObservationAutoConfiguration}.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
*/
|
||||
class JmsTemplateObservationAutoConfigurationTests {
|
||||
|
||||
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(JmsAutoConfiguration.class, ObservationAutoConfiguration.class,
|
||||
JmsTemplateObservationAutoConfiguration.class))
|
||||
.withUserConfiguration(JmsConnectionConfiguration.class);
|
||||
|
||||
@Test
|
||||
void shouldConfigureObservationRegistryOnTemplate() {
|
||||
this.contextRunner.run((context) -> {
|
||||
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
|
||||
assertThat(jmsTemplate).extracting("observationRegistry").isNotNull();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldBackOffWhenMircrometerCoreIsNotPresent() {
|
||||
this.contextRunner.withClassLoader(new FilteredClassLoader("io.micrometer.core")).run((context) -> {
|
||||
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
|
||||
assertThat(jmsTemplate).extracting("observationRegistry").isNull();
|
||||
});
|
||||
}
|
||||
|
||||
static class JmsConnectionConfiguration {
|
||||
|
||||
@Bean
|
||||
ConnectionFactory connectionFactory() {
|
||||
return mock(ConnectionFactory.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue