001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.actuate.metrics.amqp;
018
019import java.util.Collections;
020
021import com.rabbitmq.client.ConnectionFactory;
022import com.rabbitmq.client.impl.MicrometerMetricsCollector;
023import io.micrometer.core.instrument.MeterRegistry;
024import io.micrometer.core.instrument.Tag;
025import io.micrometer.core.instrument.binder.MeterBinder;
026
027import org.springframework.util.Assert;
028
029/**
030 * A {@link MeterBinder} for RabbitMQ Java Client metrics.
031 *
032 * @author Arnaud Cogoluègnes
033 * @author Stephane Nicoll
034 * @since 2.0.0
035 */
036public class RabbitMetrics implements MeterBinder {
037
038        private final Iterable<Tag> tags;
039
040        private final ConnectionFactory connectionFactory;
041
042        /**
043         * Create a new meter binder recording the specified {@link ConnectionFactory}.
044         * @param connectionFactory the {@link ConnectionFactory} to instrument
045         * @param tags tags to apply to all recorded metrics
046         */
047        public RabbitMetrics(ConnectionFactory connectionFactory, Iterable<Tag> tags) {
048                Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
049                this.connectionFactory = connectionFactory;
050                this.tags = (tags != null) ? tags : Collections.emptyList();
051        }
052
053        @Override
054        public void bindTo(MeterRegistry registry) {
055                this.connectionFactory.setMetricsCollector(
056                                new MicrometerMetricsCollector(registry, "rabbitmq", this.tags));
057        }
058
059}