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();
}
}
Aggregations