spring_reference/V. Spring Boot Actuator: Pr.../44.4. Recording your own me...

1.4 KiB
Raw Blame History

44.4. 记录自己的指标

想要记录你自己的指标,只需将CounterServiceGaugeService注入到你的bean中。CounterService暴露incrementdecrement和reset方法GaugeService提供一个submit方法。

下面是一个简单的示例,它记录了方法调用的次数:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.stereotype.Service;

@Service
public class MyService {

    private final CounterService counterService;

    @Autowired
    public MyService(CounterService counterService) {
        this.counterService = counterService;
    }

    public void exampleMethod() {
        this.counterService.increment("services.system.myservice.invoked");
    }

}

:你可以将任何的字符串用作指标的名称,但最好遵循所选存储或图技术的指南。Matt Aimonettis Blog中有一些好的关于图Graphite的指南。