001/*
002 * Copyright 2013 Atteo.
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 */
016package org.moonshine.metrics;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import javax.management.MBeanServer;
022import javax.xml.bind.annotation.XmlRootElement;
023
024import org.atteo.moonshine.TopLevelService;
025
026import com.codahale.metrics.JmxReporter;
027import com.codahale.metrics.MetricRegistry;
028import com.google.inject.AbstractModule;
029import com.google.inject.Binding;
030import com.google.inject.Inject;
031import com.google.inject.Module;
032import com.google.inject.spi.DefaultElementVisitor;
033import com.google.inject.spi.Element;
034import com.google.inject.spi.Elements;
035import com.palominolabs.metrics.guice.InstrumentationModule;
036
037/**
038 * Metrics.
039 *
040 * <p>
041 * Binds {@link MetricRegistry} which you can use to register your metrics
042 * which then will be accessible through the JMX.
043 *
044 * Example:
045 * <pre>
046 * &#064;Inject
047 * private MetricRegistry metrics;
048 *
049 * ...
050 *
051 * final String metricName = MetricRegistry.name(SomeClass.class, "constant");
052 * metrics.register(metricName, new Gauge<Long>() {
053 *     &#064;Override
054 *     public Long getValue() {
055 *         return 3l;
056 *     }
057 * });
058 * </pre>
059 * </p>
060 */
061@XmlRootElement(name = "metrics")
062public class Metrics extends TopLevelService {
063    @Override
064    public Module configure() {
065        return new AbstractModule() {
066            @Override
067            protected void configure() {
068                // filter out JmxReporter from InstrumentationModule
069                final List<Element> elements = new ArrayList<>();
070                for (Element element : Elements.getElements(new InstrumentationModule())) {
071                    element.acceptVisitor(new DefaultElementVisitor<Void>() {
072                        @Override
073                        public <T> Void visit(Binding<T> binding) {
074                            if (binding.getKey().getTypeLiteral().getRawType() != JmxReporter.class) {
075                                elements.add(binding);
076                            }
077                            return super.visit(binding);
078                        }
079
080                        @Override
081                        protected Void visitOther(Element element) {
082                            elements.add(element);
083                            return super.visitOther(element);
084                        }
085                    });
086                }
087                install(Elements.getModule(elements));
088            }
089        };
090    }
091
092    @Inject
093    private MetricRegistry registry;
094
095    @Inject(optional = true)
096    private MBeanServer mbeanServer;
097
098    private JmxReporter reporter = null;
099
100    @Override
101    public void start() {
102        if (mbeanServer != null) {
103            reporter = JmxReporter.forRegistry(registry).registerWith(mbeanServer).build();
104            reporter.start();
105        }
106    }
107
108    @Override
109    public void stop() {
110        if (mbeanServer != null) {
111            reporter.stop();
112            reporter = null;
113        }
114    }
115}