We are using com.codahale.metrics.Timer to time how long it takes for a completeableFuture method to finish.
We have this code:
import com.codahale.metrics.Timer;
import com.codahale.metrics.MetricRegistry;
...
Timer timer = MetricsRegistry.timer("older-Cat-latency");
Timer.Context context = timer.time();
DeferredResult<OlderCat> deferredResult = new DeferredResult<>();
catService.getOlderCat(catName)
.handle((result, err) -> {
if(err != null){
deferredResult.setErrorResult(new OlderCat(...));
return null;
}
context.stop(); <---- // here we stop the timer
deferredResult.setResult(result);
return null;
});
return deferredResult;
Theoretically, this code should be OK, but when checking the time in the Prometheus we see very low timing latency that doesn't seem to fit with the actual results we get (e.g. 15 Milli-Seconds are measured by the timer, compared to actual 2 Seconds we measure "by hand")
So, it seems like there are two options:
- The code above is not timing correctly
- The code is timing correctly, but the charts in Grafana are somehow misleading.
Do you know if the timing code is correct?