io.micrometer.prometheus.PrometheusTimer Maven / Gradle / Ivy
Show all versions of micrometer-registry-prometheus Show documentation
/**
* Copyright 2017 Pivotal Software, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.prometheus;
import io.micrometer.core.instrument.AbstractTimer;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.distribution.CountAtBucket;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import io.micrometer.core.instrument.distribution.TimeWindowMax;
import io.micrometer.core.instrument.distribution.pause.PauseDetector;
import io.micrometer.core.instrument.util.TimeUtils;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;
public class PrometheusTimer extends AbstractTimer {
private final LongAdder count = new LongAdder();
private final LongAdder totalTime = new LongAdder();
private final TimeWindowMax max;
PrometheusTimer(Id id, Clock clock, DistributionStatisticConfig distributionStatisticConfig, PauseDetector pauseDetector) {
super(id, clock, DistributionStatisticConfig.builder()
.expiry(Duration.ofDays(1825)) // effectively never roll over
.bufferLength(1)
.build()
.merge(distributionStatisticConfig), pauseDetector, TimeUnit.SECONDS, true);
this.max = new TimeWindowMax(clock, distributionStatisticConfig);
}
@Override
protected void recordNonNegative(long amount, TimeUnit unit) {
count.increment();
long nanoAmount = TimeUnit.NANOSECONDS.convert(amount, unit);
totalTime.add(nanoAmount);
max.record(nanoAmount, TimeUnit.NANOSECONDS);
}
@Override
public long count() {
return count.longValue();
}
@Override
public double totalTime(TimeUnit unit) {
return TimeUtils.nanosToUnit(totalTime.doubleValue(), unit);
}
@Override
public double max(TimeUnit unit) {
return max.poll(unit);
}
/**
* For Prometheus we cannot use the histogram counts from HistogramSnapshot, as it is based on a
* rolling histogram. Prometheus requires a histogram that accumulates values over the lifetime of the app.
*
* @return Cumulative histogram buckets.
*/
public CountAtBucket[] histogramCounts() {
return histogram.takeSnapshot(0, 0, 0).histogramCounts();
}
}