org.zodiac.monitor.metrics.micrometer.MeterMapCleanerTask Maven / Gradle / Ivy
package org.zodiac.monitor.metrics.micrometer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
public class MeterMapCleanerTask {
private Logger log = LoggerFactory.getLogger(getClass());
private ThreadPoolTaskScheduler threadPoolTaskScheduler;
private MeterRegistry meterRegistry;
private ReentrantLock startLock = new ReentrantLock();
private ReentrantLock stopLock = new ReentrantLock();
public MeterMapCleanerTask(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
}
public void cleanMeterMap() {
log.info("Clean meterMap of {}", this.meterRegistry);
if (this.meterRegistry instanceof CompositeMeterRegistry) {
CompositeMeterRegistry compositeMeterRegistry = (CompositeMeterRegistry)meterRegistry;
this.meterRegistry.getMeters().forEach(this.meterRegistry::remove);
compositeMeterRegistry.getRegistries().forEach(meterRegistry1 -> {
meterRegistry1.getMeters().forEach(meterRegistry1::remove);
});
} else {
this.meterRegistry.getMeters().forEach(this.meterRegistry::remove);
}
}
public void start(String trigger) {
if (meterRegistry == null) {
log.error("meterRegistry SHOULD NOT BE NULL");
return;
}
try {
startLock.tryLock(4500, TimeUnit.MILLISECONDS);
if (threadPoolTaskScheduler != null) {
log.info("MeterMapCleanerTask was already scheduled by {}", threadPoolTaskScheduler);
return;
}
threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setDaemon(true);
threadPoolTaskScheduler.setThreadNamePrefix("MeterMapCleanerTask");
threadPoolTaskScheduler.initialize();
log.info("MeterMapCleanerTask will be scheduled by {}. trigger = {}", threadPoolTaskScheduler, trigger);
threadPoolTaskScheduler.schedule(this::cleanMeterMap, new CronTrigger(trigger));
} catch (InterruptedException e) {
log.error("{}", e);
} finally {
if (startLock.isLocked()) {
startLock.unlock();
}
}
}
public void stop() {
try {
stopLock.tryLock(12500, TimeUnit.MILLISECONDS);
if (threadPoolTaskScheduler != null) {
threadPoolTaskScheduler.shutdown();
threadPoolTaskScheduler = null;
}
} catch (InterruptedException e) {
log.error("{}", e);
} finally {
if (stopLock.isLocked()) {
stopLock.unlock();
}
}
}
public static void main(String[] args) {
SimpleMeterRegistry meterRegistry = new SimpleMeterRegistry();
Metrics.globalRegistry.add(meterRegistry);
MeterMapCleanerTask task = new MeterMapCleanerTask(Metrics.globalRegistry);
task.start("0/2 * * * * ?");
ScheduledExecutorService s = Executors.newSingleThreadScheduledExecutor();
s.scheduleAtFixedRate(() -> {
meterRegistry.counter(StrUtil.fastRandomUUID()).increment();
System.out.println(meterRegistry.getMeters().size());
}, 0, 100, TimeUnit.MILLISECONDS);
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
s.shutdown();
task.stop();
}
}