为什么CounterService无法计算调用方法的次数?

时间:2017-01-10 03:47:49

标签: spring-boot spring-aop spring-boot-actuator

我使用spring AOP和spring boot CounterService来记录特定方法的调用次数。每次访问目标网址时,都会执行countServiceInvoke,但输出指标值始终为1"gauge.servo.string_com.yirendai.oss.environment.admin.controller.restcontrollertest.test()": 1

我想知道为什么这个计数器失败了?谢谢。 util类就像下面一样:

@Aspect
@Component
public class ServiceMonitor {
  @Autowired
  private CounterService counterService;

  @Before("execution(* com.yirendai.oss.environment.admin.controller.*.*(..))")
  public void countServiceInvoke(JoinPoint joinPoint) {
    System.out.println("@@@@@@@@@@@@@@@@@@" + joinPoint.getSignature());
    counterService.increment(joinPoint.getSignature() + "");
  }

}

1 个答案:

答案 0 :(得分:2)

我已阅读已实施的CounterService类的源代码,应使用"meter."启动密钥才能正确计算。

private void incrementInternal(String name, long value) {
        String strippedName = stripMetricName(name);

        if (name.startsWith("status.")) {
            // drop this metric since we are capturing it already with
            // ServoHandlerInterceptor,
            // and we are able to glean more information like exceptionType from that
            // mechanism than what
            // boot provides us
        }
        else if (name.startsWith("meter.")) {
            BasicCounter counter = counters.get(strippedName);
            if (counter == null) {
                counter = new BasicCounter(MonitorConfig.builder(strippedName).build());
                counters.put(strippedName, counter);
                registry.register(counter);
            }
            counter.increment(value);
        }
        else {
            LongGauge gauge = longGauges.get(strippedName);
            if (gauge == null) {
                gauge = new LongGauge(MonitorConfig.builder(strippedName).build());
                longGauges.put(strippedName, gauge);
                registry.register(gauge);
            }
            gauge.set(value);
        }
    }