MeterRegistry计数器不增加

时间:2019-05-11 17:56:45

标签: spring-boot spring-boot-actuator micrometer spring-micrometer

我正在尝试实现错误-404指标计数器,我只想计算在我的api中发生的404请求的数量。即使我尝试模拟对我的api的多个404请求,我也没有看到计数的增加。以下是我的指标类-

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;


@ControllerAdvice
public class MetricController {

    private MeterRegistry meterRegistry;


    @Autowired
    public MetricController(MeterRegistry meterRegistry){
        this.meterRegistry=meterRegistry;
    }


    private void countHttpStatus(HttpStatus status){
      Counter count=meterRegistry.counter(String.format("http.status.%d",status.value()));
      count.increment();

    }


    @Bean
    ApplicationRunner runner(){
        return args ->{
          countHttpStatus(HttpStatus.NOT_FOUND);


        };
    }


}

当我达到此终点时-http://localhost:8081/actuator/metrics/http.status.404

然后我看到以下回复-

{
    "name": "http.status.404",
    "description": null,
    "baseUnit": null,
    "measurements": [
        {
            "statistic": "COUNT",
            "value": 1
        }
    ],
    "availableTags": []
}

如果我用多个404击打api,我希望此计数增加

0 个答案:

没有答案