All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.flink.dropwizard.ScheduledDropwizardReporter Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.flink.dropwizard;

import org.apache.flink.annotation.PublicEvolving;
import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.dropwizard.metrics.DropwizardHistogramWrapper;
import org.apache.flink.dropwizard.metrics.DropwizardMeterWrapper;
import org.apache.flink.dropwizard.metrics.FlinkCounterWrapper;
import org.apache.flink.dropwizard.metrics.FlinkGaugeWrapper;
import org.apache.flink.dropwizard.metrics.FlinkHistogramWrapper;
import org.apache.flink.dropwizard.metrics.FlinkMeterWrapper;
import org.apache.flink.metrics.CharacterFilter;
import org.apache.flink.metrics.Counter;
import org.apache.flink.metrics.Gauge;
import org.apache.flink.metrics.Histogram;
import org.apache.flink.metrics.Meter;
import org.apache.flink.metrics.Metric;
import org.apache.flink.metrics.MetricConfig;
import org.apache.flink.metrics.MetricGroup;
import org.apache.flink.metrics.reporter.MetricReporter;
import org.apache.flink.metrics.reporter.Scheduled;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Reporter;
import com.codahale.metrics.ScheduledReporter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;

/**
 * Base class for {@link org.apache.flink.metrics.reporter.MetricReporter} that wraps a Dropwizard
 * {@link com.codahale.metrics.Reporter}.
 */
@PublicEvolving
public abstract class ScheduledDropwizardReporter
        implements MetricReporter, Scheduled, Reporter, CharacterFilter {

    protected final Logger log = LoggerFactory.getLogger(getClass());

    public static final String ARG_HOST = "host";
    public static final String ARG_PORT = "port";
    public static final String ARG_PREFIX = "prefix";
    public static final String ARG_CONVERSION_RATE = "rateConversion";
    public static final String ARG_CONVERSION_DURATION = "durationConversion";

    // ------------------------------------------------------------------------

    protected final MetricRegistry registry;

    protected ScheduledReporter reporter;

    private final Map, String> gauges = new HashMap<>();
    private final Map counters = new HashMap<>();
    private final Map histograms = new HashMap<>();
    private final Map meters = new HashMap<>();

    // ------------------------------------------------------------------------

    protected ScheduledDropwizardReporter() {
        this.registry = new MetricRegistry();
    }

    // ------------------------------------------------------------------------
    //  Getters
    // ------------------------------------------------------------------------

    @VisibleForTesting
    Map getCounters() {
        return counters;
    }

    @VisibleForTesting
    Map getMeters() {
        return meters;
    }

    @VisibleForTesting
    Map, String> getGauges() {
        return gauges;
    }

    @VisibleForTesting
    Map getHistograms() {
        return histograms;
    }

    // ------------------------------------------------------------------------
    //  life cycle
    // ------------------------------------------------------------------------

    @Override
    public void open(MetricConfig config) {
        this.reporter = getReporter(config);
    }

    @Override
    public void close() {
        this.reporter.stop();
    }

    // ------------------------------------------------------------------------
    //  adding / removing metrics
    // ------------------------------------------------------------------------

    @Override
    public void notifyOfAddedMetric(Metric metric, String metricName, MetricGroup group) {
        final String fullName = group.getMetricIdentifier(metricName, this);

        synchronized (this) {
            switch (metric.getMetricType()) {
                case COUNTER:
                    counters.put((Counter) metric, fullName);
                    registry.register(fullName, new FlinkCounterWrapper((Counter) metric));
                    break;
                case GAUGE:
                    gauges.put((Gauge) metric, fullName);
                    registry.register(fullName, FlinkGaugeWrapper.fromGauge((Gauge) metric));
                    break;
                case HISTOGRAM:
                    Histogram histogram = (Histogram) metric;
                    histograms.put(histogram, fullName);

                    if (histogram instanceof DropwizardHistogramWrapper) {
                        registry.register(
                                fullName,
                                ((DropwizardHistogramWrapper) histogram).getDropwizardHistogram());
                    } else {
                        registry.register(fullName, new FlinkHistogramWrapper(histogram));
                    }
                    break;
                case METER:
                    Meter meter = (Meter) metric;
                    meters.put(meter, fullName);

                    if (meter instanceof DropwizardMeterWrapper) {
                        registry.register(
                                fullName, ((DropwizardMeterWrapper) meter).getDropwizardMeter());
                    } else {
                        registry.register(fullName, new FlinkMeterWrapper(meter));
                    }
                    break;
                default:
                    log.warn(
                            "Cannot add metric of type {}. This indicates that the reporter "
                                    + "does not support this metric type.",
                            metric.getClass().getName());
            }
        }
    }

    @Override
    public void notifyOfRemovedMetric(Metric metric, String metricName, MetricGroup group) {
        synchronized (this) {
            String fullName;

            switch (metric.getMetricType()) {
                case COUNTER:
                    fullName = counters.remove(metric);
                    break;
                case GAUGE:
                    fullName = gauges.remove(metric);
                    break;
                case HISTOGRAM:
                    fullName = histograms.remove(metric);
                    break;
                case METER:
                    fullName = meters.remove(metric);
                    break;
                default:
                    fullName = null;
            }

            if (fullName != null) {
                registry.remove(fullName);
            }
        }
    }

    @Override
    public String filterCharacters(String metricName) {
        char[] chars = null;
        final int strLen = metricName.length();
        int pos = 0;

        for (int i = 0; i < strLen; i++) {
            final char c = metricName.charAt(i);
            switch (c) {
                case '.':
                    if (chars == null) {
                        chars = metricName.toCharArray();
                    }
                    chars[pos++] = '-';
                    break;
                case '"':
                    if (chars == null) {
                        chars = metricName.toCharArray();
                    }
                    break;

                default:
                    if (chars != null) {
                        chars[pos] = c;
                    }
                    pos++;
            }
        }

        return chars == null ? metricName : new String(chars, 0, pos);
    }

    // ------------------------------------------------------------------------
    //  scheduled reporting
    // ------------------------------------------------------------------------

    @Override
    public void report() {
        // we do not need to lock here, because the dropwizard registry is
        // internally a concurrent map
        @SuppressWarnings("rawtypes")
        final SortedMap gauges = registry.getGauges();
        final SortedMap counters = registry.getCounters();
        final SortedMap histograms =
                registry.getHistograms();
        final SortedMap meters = registry.getMeters();
        final SortedMap timers = registry.getTimers();

        this.reporter.report(gauges, counters, histograms, meters, timers);
    }

    public abstract ScheduledReporter getReporter(MetricConfig config);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy