/**
* 依赖jar包 metrics-core-4.1.9.jar
*
* NettyServer.exec 对象的定义 见 https://www.cnblogs.com/xiaodu9499/p/14328906.html
*/
public class MetricsUtils {
/**
* 监控堆外内存使用情况
* 工作线程的队列情况
*/
public static void start() {
// 注册
MetricRegistry metricRegistry = new MetricRegistry();
metricRegistry.register("已使用直接内存", (Gauge<Long>) () -> PlatformDependent.usedDirectMemory());
metricRegistry.register("最大直接内存", (Gauge<Long>) () -> PlatformDependent.maxDirectMemory());
for (EventExecutor eventExecutor : NettyServer.exec) {
metricRegistry.register("exec=======等待队列大小"+eventExecutor.hashCode(), (Gauge<Integer>) () ->((SingleThreadEventExecutor)eventExecutor).pendingTasks());
}
// 打印到控制台
ConsoleReporter consoleReporter = ConsoleReporter.forRegistry(metricRegistry).build();
// 每5秒打印一次
consoleReporter.start(5, TimeUnit.SECONDS);
}
}