Skip to content

Invalid response status in metrics for deferred methods #1102

@ilinas

Description

@ilinas

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();
        });
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions