com.github.marschall.micrometer.jfr.JfrFunctionCounter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micrometer-jfr Show documentation
Show all versions of micrometer-jfr Show documentation
A Micrometer meter registry that generates JFR events
package com.github.marschall.micrometer.jfr;
import java.lang.ref.WeakReference;
import java.util.function.ToDoubleFunction;
import io.micrometer.core.instrument.FunctionCounter;
final class JfrFunctionCounter extends AbstractJfrMeter implements FunctionCounter {
private final WeakReference reference;
private final ToDoubleFunction countFunction;
private final Runnable hook;
JfrFunctionCounter(Id id, T obj, ToDoubleFunction countFunction) {
super(id, new FunctionCounterEventFactory(id));
// all the other code uses a WeakReference
this.reference = new WeakReference<>(obj);
this.countFunction = countFunction;
this.hook = () -> this.count();
this.meterEventFactory.registerPeriodicEvent(this.jfrEventFactory, this.hook);
}
@Override
public void close() {
this.meterEventFactory.unregisterPeriodicEvent(this.hook);
super.close();
}
@Override
public double count() {
T obj = this.reference.get();
double value;
if (obj != null) {
value = this.countFunction.applyAsDouble(obj);
} else {
value = Double.NaN;
}
JfrFunctionCounterEvent event = this.newEmptyEvent();
event.setCount(value);
event.commit();
return value;
}
}