如何读取和复位弹簧执行器指标?

问题描述 投票:3回答:3

我正在使用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中读取计数值?

spring spring-boot metrics spring-boot-actuator
3个回答
5
投票
@Autowired
private BufferMetricReader metrics;

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


0
投票

@ membersound的答案有效,但我认为它更好用

@Autowired
private MetricReader metricReader;

代替

@Autowired
private BufferMetricReader metrics;
© www.soinside.com 2019 - 2024. All rights reserved.