Search in sources :

Example 1 with ValueAtPercentile

use of io.micrometer.core.instrument.distribution.ValueAtPercentile in project micrometer by micrometer-metrics.

the class PrometheusMeterRegistry method newDistributionSummary.

@Override
public DistributionSummary newDistributionSummary(Meter.Id id, DistributionStatisticConfig distributionStatisticConfig, double scale) {
    MicrometerCollector collector = collectorByName(id);
    PrometheusDistributionSummary summary = new PrometheusDistributionSummary(id, clock, distributionStatisticConfig, scale);
    List<String> tagValues = tagValues(id);
    collector.add((conventionName, tagKeys) -> {
        Stream.Builder<Collector.MetricFamilySamples.Sample> samples = Stream.builder();
        final ValueAtPercentile[] percentileValues = summary.takeSnapshot().percentileValues();
        final CountAtBucket[] histogramCounts = summary.histogramCounts();
        double count = summary.count();
        if (percentileValues.length > 0) {
            List<String> quantileKeys = new LinkedList<>(tagKeys);
            quantileKeys.add("quantile");
            // satisfies https://prometheus.io/docs/concepts/metric_types/#summary
            for (ValueAtPercentile v : percentileValues) {
                List<String> quantileValues = new LinkedList<>(tagValues);
                quantileValues.add(Collector.doubleToGoString(v.percentile()));
                samples.add(new Collector.MetricFamilySamples.Sample(conventionName, quantileKeys, quantileValues, v.value()));
            }
        }
        Collector.Type type = Collector.Type.SUMMARY;
        if (histogramCounts.length > 0) {
            // Prometheus doesn't balk at a metric being BOTH a histogram and a summary
            type = Collector.Type.HISTOGRAM;
            List<String> histogramKeys = new LinkedList<>(tagKeys);
            histogramKeys.add("le");
            // satisfies https://prometheus.io/docs/concepts/metric_types/#histogram
            for (CountAtBucket c : histogramCounts) {
                final List<String> histogramValues = new LinkedList<>(tagValues);
                histogramValues.add(Collector.doubleToGoString(c.bucket()));
                samples.add(new Collector.MetricFamilySamples.Sample(conventionName + "_bucket", histogramKeys, histogramValues, c.count()));
            }
            // the +Inf bucket should always equal `count`
            final List<String> histogramValues = new LinkedList<>(tagValues);
            histogramValues.add("+Inf");
            samples.add(new Collector.MetricFamilySamples.Sample(conventionName + "_bucket", histogramKeys, histogramValues, count));
        }
        samples.add(new Collector.MetricFamilySamples.Sample(conventionName + "_count", tagKeys, tagValues, count));
        samples.add(new Collector.MetricFamilySamples.Sample(conventionName + "_sum", tagKeys, tagValues, summary.totalAmount()));
        return Stream.of(new MicrometerCollector.Family(type, conventionName, samples.build()), new MicrometerCollector.Family(Collector.Type.GAUGE, conventionName + "_max", Stream.of(new Collector.MetricFamilySamples.Sample(conventionName + "_max", tagKeys, tagValues, summary.max()))));
    });
    return summary;
}
Also used : CountAtBucket(io.micrometer.core.instrument.distribution.CountAtBucket) LinkedList(java.util.LinkedList) ValueAtPercentile(io.micrometer.core.instrument.distribution.ValueAtPercentile) Collector(io.prometheus.client.Collector) Stream(java.util.stream.Stream)

Example 2 with ValueAtPercentile

use of io.micrometer.core.instrument.distribution.ValueAtPercentile in project micrometer by micrometer-metrics.

the class PrometheusMeterRegistry method newTimer.

@Override
protected io.micrometer.core.instrument.Timer newTimer(Meter.Id id, DistributionStatisticConfig distributionStatisticConfig, PauseDetector pauseDetector) {
    MicrometerCollector collector = collectorByName(id);
    PrometheusTimer timer = new PrometheusTimer(id, clock, distributionStatisticConfig, pauseDetector);
    List<String> tagValues = tagValues(id);
    collector.add((conventionName, tagKeys) -> {
        Stream.Builder<Collector.MetricFamilySamples.Sample> samples = Stream.builder();
        final ValueAtPercentile[] percentileValues = timer.takeSnapshot().percentileValues();
        final CountAtBucket[] histogramCounts = timer.histogramCounts();
        double count = timer.count();
        if (percentileValues.length > 0) {
            List<String> quantileKeys = new LinkedList<>(tagKeys);
            quantileKeys.add("quantile");
            // satisfies https://prometheus.io/docs/concepts/metric_types/#summary
            for (ValueAtPercentile v : percentileValues) {
                List<String> quantileValues = new LinkedList<>(tagValues);
                quantileValues.add(Collector.doubleToGoString(v.percentile()));
                samples.add(new Collector.MetricFamilySamples.Sample(conventionName, quantileKeys, quantileValues, v.value(TimeUnit.SECONDS)));
            }
        }
        Collector.Type type = distributionStatisticConfig.isPublishingHistogram() ? Collector.Type.HISTOGRAM : Collector.Type.SUMMARY;
        if (histogramCounts.length > 0) {
            // Prometheus doesn't balk at a metric being BOTH a histogram and a summary
            type = Collector.Type.HISTOGRAM;
            List<String> histogramKeys = new LinkedList<>(tagKeys);
            histogramKeys.add("le");
            // satisfies https://prometheus.io/docs/concepts/metric_types/#histogram
            for (CountAtBucket c : histogramCounts) {
                final List<String> histogramValues = new LinkedList<>(tagValues);
                histogramValues.add(Collector.doubleToGoString(c.bucket(TimeUnit.SECONDS)));
                samples.add(new Collector.MetricFamilySamples.Sample(conventionName + "_bucket", histogramKeys, histogramValues, c.count()));
            }
            // the +Inf bucket should always equal `count`
            final List<String> histogramValues = new LinkedList<>(tagValues);
            histogramValues.add("+Inf");
            samples.add(new Collector.MetricFamilySamples.Sample(conventionName + "_bucket", histogramKeys, histogramValues, count));
        }
        samples.add(new Collector.MetricFamilySamples.Sample(conventionName + "_count", tagKeys, tagValues, count));
        samples.add(new Collector.MetricFamilySamples.Sample(conventionName + "_sum", tagKeys, tagValues, timer.totalTime(TimeUnit.SECONDS)));
        return Stream.of(new MicrometerCollector.Family(type, conventionName, samples.build()), new MicrometerCollector.Family(Collector.Type.GAUGE, conventionName + "_max", Stream.of(new Collector.MetricFamilySamples.Sample(conventionName + "_max", tagKeys, tagValues, timer.max(getBaseTimeUnit())))));
    });
    return timer;
}
Also used : CountAtBucket(io.micrometer.core.instrument.distribution.CountAtBucket) LinkedList(java.util.LinkedList) ValueAtPercentile(io.micrometer.core.instrument.distribution.ValueAtPercentile) Collector(io.prometheus.client.Collector) Stream(java.util.stream.Stream)

Aggregations

CountAtBucket (io.micrometer.core.instrument.distribution.CountAtBucket)2 ValueAtPercentile (io.micrometer.core.instrument.distribution.ValueAtPercentile)2 Collector (io.prometheus.client.Collector)2 LinkedList (java.util.LinkedList)2 Stream (java.util.stream.Stream)2