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

如何通过Java实现Jetty HTTP2Client的连接与请求指标监测

How to Get Jetty HTTP2Client Metrics (Connections, Requests, Failures) Internally

Absolutely, you can track all these metrics directly within your Java application—no external tools like Wireshark or verbose debug logs needed. Jetty provides built-in, stable APIs to access this data, so reflection should only be a last resort for very old versions. Let's walk through the best approaches:

Jetty's HttpClient has built-in statistics support that covers most of your needs. First, enable statistics when setting up your client:

// Enable statistics on your HttpClient
httpClient.setStatisticsEnabled(true);

a. Track Per-Destination Connection Counts & Request Metrics

You can iterate over all destinations (host:port pairs) to get connection limits, active connections, total requests, and failures per target:

// Iterate through all target destinations
for (Destination destination : httpClient.getDestinations()) {
    String target = destination.getHost() + ":" + destination.getPort();
    DestinationStatistics destStats = destination.getStatistics();
    
    System.out.printf("=== Target: %s ===%n", target);
    System.out.printf("Active Connections: %d (Max: %d)%n", 
        destStats.getConnections(), destination.getMaxConnections());
    System.out.printf("Total Requests: %d | Successful: %d | Failed: %d%n%n",
        destStats.getRequests(), destStats.getSuccesses(), destStats.getFailures());
}

b. Track Request Counts Per HTTP/2 Connection

For HTTP/2, each connection maps to an HTTP/2 Session that handles multiple request streams. You can access these sessions to get per-connection request stats:

for (Destination destination : httpClient.getDestinations()) {
    String target = destination.getHost() + ":" + destination.getPort();
    System.out.printf("=== Target: %s - Per-Connection Stats ===%n", target);
    
    for (Connection connection : destination.getConnections()) {
        if (connection instanceof HTTP2Connection) {
            HTTP2Session session = ((HTTP2Connection) connection).getSession();
            System.out.printf("Connection ID: %s%n", connection.hashCode());
            System.out.printf("Active Requests (Streams): %d | Total Requests Handled: %d%n%n",
                session.getActiveStreams().size(), session.getStreamCount());
        }
    }
}

c. Track Detailed Failure Events

If you need granular details about why a request failed (e.g., connection timeout, HTTP error), add a Request.Listener to capture failure events:

httpClient.addRequestListener(new Request.Listener.Adapter() {
    @Override
    public void onFailure(Request request, Throwable failure) {
        String target = request.getDestination().getHost() + ":" + request.getDestination().getPort();
        // Log or count failures by target, error type, etc.
        System.out.printf("Failure on %s: %s%n", target, failure.getMessage());
    }
});

2. Fallback: Reflection (For Older Jetty Versions)

If you're stuck on a Jetty version that doesn't support the statistics APIs (pre-9.4), you can use reflection to access internal fields. Note: This is fragile—internal fields can change between Jetty versions, so use this only if you have no other option.

Example to get per-destination connection counts via reflection:

for (Destination destination : httpClient.getDestinations()) {
    String target = destination.getHost() + ":" + destination.getPort();
    try {
        // Access the internal connections field (field name may vary by version)
        Field connectionsField = destination.getClass().getDeclaredField("_connections");
        connectionsField.setAccessible(true);
        Set<Connection> connections = (Set<Connection>) connectionsField.get(destination);
        
        System.out.printf("Target %s: Active Connections = %d%n", target, connections.size());
    } catch (NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();
    }
}

Key Takeaways

  • Always prefer the native statistics APIs—they're designed for this purpose and maintain compatibility across Jetty versions.
  • Use HttpClientStatistics for global metrics, DestinationStatistics for per-target data, and HTTP2Session for per-connection request counts.
  • Reflection is a last resort—only use it if you can't upgrade to a Jetty version with proper stats support.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:34:25