Commit bd64e052 authored by Andy Wilkinson's avatar Andy Wilkinson

Instrument AMQP AbstractCF when defined as a ConnectionFactory

Fixes gh-25138
parent 82dc2dff
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
......@@ -42,7 +42,7 @@ import org.springframework.context.annotation.Configuration;
@AutoConfigureAfter({ MetricsAutoConfiguration.class, RabbitAutoConfiguration.class,
SimpleMetricsExportAutoConfiguration.class })
@ConditionalOnClass({ ConnectionFactory.class, AbstractConnectionFactory.class })
@ConditionalOnBean({ AbstractConnectionFactory.class, MeterRegistry.class })
@ConditionalOnBean({ org.springframework.amqp.rabbit.connection.ConnectionFactory.class, MeterRegistry.class })
public class RabbitMetricsAutoConfiguration {
@Bean
......
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
......@@ -19,10 +19,14 @@ package org.springframework.boot.actuate.autoconfigure.metrics.amqp;
import io.micrometer.core.instrument.MeterRegistry;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
......@@ -45,6 +49,15 @@ class RabbitMetricsAutoConfigurationTests {
});
}
@Test
void abstractConnectionFactoryDefinedAsAConnectionFactoryIsInstrumented() {
this.contextRunner.withUserConfiguration(ConnectionFactoryConfiguration.class).run((context) -> {
assertThat(context).hasBean("customConnectionFactory");
MeterRegistry registry = context.getBean(MeterRegistry.class);
registry.get("rabbitmq.connections").meter();
});
}
@Test
void rabbitmqNativeConnectionFactoryInstrumentationCanBeDisabled() {
this.contextRunner.withPropertyValues("management.metrics.enable.rabbitmq=false").run((context) -> {
......@@ -53,4 +66,14 @@ class RabbitMetricsAutoConfigurationTests {
});
}
@Configuration
static class ConnectionFactoryConfiguration {
@Bean
ConnectionFactory customConnectionFactory() {
return new CachingConnectionFactory();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment