|
| 1 | +/* |
| 2 | + * Copyright 2012-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.autoconfigure.metrics.amqp; |
| 18 | + |
| 19 | +import com.rabbitmq.client.ConnectionFactory; |
| 20 | +import com.rabbitmq.client.MetricsCollector; |
| 21 | +import io.micrometer.core.instrument.MeterRegistry; |
| 22 | +import io.micrometer.core.instrument.Tags; |
| 23 | + |
| 24 | +import org.springframework.amqp.rabbit.connection.AbstractConnectionFactory; |
| 25 | +import org.springframework.beans.factory.config.BeanPostProcessor; |
| 26 | +import org.springframework.boot.actuate.metrics.amqp.RabbitMetrics; |
| 27 | +import org.springframework.context.ApplicationContext; |
| 28 | +import org.springframework.core.Ordered; |
| 29 | +import org.springframework.util.StringUtils; |
| 30 | + |
| 31 | +/** |
| 32 | + * {@link BeanPostProcessor} that configures RabbitMQ metrics. Such arrangement is |
| 33 | + * necessary because a connection can be eagerly created and cached without a reference |
| 34 | + * to a proper {@link MetricsCollector}. |
| 35 | + * |
| 36 | + * @author Stephane Nicoll |
| 37 | + */ |
| 38 | +public class RabbitConnectionFactoryMetricsPostProcessor |
| 39 | + implements BeanPostProcessor, Ordered { |
| 40 | + |
| 41 | + private static final String CONNECTION_FACTORY_SUFFIX = "connectionFactory"; |
| 42 | + |
| 43 | + private final ApplicationContext context; |
| 44 | + |
| 45 | + private volatile MeterRegistry meterRegistry; |
| 46 | + |
| 47 | + RabbitConnectionFactoryMetricsPostProcessor(ApplicationContext context) { |
| 48 | + this.context = context; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Object postProcessAfterInitialization(Object bean, String beanName) { |
| 53 | + if (bean instanceof AbstractConnectionFactory) { |
| 54 | + bindConnectionFactoryToRegistry(getMeterRegistry(), beanName, |
| 55 | + (AbstractConnectionFactory) bean); |
| 56 | + } |
| 57 | + return bean; |
| 58 | + } |
| 59 | + |
| 60 | + private void bindConnectionFactoryToRegistry(MeterRegistry registry, String beanName, |
| 61 | + AbstractConnectionFactory connectionFactory) { |
| 62 | + ConnectionFactory rabbitConnectionFactory = connectionFactory |
| 63 | + .getRabbitConnectionFactory(); |
| 64 | + String connectionFactoryName = getConnectionFactoryName(beanName); |
| 65 | + new RabbitMetrics(rabbitConnectionFactory, Tags.of("name", connectionFactoryName)) |
| 66 | + .bindTo(registry); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Get the name of a ConnectionFactory based on its {@code beanName}. |
| 71 | + * @param beanName the name of the connection factory bean |
| 72 | + * @return a name for the given connection factory |
| 73 | + */ |
| 74 | + private String getConnectionFactoryName(String beanName) { |
| 75 | + if (beanName.length() > CONNECTION_FACTORY_SUFFIX.length() |
| 76 | + && StringUtils.endsWithIgnoreCase(beanName, CONNECTION_FACTORY_SUFFIX)) { |
| 77 | + return beanName.substring(0, |
| 78 | + beanName.length() - CONNECTION_FACTORY_SUFFIX.length()); |
| 79 | + } |
| 80 | + return beanName; |
| 81 | + } |
| 82 | + |
| 83 | + private MeterRegistry getMeterRegistry() { |
| 84 | + if (this.meterRegistry == null) { |
| 85 | + this.meterRegistry = this.context.getBean(MeterRegistry.class); |
| 86 | + } |
| 87 | + return this.meterRegistry; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public int getOrder() { |
| 92 | + return Ordered.HIGHEST_PRECEDENCE; |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments