io.micrometer.core.instrument.distribution.HistogramSnapshot Java Examples

The following examples show how to use io.micrometer.core.instrument.distribution.HistogramSnapshot. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OpStatsLoggerImpl.java    From pravega with Apache License 2.0 6 votes vote down vote up
@Override
public OpStatsData toOpStatsData() {
    long numFailed = fail.count();
    long numSuccess = success.count();
    HistogramSnapshot snapshot = success.takeSnapshot();
    double avgLatencyMillis = snapshot.mean();

    EnumMap<OpStatsData.Percentile, Long> percentileLongMap  =
            new EnumMap<>(OpStatsData.Percentile.class);

    //Only add entries into percentile map when percentile values are not missing from snapshot.
    if (OpStatsData.PERCENTILE_ARRAY.length == snapshot.percentileValues().length) {
        int index = 0;
        for (OpStatsData.Percentile percent : OpStatsData.PERCENTILE_SET) {
            percentileLongMap.put(percent, (long) snapshot.percentileValues()[index++].value());
        }
    }
    return new OpStatsData(numSuccess, numFailed, avgLatencyMillis, percentileLongMap);
}
 
Example #2
Source File: OpenTSDBMeterRegistry.java    From micrometer with Apache License 2.0 6 votes vote down vote up
Stream<String> writeLongTaskTimer(LongTaskTimer timer) {
    long wallTime = config().clock().wallTime();

    HistogramSnapshot histogramSnapshot = timer.takeSnapshot();
    final ValueAtPercentile[] percentileValues = histogramSnapshot.percentileValues();
    final CountAtBucket[] histogramCounts = histogramSnapshot.histogramCounts();
    double count = timer.activeTasks();

    List<String> metrics = new ArrayList<>();

    metrics.add(writeMetricWithSuffix(timer.getId(), "active.count", wallTime, count));
    metrics.add(writeMetricWithSuffix(timer.getId(), "duration.sum", wallTime, timer.duration(getBaseTimeUnit())));
    metrics.add(writeMetricWithSuffix(timer.getId(), "max", wallTime, timer.max(getBaseTimeUnit())));

    if (percentileValues.length > 0) {
        metrics.addAll(writePercentiles(timer, wallTime, percentileValues));
    }

    if (histogramCounts.length > 0) {
        metrics.addAll(writeHistogram(wallTime, timer, histogramCounts, count, getBaseTimeUnit()));
    }

    return metrics.stream();
}
 
Example #3
Source File: OpenTSDBMeterRegistry.java    From micrometer with Apache License 2.0 6 votes vote down vote up
Stream<String> writeTimer(Timer timer) {
    long wallTime = config().clock().wallTime();

    HistogramSnapshot histogramSnapshot = timer.takeSnapshot();
    final ValueAtPercentile[] percentileValues = histogramSnapshot.percentileValues();
    final CountAtBucket[] histogramCounts = histogramSnapshot.histogramCounts();
    double count = timer.count();

    List<String> metrics = new ArrayList<>();

    metrics.add(writeMetricWithSuffix(timer.getId(), "count", wallTime, count));
    metrics.add(writeMetricWithSuffix(timer.getId(), "sum", wallTime, timer.totalTime(getBaseTimeUnit())));
    metrics.add(writeMetricWithSuffix(timer.getId(), "max", wallTime, timer.max(getBaseTimeUnit())));

    if (percentileValues.length > 0) {
        metrics.addAll(writePercentiles(timer, wallTime, percentileValues));
    }

    if (histogramCounts.length > 0) {
        metrics.addAll(writeHistogram(wallTime, timer, histogramCounts, count, getBaseTimeUnit()));
    }

    return metrics.stream();
}
 
Example #4
Source File: StackdriverMeterRegistry.java    From micrometer with Apache License 2.0 6 votes vote down vote up
Stream<TimeSeries> createTimeSeries(HistogramSupport histogramSupport, boolean timeDomain) {
    HistogramSnapshot snapshot = histogramSupport.takeSnapshot();
    return Stream.concat(
            Stream.of(
                    createTimeSeries(histogramSupport, distribution(snapshot, timeDomain)),
                    createTimeSeries(histogramSupport,
                            timeDomain ? snapshot.max(getBaseTimeUnit()) : snapshot.max(),
                            "max"),
                    createTimeSeries(histogramSupport, snapshot.count(), "count")
            ),
            Arrays.stream(snapshot.percentileValues())
                    .map(valueAtP -> createTimeSeries(histogramSupport,
                            timeDomain ? valueAtP.value(getBaseTimeUnit()) : valueAtP.value(),
                            "p" + DoubleFormat.wholeOrDecimal(valueAtP.percentile() * 100)))
    );
}
 
Example #5
Source File: StackdriverMeterRegistryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
@Issue("#1325")
void distributionCountBucketsInfinityBucketIsNotNegative() {
    StackdriverMeterRegistry.Batch batch = meterRegistry.new Batch();
    // count is 4, but sum of bucket counts is 5 due to inconsistent snapshotting
    HistogramSnapshot histogramSnapshot = new HistogramSnapshot(4, 14.7, 5, null, new CountAtBucket[]{new CountAtBucket(1.0, 2), new CountAtBucket(2.0, 5)}, null);
    Distribution distribution = batch.distribution(histogramSnapshot, false);
    List<Long> bucketCountsList = distribution.getBucketCountsList();
    assertThat(bucketCountsList.get(bucketCountsList.size() - 1)).isNotNegative();
}
 
Example #6
Source File: StackdriverMeterRegistryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
@Issue("#2045")
void batchDistributionWhenHistogramSnapshotIsEmpty() {
    StackdriverMeterRegistry.Batch batch = meterRegistry.new Batch();
    HistogramSnapshot histogramSnapshot = HistogramSnapshot.empty(0, 0.0, 0.0);
    Distribution distribution = batch.distribution(histogramSnapshot, false);
    assertThat(distribution.getBucketOptions().getExplicitBuckets().getBoundsCount()).isEqualTo(1);
    assertThat(distribution.getBucketCountsList()).hasSize(1);
}
 
Example #7
Source File: GangliaMeterRegistry.java    From micrometer with Apache License 2.0 5 votes vote down vote up
private void announceTimer(Timer timer) {
    HistogramSnapshot snapshot = timer.takeSnapshot();
    announce(timer, snapshot.count(), "count");
    announce(timer, snapshot.total(getBaseTimeUnit()), "sum");
    announce(timer, snapshot.mean(getBaseTimeUnit()), "avg");
    announce(timer, snapshot.max(getBaseTimeUnit()), "max");
}
 
Example #8
Source File: GangliaMeterRegistry.java    From micrometer with Apache License 2.0 5 votes vote down vote up
private void announceSummary(DistributionSummary summary) {
    HistogramSnapshot snapshot = summary.takeSnapshot();
    announce(summary, snapshot.count(), "count");
    announce(summary, snapshot.total(), "sum");
    announce(summary, snapshot.mean(), "avg");
    announce(summary, snapshot.max(), "max");
}
 
Example #9
Source File: PrometheusMeterRegistryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Issue("#2060")
@Test
void timerCountAndSumHasCorrectBaseUnit() {
    Timer timer = Timer.builder("my.timer")
            .publishPercentileHistogram()
            .register(registry);

    timer.record(1, TimeUnit.SECONDS);
    HistogramSnapshot histogramSnapshot = timer.takeSnapshot();
    assertThat(histogramSnapshot.total(TimeUnit.SECONDS)).isEqualTo(1);
}
 
Example #10
Source File: DynatraceMeterRegistry.java    From micrometer with Apache License 2.0 5 votes vote down vote up
private Stream<DynatraceCustomMetric> writeTimer(Timer timer) {
    final long wallTime = clock.wallTime();
    final Meter.Id id = timer.getId();
    final HistogramSnapshot snapshot = timer.takeSnapshot();

    return Stream.of(
            createCustomMetric(idWithSuffix(id, "sum"), wallTime, snapshot.total(getBaseTimeUnit())),
            createCustomMetric(idWithSuffix(id, "count"), wallTime, snapshot.count(), DynatraceUnit.Count),
            createCustomMetric(idWithSuffix(id, "avg"), wallTime, snapshot.mean(getBaseTimeUnit())),
            createCustomMetric(idWithSuffix(id, "max"), wallTime, snapshot.max(getBaseTimeUnit())));
}
 
Example #11
Source File: DynatraceMeterRegistry.java    From micrometer with Apache License 2.0 5 votes vote down vote up
private Stream<DynatraceCustomMetric> writeSummary(DistributionSummary summary) {
    final long wallTime = clock.wallTime();
    final Meter.Id id = summary.getId();
    final HistogramSnapshot snapshot = summary.takeSnapshot();

    return Stream.of(
            createCustomMetric(idWithSuffix(id, "sum"), wallTime, snapshot.total(getBaseTimeUnit())),
            createCustomMetric(idWithSuffix(id, "count"), wallTime, snapshot.count(), DynatraceUnit.Count),
            createCustomMetric(idWithSuffix(id, "avg"), wallTime, snapshot.mean(getBaseTimeUnit())),
            createCustomMetric(idWithSuffix(id, "max"), wallTime, snapshot.max(getBaseTimeUnit())));
}
 
Example #12
Source File: AppOpticsMeterRegistry.java    From micrometer with Apache License 2.0 5 votes vote down vote up
private Optional<String> writeSummary(DistributionSummary summary) {
    HistogramSnapshot snapshot = summary.takeSnapshot();
    if (snapshot.count() > 0) {
        return Optional.of(write(summary.getId(), "distributionSummary",
                Fields.Count.tag(), decimal(summary.count()),
                Fields.Sum.tag(), decimal(summary.totalAmount()),
                Fields.Max.tag(), decimal(summary.max())));
    }
    return Optional.empty();
}
 
Example #13
Source File: AppOpticsMeterRegistry.java    From micrometer with Apache License 2.0 5 votes vote down vote up
private Optional<String> writeTimer(Timer timer) {
    HistogramSnapshot snapshot = timer.takeSnapshot();
    long count = snapshot.count();
    if (count > 0) {
        return Optional.of(write(timer.getId(), "timer",
                Fields.Count.tag(), decimal(count),
                Fields.Sum.tag(), decimal(snapshot.total(getBaseTimeUnit())),
                Fields.Max.tag(), decimal(snapshot.max(getBaseTimeUnit()))));
    }
    return Optional.empty();
}
 
Example #14
Source File: HumioMeterRegistry.java    From micrometer with Apache License 2.0 5 votes vote down vote up
String writeSummary(DistributionSummary summary) {
    HistogramSnapshot snap = summary.takeSnapshot();
    return writeEvent(summary,
            event("count", snap.count()),
            event("sum", snap.total()),
            event("avg", snap.mean()),
            event("max", snap.max()));
}
 
Example #15
Source File: HumioMeterRegistry.java    From micrometer with Apache License 2.0 5 votes vote down vote up
String writeTimer(Timer timer) {
    HistogramSnapshot snap = timer.takeSnapshot();
    return writeEvent(timer,
            event("count", snap.count()),
            event("sum", snap.total(getBaseTimeUnit())),
            event("avg", snap.mean(getBaseTimeUnit())),
            event("max", snap.max(getBaseTimeUnit())));
}
 
Example #16
Source File: ElasticMeterRegistry.java    From micrometer with Apache License 2.0 5 votes vote down vote up
Optional<String> writeSummary(DistributionSummary summary) {
    HistogramSnapshot histogramSnapshot = summary.takeSnapshot();
    return Optional.of(writeDocument(summary, builder -> {
        builder.append(",\"count\":").append(histogramSnapshot.count());
        builder.append(",\"sum\":").append(histogramSnapshot.total());
        builder.append(",\"mean\":").append(histogramSnapshot.mean());
        builder.append(",\"max\":").append(histogramSnapshot.max());
    }));
}
 
Example #17
Source File: MetricsConverter.java    From cf-java-logging-support with Apache License 2.0 5 votes vote down vote up
protected Stream<Metric> getMetrics(Meter meter, HistogramSnapshot snapshot) {
    return Stream.concat(
        Stream.of(toMetric(withStatistic(meter, "count"), snapshot.count()),
            toMetric(withStatistic(meter, "max"), snapshot.max(getBaseTimeUnit())),
            toMetric(withStatistic(meter, "mean"), snapshot.mean(getBaseTimeUnit())),
            toMetric(withStatistic(meter, "totalTime"), snapshot.total(getBaseTimeUnit()))),
        getMetrics(meter, snapshot.percentileValues()));
}
 
Example #18
Source File: Metric.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new {@link Metric} instance.
 * @param id Meter id
 * @param snapshot instance of HistogramSnapshot
 */
Metric(Meter.Id id, HistogramSnapshot snapshot) {
	this.timestamp = new Date();
	this.id = id;
	this.sum = snapshot.total(TimeUnit.MILLISECONDS);
	this.count = snapshot.count();
	this.mean = snapshot.mean(TimeUnit.MILLISECONDS);
	this.upper = snapshot.max(TimeUnit.MILLISECONDS);
	this.total = snapshot.total(TimeUnit.MILLISECONDS);
}
 
Example #19
Source File: NoopTimerTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void returnsEmptySnapshot() {
    HistogramSnapshot snapshot = timer.takeSnapshot();
    HistogramSnapshot expectedHistogram = HistogramSnapshot.empty(0, 0, 0);
    assertThat(snapshot.count()).isEqualTo(expectedHistogram.count());
    assertThat(snapshot.total()).isEqualTo(expectedHistogram.total());
    assertThat(snapshot.max()).isEqualTo(expectedHistogram.max());
}
 
Example #20
Source File: NoopDistributionSummaryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void returnsEmptySnapshot() {
    HistogramSnapshot snapshot = distributionSummary.takeSnapshot();
    HistogramSnapshot expectedHistogram = HistogramSnapshot.empty(0, 0, 0);
    assertThat(snapshot.count()).isEqualTo(expectedHistogram.count());
    assertThat(snapshot.total()).isEqualTo(expectedHistogram.total());
    assertThat(snapshot.max()).isEqualTo(expectedHistogram.max());
}
 
Example #21
Source File: MetricsServiceImpl.java    From vertx-micrometer-metrics with Apache License 2.0 5 votes vote down vote up
private static JsonObject summaryToJson(JsonObject obj, DistributionSummary summary) {
  HistogramSnapshot snapshot = summary.takeSnapshot(false);
  return obj.put("type", "summary")
    .put("count", snapshot.count())
    .put("total", snapshot.total())
    .put("mean", snapshot.mean())
    .put("max", snapshot.max());
}
 
Example #22
Source File: DistributionSummaryTest.java    From pepper-metrics with Apache License 2.0 4 votes vote down vote up
@Test
    public void test() throws InterruptedException {
        final PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
        final Timer summary = Timer.builder("test")
                .distributionStatisticExpiry(Duration.ofSeconds(10))
                .publishPercentiles(0.9D, 0.99D, 0.999D)
                .distributionStatisticBufferLength(20)
                .publishPercentileHistogram(false)
                .tags(new String[]{"method", "get()"})
                .register(prometheusRegistry);
//        final DistributionSummary summary = DistributionSummary.builder("test")
//                .distributionStatisticExpiry(Duration.ofSeconds(30))
//                .publishPercentiles(0.9, 0.99, 0.999)
//                .publishPercentileHistogram(false)
//                .tags(new String[]{"method", "get()"})
//                .register(prometheusRegistry);
        AtomicInteger second = new AtomicInteger();
        Executors.newScheduledThreadPool(1).scheduleAtFixedRate(() -> {
            final HistogramSnapshot snapshot = summary.takeSnapshot();
            final ValueAtPercentile[] valueAtPercentiles = snapshot.percentileValues();
            double p90 = 0, p99 = 0, p999 = 0;
            for (ValueAtPercentile percentile : valueAtPercentiles) {
                if (percentile.percentile() == 0.9D) {
                    p90 = percentile.value(TimeUnit.MILLISECONDS);
                } else if (percentile.percentile() == 0.99D) {
                    p99 = percentile.value(TimeUnit.MILLISECONDS);
                } else {
                    p999 = percentile.value(TimeUnit.MILLISECONDS);
                }
            }
            System.out.println(String.format("second: %s, p90: %s, p99: %s, p999: %s", second.incrementAndGet(), p90, p99, p999));
        }, 1, 1, TimeUnit.SECONDS);
        for (int j = 0; j < 100; j++) {
//            for (long i = 0; i < 1000; i++) {
//                summary.record(i, TimeUnit.MILLISECONDS);
//            }
            summary.record(j % 10, TimeUnit.MILLISECONDS);
            TimeUnit.SECONDS.sleep(1);
        }

        for (int i = 0; i < 10; i++) {
            TimeUnit.SECONDS.sleep(1);
        }

        for (int i = 0; i < 100; i++) {
            TimeUnit.SECONDS.sleep(1);
        }
    }
 
Example #23
Source File: AbstractPerfPrinter.java    From pepper-metrics with Apache License 2.0 4 votes vote down vote up
private List<PrinterDomain> collector(Stats stats, ConcurrentMap<String, ConcurrentMap<List<String>, Double>> currentErrCollector,
                                      ConcurrentMap<String, ConcurrentMap<List<String>, Long>> currentSummaryCollector) {
    ConcurrentMap<List<String>, Counter> errCollector = stats.getErrCollector();
    ConcurrentMap<List<String>, AtomicLong> gaugeCollector = stats.getGaugeCollector();
    ConcurrentMap<List<String>, Timer> summaryCollector = stats.getSummaryCollector();

    // 记录上一次的error数
    currentErrCollector.put(buildCollectorKey(stats), parseErrCollector(errCollector));
    currentSummaryCollector.put(buildCollectorKey(stats), parseSummaryCollector(summaryCollector));

    List<PrinterDomain> retList = new ArrayList<>();

    for (Map.Entry<List<String>, Timer> entry : summaryCollector.entrySet()) {
        List<String> tag = entry.getKey();
        Timer summary= entry.getValue();

        Counter counter = errCollector.get(tag);
        AtomicLong concurrent = gaugeCollector.get(tag);

        PrinterDomain domain = new PrinterDomain();

        String name = setMetricsName(stats, tag);
        HistogramSnapshot snapshot = summary.takeSnapshot();

        domain.setTag(name);

        domain.setConcurrent(concurrent == null ? "0" : concurrent.toString());
        domain.setErr(counter == null ? "0" : String.valueOf(counter.count() - getLastTimeErrCount(stats, entry.getKey())));
        domain.setSum(String.valueOf(snapshot.count() - getLastTimeSummaryCount(stats, entry.getKey())));
        ValueAtPercentile[] vps = snapshot.percentileValues();
        for (ValueAtPercentile vp : vps) {
            if (vp.percentile() == 0.9D) {
                domain.setP90(String.valueOf(vp.value(TimeUnit.MILLISECONDS)));
            } else if (vp.percentile() == 0.99D) {
                domain.setP99(String.valueOf(vp.value(TimeUnit.MILLISECONDS)));
            } else if (vp.percentile() == 0.999D) {
                domain.setP999(String.valueOf(vp.value(TimeUnit.MILLISECONDS)));
            } else if (vp.percentile() == 0.99999D) {
                domain.setMax(String.valueOf(vp.value(TimeUnit.MILLISECONDS)));
            }
        }

        // 计算qps
        domain.setQps(String.format("%.1f", Float.parseFloat(domain.getSum()) / 60));

        retList.add(domain);
    }

    return retList;
}
 
