如何读取和重置 spring 执行器指标?

How to read and reset spring actuator metrics?

我正在使用 spring-boot.1.3.0,它提供了在内存中存储自定义 metrics 的能力,如下所示:

@Service
public class MyService {
    private CounterService counterService;
    private GaugeService gaucheService;

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

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

问题:如何以编程方式从 CounterServiceGaugeService 中读取计数值?

Javadoc 显示了一个 reset 方法:http://docs.spring.io/autorepo/docs/spring-boot/1.3.0.RELEASE/api/org/springframework/boot/actuate/metrics/CounterService.html

必须从出口商处读取:http://docs.spring.io/autorepo/docs/spring-boot/1.3.0.RELEASE/api/org/springframework/boot/actuate/metrics/export/package-summary.html

@Autowired
private BufferMetricReader metrics;

int count = metrics.findOne("my.metrics.key").getValue().intValue();

@membersound 的回答有效,但我认为最好使用

@Autowired
private MetricReader metricReader;

而不是

@Autowired
private BufferMetricReader metrics;