Polish "Add auto-configuration for RabbitMQ metrics"
Closes gh-10887
This commit is contained in:
@@ -24,30 +24,42 @@ import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.Tag;
|
||||
import io.micrometer.core.instrument.binder.MeterBinder;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A {@link MeterBinder} for RabbitMQ Java Client metrics.
|
||||
*
|
||||
* @author Arnaud Cogoluègnes
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class RabbitMetrics implements MeterBinder {
|
||||
|
||||
private final Iterable<Tag> tags;
|
||||
|
||||
private final String name;
|
||||
|
||||
private final ConnectionFactory connectionFactory;
|
||||
|
||||
public RabbitMetrics(ConnectionFactory connectionFactory) {
|
||||
this(connectionFactory, Collections.emptyList());
|
||||
}
|
||||
|
||||
public RabbitMetrics(ConnectionFactory connectionFactory, Iterable<Tag> tags) {
|
||||
/**
|
||||
* Create a new meter binder recording the specified {@link ConnectionFactory}.
|
||||
* @param connectionFactory the {@link ConnectionFactory} to instrument
|
||||
* @param name the name prefix of the metrics
|
||||
* @param tags tags to apply to all recorded metrics
|
||||
*/
|
||||
public RabbitMetrics(ConnectionFactory connectionFactory, String name,
|
||||
Iterable<Tag> tags) {
|
||||
Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
|
||||
Assert.notNull(name, "Name must not be null");
|
||||
this.connectionFactory = connectionFactory;
|
||||
this.tags = tags;
|
||||
this.name = name;
|
||||
this.tags = (tags != null ? tags : Collections.EMPTY_LIST);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bindTo(MeterRegistry registry) {
|
||||
this.connectionFactory.setMetricsCollector(new MicrometerMetricsCollector(registry, "rabbitmq", this.tags));
|
||||
this.connectionFactory.setMetricsCollector(new MicrometerMetricsCollector(
|
||||
registry, this.name, this.tags));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2012-2018 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
|
||||
*
|
||||
* http://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.metrics.amqp;
|
||||
|
||||
import com.rabbitmq.client.ConnectionFactory;
|
||||
import io.micrometer.core.instrument.Tags;
|
||||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link RabbitMetrics}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class RabbitMetricsTests {
|
||||
|
||||
@Test
|
||||
public void connectionFactoryIsInstrumented() {
|
||||
ConnectionFactory connectionFactory = mockConnectionFactory();
|
||||
SimpleMeterRegistry registry = new SimpleMeterRegistry();
|
||||
new RabbitMetrics(connectionFactory, "rabbit", null).bindTo(registry);
|
||||
assertThat(registry.find("rabbit.connections").meter()).isPresent();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void connectionFactoryWithTagsIsInstrumented() {
|
||||
ConnectionFactory connectionFactory = mockConnectionFactory();
|
||||
SimpleMeterRegistry registry = new SimpleMeterRegistry();
|
||||
new RabbitMetrics(connectionFactory, "test", Tags.zip("env", "prod"))
|
||||
.bindTo(registry);
|
||||
assertThat(registry.find("test.connections")
|
||||
.tags("env", "prod").meter()).isPresent();
|
||||
assertThat(registry.find("test.connections")
|
||||
.tags("env", "dev").meter()).isNotPresent();
|
||||
}
|
||||
|
||||
private ConnectionFactory mockConnectionFactory() {
|
||||
return mock(ConnectionFactory.class);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user