javaspring-bootspring-webclient

How to get PoolStats for WebClient for logging in Spring Boot 3


I converted the RestTemplate to WebClient in my Spring Boot 3 project. In RestTemplate we were using PoolStats for logging metrics of leased, pending, available, max stats. (Code below)

    private void printHttpClientStats(String qualifier, PoolingHttpClientConnectionManager httpClientConnectionManager) {

        final PoolStats totalStats = httpClientConnectionManager.getTotalStats();
        log.info("HttpClient - {}: {}", qualifier, totalStats);
    }

However after switching to WebClient I couldn't be able to find how to get these info and log it the same way. How can I do this in WebClient?

I find this similar question but there are no answers to this one: How to log thread pool metrics for Spring WebClient?


Solution

  • I needed to enable metrics in ConnectionProvider

    ConnectionProvider provider = ConnectionProvider.builder("custom")
                    .maxConnections(maxConnTotal)
                    .metrics(true)
                    .maxLifeTime(Duration.ofSeconds(5))
                    .build();
    

    Then I was able to see logs from MeterRegistry object (repo)

    Object[] meters = repo.getMeters().stream().filter(meter -> meter.getId().getName().contains("reactor")).toArray();
    

    Reference: https://projectreactor.io/docs/netty/release/reference/index.html#_metrics_5