【问题标题】:Does Micrometer timer records when exception is thrown inside of the record block?在记录块内引发异常时,千分尺计时器是否记录?
【发布时间】:2021-12-13 09:21:26
【问题描述】:

只是一个简单的例子:

fun main(): String? {
    val response: String? = null
    meterRegistry.timer("name").record {
        response = getResponse() // throws exception
    }
    return response
}

在这种情况下 getResponse() 会抛出异常,并且 main 方法中没有捕获。 它仍然记录到grafana吗?

【问题讨论】:

    标签: spring-boot kotlin timer grafana micrometer


    【解决方案1】:

    看着source code…

    @Override
    public void record(Runnable f) {
        final long s = clock.monotonicTime();
        try {
            f.run();
        } finally {
            final long e = clock.monotonicTime();
            record(e - s, TimeUnit.NANOSECONDS);
        }
    }
    

    ...您可以看到,在异常情况下也会记录时间(因为finally 块)。

    【讨论】:

      猜你喜欢
      • 2013-11-11
      • 2015-11-18
      • 2016-07-14
      • 2019-03-11
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多