001/*
002 * (C) Copyright 2020 Nuxeo (http://nuxeo.com/) and others.
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 * Contributors:
017 *     bdelbosc
018 */
019package org.nuxeo.runtime.metrics.reporter;
020
021import java.net.InetSocketAddress;
022import java.util.Map;
023import java.util.Set;
024import java.util.concurrent.TimeUnit;
025
026import org.apache.logging.log4j.LogManager;
027import org.apache.logging.log4j.Logger;
028import org.nuxeo.runtime.metrics.AbstractMetricsReporter;
029import org.nuxeo.runtime.metrics.reporter.patch.NuxeoGraphiteReporter;
030
031import io.dropwizard.metrics5.MetricAttribute;
032import io.dropwizard.metrics5.MetricFilter;
033import io.dropwizard.metrics5.MetricRegistry;
034import io.dropwizard.metrics5.graphite.Graphite;
035import io.dropwizard.metrics5.graphite.GraphiteSender;
036import io.dropwizard.metrics5.graphite.GraphiteUDP;
037
038/**
039 * Reports metrics to Graphite.
040 *
041 * @since 11.1
042 */
043public class GraphiteReporter extends AbstractMetricsReporter {
044
045    private static final Logger log = LogManager.getLogger(GraphiteReporter.class);
046
047    protected static final Integer DEFAULT_PORT = 2003;
048
049    protected static final String DEFAULT_PREFIX = "servers.${hostname}.nuxeo";
050
051    protected InetSocketAddress address;
052
053    protected NuxeoGraphiteReporter reporter;
054
055    protected String prefix;
056
057    @Override
058    public void init(long pollInterval, Map<String, String> options) {
059        super.init(pollInterval, options);
060        String host = requireOption("host");
061        int port = getOptionAsInt("port", DEFAULT_PORT);
062        address = new InetSocketAddress(host, port);
063        prefix = getPrefix();
064    }
065
066    @Override
067    public void start(MetricRegistry registry, MetricFilter filter, Set<MetricAttribute> deniedExpansions) {
068        GraphiteSender graphite;
069        if (getOptionAsBoolean("udp", false)) {
070            log.warn("Connecting to graphite in UDP {} reporting every {}s with prefix: {}", address, pollInterval,
071                    prefix);
072            graphite = new GraphiteUDP(address);
073        } else {
074            log.warn("Connecting to graphite {} reporting every {}s with prefix: {}", address, pollInterval, prefix);
075            graphite = new Graphite(address);
076        }
077        reporter = new NuxeoGraphiteReporter(registry, filter,
078                io.dropwizard.metrics5.graphite.GraphiteReporter.forRegistry(registry)
079                                                                .convertRatesTo(TimeUnit.SECONDS)
080                                                                .convertDurationsTo(TimeUnit.MICROSECONDS)
081                                                                .prefixedWith(prefix)
082                                                                .filter(filter)
083                                                                .disabledMetricAttributes(deniedExpansions)
084                                                                .build(graphite));
085        reporter.start(getPollInterval(), TimeUnit.SECONDS);
086    }
087
088    @Override
089    public void stop() {
090        log.debug("Stop reporting");
091        reporter.stop();
092    }
093
094    protected String getPrefix() {
095        prefix = getOption("prefix", DEFAULT_PREFIX);
096        return prefix.replace("${hostname}", getCurrentHostname());
097    }
098
099}