Search in sources :

Example 1 with Summarizable

use of com.yammer.metrics.core.Summarizable in project java by wavefrontHQ.

the class FlushProcessor method processHistogram.

@Override
public void processHistogram(MetricName name, Histogram histogram, FlushProcessorContext context) {
    if (histogram instanceof WavefrontHistogram) {
        WavefrontHistogram wavefrontHistogram = (WavefrontHistogram) histogram;
        if (useWavefrontHistograms) {
            // export Wavefront histograms in its native format
            if (wavefrontHistogram.count() == 0)
                return;
            for (WavefrontHistogram.MinuteBin bin : wavefrontHistogram.bins(true)) {
                if (bin.getDist().size() == 0)
                    continue;
                int size = bin.getDist().centroids().size();
                List<Double> centroids = new ArrayList<>(size);
                List<Integer> counts = new ArrayList<>(size);
                for (Centroid centroid : bin.getDist().centroids()) {
                    centroids.add(centroid.mean());
                    counts.add(centroid.count());
                }
                context.report(wavefront.report.Histogram.newBuilder().setDuration(// minute bins
                60_000).setType(HistogramType.TDIGEST).setBins(centroids).setCounts(counts).build(), bin.getMinMillis());
                histogramCounter.inc();
            }
        } else {
            // convert Wavefront histogram to Yammer-style histogram
            TDigest tDigest = new AVLTreeDigest(100);
            List<WavefrontHistogram.MinuteBin> bins = wavefrontHistogram.bins(true);
            bins.stream().map(WavefrontHistogram.MinuteBin::getDist).forEach(tDigest::add);
            context.reportSubMetric(tDigest.centroids().stream().mapToLong(Centroid::count).sum(), "count");
            Summarizable summarizable = new Summarizable() {

                @Override
                public double max() {
                    return tDigest.centroids().stream().map(Centroid::mean).max(Comparator.naturalOrder()).orElse(Double.NaN);
                }

                @Override
                public double min() {
                    return tDigest.centroids().stream().map(Centroid::mean).min(Comparator.naturalOrder()).orElse(Double.NaN);
                }

                @Override
                public double mean() {
                    Centroid mean = tDigest.centroids().stream().reduce((x, y) -> new Centroid(x.mean() + (y.mean() * y.count()), x.count() + y.count())).orElse(null);
                    return mean == null || tDigest.centroids().size() == 0 ? Double.NaN : mean.mean() / mean.count();
                }

                @Override
                public double stdDev() {
                    return Double.NaN;
                }

                @Override
                public double sum() {
                    return Double.NaN;
                }
            };
            for (Map.Entry<String, Double> entry : MetricsToTimeseries.explodeSummarizable(summarizable, reportEmptyHistogramStats).entrySet()) {
                if (!entry.getValue().isNaN()) {
                    context.reportSubMetric(entry.getValue(), entry.getKey());
                }
            }
            Sampling sampling = () -> new Snapshot(new double[0]) {

                @Override
                public double get75thPercentile() {
                    return tDigest.quantile(.75);
                }

                @Override
                public double get95thPercentile() {
                    return tDigest.quantile(.95);
                }

                @Override
                public double get98thPercentile() {
                    return tDigest.quantile(.98);
                }

                @Override
                public double get999thPercentile() {
                    return tDigest.quantile(.999);
                }

                @Override
                public double get99thPercentile() {
                    return tDigest.quantile(.99);
                }

                @Override
                public double getMedian() {
                    return tDigest.quantile(.50);
                }

                @Override
                public double getValue(double quantile) {
                    return tDigest.quantile(quantile);
                }

                @Override
                public double[] getValues() {
                    return new double[0];
                }

                @Override
                public int size() {
                    return (int) tDigest.size();
                }
            };
            for (Map.Entry<String, Double> entry : MetricsToTimeseries.explodeSampling(sampling, reportEmptyHistogramStats).entrySet()) {
                if (!entry.getValue().isNaN()) {
                    context.reportSubMetric(entry.getValue(), entry.getKey());
                }
            }
            sentCounter.inc();
        }
    } else {
        context.reportSubMetric(histogram.count(), "count");
        for (Map.Entry<String, Double> entry : MetricsToTimeseries.explodeSummarizable(histogram, reportEmptyHistogramStats).entrySet()) {
            if (!entry.getValue().isNaN()) {
                context.reportSubMetric(entry.getValue(), entry.getKey());
            }
        }
        for (Map.Entry<String, Double> entry : MetricsToTimeseries.explodeSampling(histogram, reportEmptyHistogramStats).entrySet()) {
            if (!entry.getValue().isNaN()) {
                context.reportSubMetric(entry.getValue(), entry.getKey());
            }
        }
        sentCounter.inc();
        histogram.clear();
    }
}
Also used : Sampling(com.yammer.metrics.core.Sampling) Counter(com.yammer.metrics.core.Counter) TDigest(com.tdunning.math.stats.TDigest) Histogram(com.yammer.metrics.core.Histogram) AVLTreeDigest(com.tdunning.math.stats.AVLTreeDigest) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) MetricProcessor(com.yammer.metrics.core.MetricProcessor) List(java.util.List) Metered(com.yammer.metrics.core.Metered) Gauge(com.yammer.metrics.core.Gauge) Map(java.util.Map) Snapshot(com.yammer.metrics.stats.Snapshot) DeltaCounter(com.yammer.metrics.core.DeltaCounter) MetricName(com.yammer.metrics.core.MetricName) Metrics(com.yammer.metrics.Metrics) WavefrontHistogram(com.yammer.metrics.core.WavefrontHistogram) Timer(com.yammer.metrics.core.Timer) Comparator(java.util.Comparator) Centroid(com.tdunning.math.stats.Centroid) Summarizable(com.yammer.metrics.core.Summarizable) HistogramType(wavefront.report.HistogramType) MetricsToTimeseries(com.wavefront.common.MetricsToTimeseries) ArrayList(java.util.ArrayList) AVLTreeDigest(com.tdunning.math.stats.AVLTreeDigest) Summarizable(com.yammer.metrics.core.Summarizable) Centroid(com.tdunning.math.stats.Centroid) Snapshot(com.yammer.metrics.stats.Snapshot) TDigest(com.tdunning.math.stats.TDigest) WavefrontHistogram(com.yammer.metrics.core.WavefrontHistogram) Sampling(com.yammer.metrics.core.Sampling) Map(java.util.Map)

Aggregations

AVLTreeDigest (com.tdunning.math.stats.AVLTreeDigest)1 Centroid (com.tdunning.math.stats.Centroid)1 TDigest (com.tdunning.math.stats.TDigest)1 MetricsToTimeseries (com.wavefront.common.MetricsToTimeseries)1 Metrics (com.yammer.metrics.Metrics)1 Counter (com.yammer.metrics.core.Counter)1 DeltaCounter (com.yammer.metrics.core.DeltaCounter)1 Gauge (com.yammer.metrics.core.Gauge)1 Histogram (com.yammer.metrics.core.Histogram)1 Metered (com.yammer.metrics.core.Metered)1 MetricName (com.yammer.metrics.core.MetricName)1 MetricProcessor (com.yammer.metrics.core.MetricProcessor)1 Sampling (com.yammer.metrics.core.Sampling)1 Summarizable (com.yammer.metrics.core.Summarizable)1 Timer (com.yammer.metrics.core.Timer)1 WavefrontHistogram (com.yammer.metrics.core.WavefrontHistogram)1 Snapshot (com.yammer.metrics.stats.Snapshot)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1