Search in sources :

Example 1 with CountAtBucket

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

the class DistributionSummaryTest method histogramsInStepMode.

@Test
void histogramsInStepMode() {
    MockClock clock = new MockClock();
    MeterRegistry registry = new SimpleMeterRegistry(new SimpleConfig() {

        @Override
        public String get(String key) {
            return null;
        }

        @Override
        public CountingMode mode() {
            return CountingMode.STEP;
        }
    }, clock);
    DistributionSummary summary = DistributionSummary.builder("my.summary").sla(1).register(registry);
    summary.record(1);
    assertThat(summary.takeSnapshot().histogramCounts()).containsExactly(new CountAtBucket(1, 1));
    clock.add(SimpleConfig.DEFAULT.step());
    assertThat(summary.takeSnapshot().histogramCounts()).containsExactly(new CountAtBucket(1, 0));
}
Also used : SimpleConfig(io.micrometer.core.instrument.simple.SimpleConfig) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) CountingMode(io.micrometer.core.instrument.simple.CountingMode) CountAtBucket(io.micrometer.core.instrument.distribution.CountAtBucket) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Test(org.junit.jupiter.api.Test)

Example 2 with CountAtBucket

use of io.micrometer.core.instrument.distribution.CountAtBucket 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 3 with CountAtBucket

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

the class DistributionSummaryTest method histogramsInCumulativeMode.

@Test
void histogramsInCumulativeMode() {
    MockClock clock = new MockClock();
    MeterRegistry registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock);
    DistributionSummary summary = DistributionSummary.builder("my.summary").sla(1).register(registry);
    summary.record(1);
    // Histogram bucket counts DO roll over at the step interval, so decay.
    assertThat(summary.takeSnapshot().histogramCounts()).containsExactly(new CountAtBucket(1, 1));
    clock.add(SimpleConfig.DEFAULT.step());
    assertThat(summary.takeSnapshot().histogramCounts()).containsExactly(new CountAtBucket(1, 0));
}
Also used : SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) CountAtBucket(io.micrometer.core.instrument.distribution.CountAtBucket) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Test(org.junit.jupiter.api.Test)

Example 4 with CountAtBucket

use of io.micrometer.core.instrument.distribution.CountAtBucket 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)4 ValueAtPercentile (io.micrometer.core.instrument.distribution.ValueAtPercentile)2 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)2 Collector (io.prometheus.client.Collector)2 LinkedList (java.util.LinkedList)2 Stream (java.util.stream.Stream)2 Test (org.junit.jupiter.api.Test)2 CountingMode (io.micrometer.core.instrument.simple.CountingMode)1 SimpleConfig (io.micrometer.core.instrument.simple.SimpleConfig)1