You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何监控Quarkus中PoolingHttpClientConnectionManager连接池及暴露指标

Quarkus中监控Apache HttpClient连接池的方案

一、直接获取连接池核心指标

PoolingHttpClientConnectionManager本身提供了原生API,可直接获取你需要的关键指标:

  • 最大连接数:调用getTotalStats().getMax()获取全局上限;若需按路由维度,用getStats(指定HttpRoute)获取单路由最大连接数
  • 当前活跃/已分配连接数:getTotalStats().getLeased()(分配出去的连接即为活跃状态)
  • 空闲连接数:getTotalStats().getAvailable()

示例代码:

@Inject
PoolingHttpClientConnectionManager connectionManager;

// 可在自定义监控端点或定时任务中调用
public Map<String, Integer> getPoolMetrics() {
    PoolStats totalStats = connectionManager.getTotalStats();
    return Map.of(
        "max_connections", totalStats.getMax(),
        "active_connections", totalStats.getLeased(),
        "idle_connections", totalStats.getAvailable()
    );
}

二、检测连接池溢出(请求等待空闲连接)

判断是否有请求因连接池满而等待,可通过两种方式实现:

  1. 拦截连接请求记录等待时长:
    在获取连接的逻辑中添加监控,当等待时间超过阈值时标记告警:
ConnectionRequest originalRequest = connectionManager.requestConnection(route, state);
ConnectionRequest monitoredRequest = new ConnectionRequest() {
    @Override
    public boolean cancel() {
        return originalRequest.cancel();
    }

    @Override
    public HttpClientConnection get(long timeout, TimeUnit timeUnit) throws InterruptedException, ExecutionException, ConnectionPoolTimeoutException {
        long start = System.currentTimeMillis();
        try {
            return originalRequest.get(timeout, timeUnit);
        } finally {
            long waitTime = System.currentTimeMillis() - start;
            // 自定义阈值,比如超过100ms则记录告警指标
            if (waitTime > 100) {
                Metrics.counter("http.client.connection.wait.over.threshold").increment();
            }
        }
    }
};
  1. 统计连接超时异常:
    当请求因连接池无空闲连接超时,会抛出ConnectionPoolTimeoutException,直接统计该异常的发生次数即可判断连接池是否溢出。

三、Quarkus中暴露Apache HttpClient池指标的推荐方式

若之前用Micrometer未获取到数据,通常是未正确完成指标绑定,以下是可行方案:

  1. 手动绑定Micrometer指标:
    通过PoolingHttpClientConnectionManagerMetricsBinder将连接池指标注册到Micrometer:
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.binder.httpcomponents.PoolingHttpClientConnectionManagerMetricsBinder;

@Singleton
public class PoolMetricsBinder {

    public PoolMetricsBinder(PoolingHttpClientConnectionManager connectionManager) {
        new PoolingHttpClientConnectionManagerMetricsBinder(connectionManager, "quarkus.rest.client.pool")
            .bindTo(Metrics.globalRegistry);
    }
}

绑定后会自动采集以下指标:

  • http.client.connections.max:最大连接数
  • http.client.connections.active:活跃连接数
  • http.client.connections.idle:空闲连接数
  • http.client.connections.pending:等待连接的请求数(直接对应连接池溢出场景)
  1. 启用Quarkus原生配置支持:
    若使用Quarkus REST Client的Apache HttpClient实现,确保版本在2.10+,并在application.properties中开启配置:
quarkus.micrometer.binder.http-client.enabled=true
quarkus.rest-client.use-apache-httpclient=true
  1. 暴露指标端点:
    开启Quarkus指标端点,即可通过/q/metrics获取所有采集到的指标:
quarkus.micrometer.export.prometheus.enabled=true
quarkus.http.expose=metrics

内容的提问来源于stack exchange,提问作者amine gh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 08:42:45