com.github.marschall.micrometer.jfr.JfrMeterRegistry 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.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.function.ToDoubleFunction;
import java.util.function.ToLongFunction;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.FunctionCounter;
import io.micrometer.core.instrument.FunctionTimer;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.LongTaskTimer;
import io.micrometer.core.instrument.Measurement;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Meter.Id;
import io.micrometer.core.instrument.Meter.Type;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import io.micrometer.core.instrument.distribution.pause.PauseDetector;
import io.micrometer.core.lang.Nullable;
/**
* A Micrometer meter registry that generates JFR events for metrics.
*/
public final class JfrMeterRegistry extends MeterRegistry {
/**
* Default constructor using the given clock.
*
* @param clock the clock to use, not {@code null}
*/
public JfrMeterRegistry(Clock clock) {
super(clock);
Objects.requireNonNull(clock, "clock");
}
/**
* Default constructor using the system clock.
*/
public JfrMeterRegistry() {
this(Clock.SYSTEM);
}
@Override
protected Gauge newGauge(Id id, @Nullable T obj, ToDoubleFunction valueFunction) {
Gauge gauge = new JfrGauge<>(id, obj, valueFunction);
gauge.value(); // record the initial value
return gauge;
}
@Override
protected Counter newCounter(Id id) {
return new JfrCounter(id);
}
@Override
protected Timer newTimer(Id id, DistributionStatisticConfig distributionStatisticConfig, PauseDetector pauseDetector) {
return new JfrTimer(id, distributionStatisticConfig, pauseDetector, this.getBaseTimeUnit(), this.clock);
}
@Override
protected LongTaskTimer newLongTaskTimer(Id id, DistributionStatisticConfig distributionStatisticConfig) {
return new JfrLongTaskTimer(id, distributionStatisticConfig, this.getBaseTimeUnit(), this.clock);
}
@Override
@Deprecated
protected LongTaskTimer newLongTaskTimer(Id id) {
return this.newLongTaskTimer(id, null);
}
@Override
protected DistributionSummary newDistributionSummary(Id id, DistributionStatisticConfig distributionStatisticConfig, double scale) {
return new JfrDistributionSummary(id, distributionStatisticConfig, scale);
}
@Override
protected Meter newMeter(Id id, Type type, Iterable measurements) {
return new JfrMeter(id, type, measurements, this.getBaseTimeUnit());
}
@Override
protected FunctionTimer newFunctionTimer(Id id, T obj,
ToLongFunction countFunction,
ToDoubleFunction totalTimeFunction,
TimeUnit totalTimeFunctionUnit) {
JfrFunctionTimer functionTimer = new JfrFunctionTimer<>(id, obj, countFunction, totalTimeFunction, totalTimeFunctionUnit, this.getBaseTimeUnit());
functionTimer.recordEvent(); // record the initial value
return functionTimer;
}
@Override
protected FunctionCounter newFunctionCounter(Id id, T obj, ToDoubleFunction countFunction) {
FunctionCounter functionCounter = new JfrFunctionCounter<>(id, obj, countFunction);
functionCounter.count(); // record the initial value
return functionCounter;
}
@Override
protected TimeUnit getBaseTimeUnit() {
return TimeUnit.NANOSECONDS;
}
@Override
protected DistributionStatisticConfig defaultHistogramConfig() {
return DistributionStatisticConfig.NONE;
}
@Override
public String toString() {
return "JFR MeterRegistry";
}
}