I have written a ratpack handler that measures the execution time of a handler chain.
public class EndpointMetricsEmitter implements Handler {
public static final String HANDLE_DURATION_METRIC = "HANDLE_DURATION_MILLIS";
private final MetricContext baseContext;
private MonitoringRegistry registry;
public EndpointMetricsEmitter(MonitoringRegistry monitoringRegistry) {
this.registry = monitoringRegistry;
this.baseContext = MetricContext.newContext(REST);
}
@Override
public void handle(Context ctx) throws Exception {
Instant start = Instant.now();
ctx.next();
Instant end = Instant.now();
long dur = Duration.between(start, end).toMillis();
MetricContext endpointCtx = this.baseContext.withService(ctx.getPathBinding().getDescription());
this.registry.timer(endpointCtx, HANDLE_DURATION_METRIC, dur);
}
The problem is that I realised that ctx.next()
is an async method and it returns void
.
is there a way to invoke a function in when the last handler of the chain returns?
thank you