I don't know my approach is correct or not? Also, please let me know if there is any other work-around to achieve this.
Is it possible using WebFilter?
package request.middlewares;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;
@Component
public class ResponseTimeCalculator implements WebFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
// is it possible here?
exchange.getResponse().getHeaders().add("X-Runtime", "10ms");
return chain.filter(exchange);
}
}
If you're using Spring Boot, you should use
spring-boot-starter-actuator
for that.If you're not using Spring Boot, you should still take a look at Micrometer, which instruments Spring WebFlux to gather that kind of data.
Note that there are many pitfalls to your approach:
In any case, you can take a look at Spring Boot's
MetricsWebFilter
(here) to check how this is achieved with Micrometer.