3

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

OhadBasan
  • 797
  • 6
  • 15

1 Answers1

-1

One way to do it is with the ChannelHandlerContext attributeMap api.

What I would do is create some sort of statistics pojo, initialize it within the first handler and update it in your final handler.

A benefit of this is that optionally, each handler in the pipeline can track its individual performance and update the Attribute themselves.

pierater
  • 23
  • 6