Home > OS >  Does Micrometer timer records when exception is thrown inside of the record block?
Does Micrometer timer records when exception is thrown inside of the record block?

Time:10-28

Just a quick example:

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

In that case getResponse() throws an exception and there is no catch in the main method. Does it still record to grafana?

CodePudding user response:

Looking at the 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);
    }
}

…you can see that the timing is recorded also in case of exceptions (because of the finally block).

  • Related