com.github.marschall.micrometer.jfr.AbstractJfrGauge 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
The newest version!
package com.github.marschall.micrometer.jfr;
import java.lang.ref.WeakReference;
import java.util.function.ToDoubleFunction;
import io.micrometer.core.instrument.Gauge;
abstract class AbstractJfrGauge, E extends AbstractJfrMeterEvent & DoubleValueEvent> extends AbstractJfrMeter implements Gauge {
final WeakReference reference;
final ToDoubleFunction valueFunction;
private final Runnable hook;
AbstractJfrGauge(Id id, T obj, ToDoubleFunction valueFunction, F meterEventFactory) {
super(id, meterEventFactory);
// all the other code uses a WeakReference
this.reference = new WeakReference<>(obj);
this.valueFunction = valueFunction;
this.hook = this::value;
this.meterEventFactory.registerPeriodicEvent(this.jfrEventFactory, this.hook);
}
@Override
public double value() {
T obj = this.reference.get();
double value;
if (obj != null) {
value = this.valueFunction.applyAsDouble(obj);
} else {
value = Double.NaN;
}
E event = this.newEmptyEvent();
event.setValue(value);
event.commit();
return value;
}
@Override
public void close() {
this.meterEventFactory.unregisterPeriodicEvent(this.hook);
super.close();
}
}