In Spring Cloud Gateway I would like to measure, how long downstream requests exactly take in order to compare it to complete time needed (downstream service time + processing time of Spring cloud Gateway).
I implemented a GlobalFilter
which does the measure right in front of NettyRoutingFilter
. This however also includes part of the event loop processing (as far as I understand). At least under load the times measured here do not correlate to the response times of a known downstream service.
Basic simplified approach:
class ProxyResponseTimeFilter
implements GlobalFilter, Ordered
{
@Override
public int getOrder()
{
// see NettyRoutingFilter
return Ordered.LOWEST_PRECEDENCE - 1;
}
@Override
public Mono<Void> filter(final ServerWebExchange exchange, final GatewayFilterChain chain)
{
return chain.filter(exchange).transformDeferred(request ->
{
final long start = System.nanoTime();
return request.doFinally(s ->
{
final long duration = System.nanoTime() - start;
System.out.println(exchange.getRequest().getPath() + " " + TimeUnit.NANOSECONDS.toMillis(duration));
});
});
}
}
I'm using Spring Cloud Hoxton.SR9.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…