public class DemoMetrics implements MeterBinder {

    private AtomicLong systemMemoryUsed = new AtomicLong(0);

    //这里实现了MeterBinder接口的bindTo方法,将要采集的指标注册到MeterRegistry
    @Override
    public void bindTo(MeterRegistry meterRegistry) {
        //这里的MeterRegistry 是全局的
        Gauge.builder("system.memory.used",systemMemoryUsed, AtomicLong::get)
    //              .tag("groupName", this.groupName)
                    .description("系统已用内存(byte)")
                    .register(registry);
    }

    //定时器,定时改变内存数值
    @Scheduled(fixedRate = 1000)
    public void recordMemory(){
        //获取内存信息,省略
        //更改内存
        systemMemoryUsed.set(physicalUse);
    }
}

@Bean
public DemoMetrics demoMetrics(){
    return new DemoMetrics();
}
作者:Jeebiz  创建时间:2023-04-27 14:35
最后编辑:Jeebiz  更新时间:2024-02-26 11:18