I noticed that the Metrics module would report all responses as status 200 for deferred methods. It appears that this happens because InstrumentedHandler puts the meter in a finally block, and ResponseImpl throws a DeferredExecution exception as a way to handle responses. This results in the finally block being executed before the response has a status set, and it just defaults to 200.
I came up with this alternative implementation for the InstrumentedHandler (inspired by the RequestLogger) that seems to work. Please consider incorporating this into Jooby code.
public class InstrumentedHandler implements Route.Handler {
@Override
public void handle(final Request req, final Response rsp) throws Throwable {
MetricRegistry registry = req.require(MetricRegistry.class);
Counter counter = registry.counter("request.actives");
Timer.Context timer = registry.timer("request").time();
counter.inc();
rsp.complete((ereq, ersp, x) -> {
timer.stop();
counter.dec();
Meter meter = registry.meter("responses." + rsp.status().orElse(Status.OK).value());
meter.mark();
});
}
}
I noticed that the Metrics module would report all responses as status 200 for deferred methods. It appears that this happens because
InstrumentedHandlerputs the meter in a finally block, andResponseImplthrows aDeferredExecutionexception as a way to handle responses. This results in the finally block being executed before the response has a status set, and it just defaults to 200.I came up with this alternative implementation for the
InstrumentedHandler(inspired by theRequestLogger) that seems to work. Please consider incorporating this into Jooby code.