如何记录 Spring WebClient 的线程池指标?

问题描述 投票:0回答:1

有没有办法获取线程池指标以将其记录(导出)到 Spring WebClient 的石墨仪表板。

有一种方法可以通过配置 PoolingHttpClientConnectionManager 对 Rest Template 执行类似的操作。

 public Map<String, Metric> getMetrics() {
    final Map<String, Metric> gauges = new HashMap<>();

    for (Entry<String, PoolingHttpClientConnectionManager> entry : connectionManagerMap
        .entrySet()) {
      gauges.put(entry.getKey() + ".leasedConnections",
          (Gauge<Integer>) () -> entry.getValue().getTotalStats().getLeased());
      gauges.put(entry.getKey() + ".maxConnections",
          (Gauge<Integer>) () -> entry.getValue().getTotalStats().getMax());
      gauges.put(entry.getKey() + ".pendingConnections",
          (Gauge<Integer>) () -> entry.getValue().getTotalStats().getPending());
      gauges.put(entry.getKey() + ".availableConnections",
          (Gauge<Integer>) () -> entry.getValue().getTotalStats().getAvailable());
    }

    return gauges;
  }

对于 WebClient,我不确定如何获取 HttpThreadPool,我通过以下方式创建了 WebClient 对象。

WebClient webClient = WebClient.builder()
            .baseUrl(downstream.getServiceLocation())
            .clientConnector(clientHttpConnector)
            .exchangeStrategies(exchangeStrategies)
            .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
            .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML_VALUE)
            .build();

**and clientHttpConnector was created:**

public ClientHttpConnector lbosClientHttpConnector() {
        return new ReactorClientHttpConnector(options -> {
            options.option(ChannelOption.SO_TIMEOUT, applicationConfiguration.getDownstreams().getHttpClient().getSocketTimeout());
            options.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, applicationConfiguration.getDownstreams().getHttpClient().getKeepAliveTimeoutInMs());
            options.poolResources(PoolResources.fixed("HTTP_CONNECTION_POOL", applicationConfiguration.getDownstreams().getHttpClient().getMaxConnections()));
        });
    }

请提供解决方案或指导我解决。

java spring threadpool webclient metrics
1个回答
0
投票

在 Spring Boot 2.7.18 中,我能够通过这种方式从

ConnectionProvider
获取连接池指标:

首先配置

ConnectionProvider
实例:

var connectionProvider = ConnectionProvider.builder("myConnectionPool")
  .maxConnections(maxConnections)
  .metrics(true) // enables metrics to be gathered and reported to Metrics.globalRegistry by default
  .build();

然后创建

WebClient

var httpClient = HttpClient.create(connectionProvider);
var webClient = WebClient.builder()
  .clientConnector(new ReactorClientHttpConnector(httpClient))
  .build();

然后在一个单独的类中,定期调用该类来报告连接池指标:

private int getIntMetricValue(String poolName, String metricName) {
  var gauge = Metrics.globalRegistry.find(metricName).tags("name", poolName).gauge();
  return gauge != null ? (int) gauge.value() : 0;
}

var activeConnectionsCount = getIntMetricValue("myConnectionPool", "reactor.netty.connection.provider.active.connections");
var pendingConnectionsCount = getIntMetricValue("myConnectionPool", "reactor.netty.connection.provider.pending.connections");

指标名称在 Reactor Netty 文档中定义(在“池化 ConnectionProvider 指标”部分下)

© www.soinside.com 2019 - 2024. All rights reserved.