io.micrometer.core.instrument.cumulative.CumulativeFunctionTimer Maven / Gradle / Ivy
Show all versions of micrometer-core Show documentation
/**
* Copyright 2017 VMware, 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
*
* https://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.core.instrument.cumulative;
import io.micrometer.core.instrument.FunctionTimer;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.util.TimeUtils;
import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;
import java.util.function.ToDoubleFunction;
import java.util.function.ToLongFunction;
/**
* A timer that tracks monotonically increasing functions for count and totalTime.
*
* @author Jon Schneider
* @author Johnny Lim
*/
public class CumulativeFunctionTimer implements FunctionTimer {
private final Meter.Id id;
private final WeakReference ref;
private final ToLongFunction countFunction;
private final ToDoubleFunction totalTimeFunction;
private final TimeUnit totalTimeFunctionUnit;
private final TimeUnit baseTimeUnit;
private volatile long lastCount;
private volatile double lastTime;
public CumulativeFunctionTimer(Id id, T obj, ToLongFunction countFunction, ToDoubleFunction totalTimeFunction,
TimeUnit totalTimeFunctionUnit, TimeUnit baseTimeUnit) {
this.id = id;
this.ref = new WeakReference<>(obj);
this.countFunction = countFunction;
this.totalTimeFunction = totalTimeFunction;
this.totalTimeFunctionUnit = totalTimeFunctionUnit;
this.baseTimeUnit = baseTimeUnit;
}
/**
* The total number of occurrences of the timed event.
*/
public double count() {
T obj2 = ref.get();
return obj2 != null ? (lastCount = Math.max(countFunction.applyAsLong(obj2), 0)) : lastCount;
}
/**
* The total time of all occurrences of the timed event.
*/
public double totalTime(TimeUnit unit) {
T obj2 = ref.get();
if (obj2 != null) {
lastTime = Math.max(TimeUtils.convert(totalTimeFunction.applyAsDouble(obj2), totalTimeFunctionUnit, baseTimeUnit()), 0);
}
return TimeUtils.convert(lastTime, baseTimeUnit(), unit);
}
@Override
public Id getId() {
return id;
}
@Override
public TimeUnit baseTimeUnit() {
return this.baseTimeUnit;
}
public Type type() {
return Type.TIMER;
}
}