Example #24
Source File: RntbdMetrics.java    From azure-cosmosdb-java with MIT License 4 votes vote down vote up
@JsonProperty
public HistogramSnapshot requestSize() {
    return this.requestSize.takeSnapshot();
}
 
Example #25
Source File: RntbdMetrics.java    From azure-cosmosdb-java with MIT License 4 votes vote down vote up
@JsonProperty
public HistogramSnapshot requests() {
    return this.requests.takeSnapshot();
}
 
Example #26
Source File: StackdriverMeterRegistry.java    From micrometer with Apache License 2.0 4 votes vote down vote up
Distribution distribution(HistogramSnapshot snapshot, boolean timeDomain) {
    CountAtBucket[] histogram = snapshot.histogramCounts();

    // selected finite buckets (represented as a normal histogram)
    AtomicLong truncatedSum = new AtomicLong();
    AtomicReference<Double> last = new AtomicReference<>(0.0);
    List<Long> bucketCounts = Arrays.stream(histogram)
            .map(countAtBucket -> {
                double cumulativeCount = countAtBucket.count();
                long bucketCount = (long) (cumulativeCount - last.getAndSet(cumulativeCount));
                truncatedSum.addAndGet(bucketCount);
                return bucketCount;
            })
            .collect(toCollection(ArrayList::new));

    if (!bucketCounts.isEmpty()) {
        int endIndex = bucketCounts.size() - 1;
        // trim zero-count buckets on the right side of the domain
        if (bucketCounts.get(endIndex) == 0) {
            int lastNonZeroIndex = 0;
            for (int i = endIndex - 1; i >= 0; i--) {
                if (bucketCounts.get(i) > 0) {
                    lastNonZeroIndex = i;
                    break;
                }
            }
            bucketCounts = bucketCounts.subList(0, lastNonZeroIndex + 1);
        }
    }

    // add the "+infinity" bucket, which does NOT have a corresponding bucket boundary
    bucketCounts.add(Math.max(0, snapshot.count() - truncatedSum.get()));

    List<Double> bucketBoundaries = Arrays.stream(histogram)
            .map(countAtBucket -> timeDomain ? countAtBucket.bucket(getBaseTimeUnit()) : countAtBucket.bucket())
            .collect(toCollection(ArrayList::new));

    if (bucketBoundaries.size() != bucketCounts.size() - 1) {
        bucketBoundaries = bucketBoundaries.subList(0, bucketCounts.size() - 1);
    }

    // stackdriver requires at least one finite bucket
    if (bucketBoundaries.isEmpty()) {
        bucketBoundaries.add(0.0);
    }

    return Distribution.newBuilder()
            .setMean(timeDomain ? snapshot.mean(getBaseTimeUnit()) : snapshot.mean())
            .setCount(snapshot.count())
            .setBucketOptions(Distribution.BucketOptions.newBuilder()
                    .setExplicitBuckets(Distribution.BucketOptions.Explicit.newBuilder()
                            .addAllBounds(bucketBoundaries)
                            .build())
                    .build())
            .addAllBucketCounts(bucketCounts)
            .build();
}
 
Example #27
Source File: RntbdMetrics.java    From azure-cosmosdb-java with MIT License 4 votes vote down vote up
@JsonProperty
public HistogramSnapshot responseErrors() {
    return this.responseErrors.takeSnapshot();
}
 
Example #28
Source File: RntbdMetrics.java    From azure-cosmosdb-java with MIT License 4 votes vote down vote up
@JsonProperty
public HistogramSnapshot responseSize() {
    return this.responseSize.takeSnapshot();
}
 
Example #29
Source File: RntbdMetrics.java    From azure-cosmosdb-java with MIT License 4 votes vote down vote up
@JsonProperty
public HistogramSnapshot responseSuccesses() {
    return this.responseSuccesses.takeSnapshot();
}
 
Example #30
Source File: CompositeTimer.java    From micrometer with Apache License 2.0 4 votes vote down vote up
@Override
public HistogramSnapshot takeSnapshot() {
    return firstChild().takeSnapshot();
}