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

io.prometheus.client.dropwizard.DropwizardExports Maven / Gradle / Ivy

There is a newer version: 0.16.0
Show newest version
package io.prometheus.client.dropwizard;

import com.codahale.metrics.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.SortedMap;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Collect dropwizard metrics from a MetricRegistry.
 */
public class DropwizardExports extends io.prometheus.client.Collector {
    private MetricRegistry registry;
    private static final Logger LOGGER = Logger.getLogger(DropwizardExports.class.getName());

    /**
     * @param registry a metric registry to export in prometheus.
     */
    public DropwizardExports(MetricRegistry registry) {
        this.registry = registry;
    }

    /**
     * Export counter as prometheus counter.
     */
    List fromCounter(String name, Counter counter) {
        MetricFamilySamples.Sample sample = new MetricFamilySamples.Sample(name, new ArrayList(), new ArrayList(),
                new Long(counter.getCount()).doubleValue());
        return Arrays.asList(new MetricFamilySamples(name, Type.GAUGE, getHelpMessage(name, counter), Arrays.asList(sample)));
    }

    private static String getHelpMessage(String metricName, Metric metric){
        return String.format("Generated from dropwizard metric import (metric=%s, type=%s)",
                metricName, metric.getClass().getName());
    }

    /**
     * Export gauge as a prometheus gauge.
     */
    List fromGauge(String name, Gauge gauge) {
        Object obj = gauge.getValue();
        double value;
        if (obj instanceof Number) {
            value = ((Number) obj).doubleValue();
        } else if (obj instanceof Boolean) {
            value = ((Boolean) obj) ? 1 : 0;
        } else {
            LOGGER.log(Level.FINE, String.format("Invalid type for Gauge %s: %s", name,
                    obj.getClass().getName()));
            return new ArrayList();
        }
        MetricFamilySamples.Sample sample = new MetricFamilySamples.Sample(name,
                new ArrayList(), new ArrayList(), value);
        return Arrays.asList(new MetricFamilySamples(name, Type.GAUGE, getHelpMessage(name, gauge), Arrays.asList(sample)));
    }

    /**
     * Export a histogram snapshot as a prometheus SUMMARY.
     *
     * @param name metric name.
     * @param snapshot the histogram snapshot.
     * @param count the total sample count for this snapshot.
     * @param factor a factor to apply to histogram values.
     *
     */
    List fromSnapshotAndCount(String name, Snapshot snapshot, long count, double factor, String helpMessage) {
        long sum = 0;
        for (long i : snapshot.getValues()) {
            sum += i;
        }
        List samples = Arrays.asList(
                new MetricFamilySamples.Sample(name, Arrays.asList("quantile"), Arrays.asList("0.5"), snapshot.getMedian() * factor),
                new MetricFamilySamples.Sample(name, Arrays.asList("quantile"), Arrays.asList("0.75"), snapshot.get75thPercentile() * factor),
                new MetricFamilySamples.Sample(name, Arrays.asList("quantile"), Arrays.asList("0.95"), snapshot.get95thPercentile() * factor),
                new MetricFamilySamples.Sample(name, Arrays.asList("quantile"), Arrays.asList("0.98"), snapshot.get98thPercentile() * factor),
                new MetricFamilySamples.Sample(name, Arrays.asList("quantile"), Arrays.asList("0.99"), snapshot.get99thPercentile() * factor),
                new MetricFamilySamples.Sample(name, Arrays.asList("quantile"), Arrays.asList("0.999"), snapshot.get999thPercentile() * factor),
                new MetricFamilySamples.Sample(name + "_count", new ArrayList(), new ArrayList(), count),
                new MetricFamilySamples.Sample(name + "_sum", new ArrayList(), new ArrayList(), sum * factor)
        );
        return Arrays.asList(
                new MetricFamilySamples(name, Type.SUMMARY, helpMessage, samples)
        );
    }

    /**
     * Convert histogram snapshot.
     */
    List fromHistogram(String name, Histogram histogram) {
        return fromSnapshotAndCount(name, histogram.getSnapshot(), histogram.getCount(), 1.0,
                getHelpMessage(name, histogram));
    }

    /**
     * Export dropwizard Timer as a histogram. Use TIME_UNIT as time unit.
     */
    List fromTimer(String name, Timer timer) {
        return fromSnapshotAndCount(name, timer.getSnapshot(), timer.getCount(),
                1.0D / TimeUnit.SECONDS.toNanos(1L), getHelpMessage(name, timer));
    }

    /**
     * Export a Meter as as prometheus COUNTER.
     */
    List fromMeter(String name, Meter meter) {
        return Arrays.asList(
                new MetricFamilySamples(name + "_total", Type.COUNTER, getHelpMessage(name, meter),
                        Arrays.asList(new MetricFamilySamples.Sample(name + "_total",
                                new ArrayList(),
                                new ArrayList(),
                                meter.getCount())))

        );
    }

    /**
     * Replace all unsupported chars with '_'.
     *
     * @param name metric name.
     * @return the sanitized metric name.
     */
    public static String sanitizeMetricName(String name){
        return name.replaceAll("[^a-zA-Z0-9:_]", "_");
    }

    @Override
    public List collect() {
        ArrayList mfSamples = new ArrayList();
        for (SortedMap.Entry entry : registry.getGauges().entrySet()) {
            mfSamples.addAll(fromGauge(sanitizeMetricName(entry.getKey()), entry.getValue()));
        }
        for (SortedMap.Entry entry : registry.getCounters().entrySet()) {
            mfSamples.addAll(fromCounter(sanitizeMetricName(entry.getKey()), entry.getValue()));
        }
        for (SortedMap.Entry entry : registry.getHistograms().entrySet()) {
            mfSamples.addAll(fromHistogram(sanitizeMetricName(entry.getKey()), entry.getValue()));
        }
        for (SortedMap.Entry entry : registry.getTimers().entrySet()) {
            mfSamples.addAll(fromTimer(sanitizeMetricName(entry.getKey()), entry.getValue()));
        }
        for (SortedMap.Entry entry : registry.getMeters().entrySet()) {
            mfSamples.addAll(fromMeter(sanitizeMetricName(entry.getKey()), entry.getValue()));
        }
        return mfSamples;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy