The MeterRegistryHolder class is designed to hold MeterRegistry as a static field.
The goal is to simplify using MeterRegistry in a spring application. By this holder, you can use metrics outside the bean classes.
Initialize holder on application start
@Configuration
public class MeterRegistryHolderInitializer {
MeterRegistryHolderInitializer(MeterRegistry meterRegistry) {
MeterRegistryHolder.init(meterRegistry);
}
}
Use holder instead of regular injected MeterRegistry, e.g.
MeterRegistryHolder.meterRegistry().counter("my.metric").increment();
When initializing the context, before the holder is initialized, you can register metrics via lateInitMeterRegistry method, e.g.
MeterRegistryHolder.lateInitMeterRegistry(meterRegistry -> meterRegistry.counter("my.metric").increment());
If the holder is initialized, the lambda will be executed immediately. Otherwise, the lambda will be remembered and executed at the time of initialization.