javajettyhttp2jetty-9jetty-httpclient

Instrument Jetty HttpClient for Http2


I m using Jetty's http2client to make synchronous calls to my server, my sample program is a follows,

Client part

Security.addProvider(new OpenSSLProvider());
SslContextFactory sslContextFactory = new SslContextFactory(true);
sslContextFactory.setProvider("Conscrypt");
sslContextFactory.setProtocol("TLSv1.3");
HTTP2Client http2Client = new HTTP2Client();
http2Client.setConnectTimeout(5000);
http2Client.setIdleTimeout(5000);

HttpClient httpClient = new org.eclipse.jetty.client.HttpClient(new HttpClientTransportOverHTTP2(http2Client), sslContextFactory);
httpClient.setMaxConnectionsPerDestination(20);
httpClient.setMaxRequestsQueuedPerDestination(100);
httpClient.setConnectTimeout(5000);
httpClient.addBean(sslContextFactory);
httpClient.start();

Request Part

Request request = httpClient.POST("my url goes here");
request.header(HttpHeader.CONTENT_TYPE, "application/json");
request.content(new StringContentProvider("xmlRequest PayLoad goes here","utf-8"));
ContentResponse response = request.send();
String res = new String(response.getContent());

I need to instrument to get metrics like number of connections per destination, number of requests per connections, number of failed transactions, etc.

My application runs in a server where using wireshark or any other tcp tool is restricted. so I need to get this data within java. Enabling debug logs of jetty is not viable as it writes GBs of data.

Is there a way to get these metrics either by some util or by java reflection?

Thanks in Advance


Solution

  • http2Client.setMaxConcurrentPushedStreams(1000);

    This is way too big, it's unlikely the server will push 1000 concurrent streams.

    http2Client.setConnectTimeout(30);

    http2Client.setIdleTimeout(5);

    The timeouts are measured in milliseconds, so these values are way too small. I also recommend the idle timeout to be a larger value than 5000 milliseconds, something like 20000-30000 is typically better.

    String res = new String(response.getContent());

    This is wrong, as you don't take into account the response charset. Use instead response.getContentAsString().

    As for the metrics, you can use JMX and extract a number of metrics using a JMX console (or via standard JMX APIs). To setup JMX for HttpClient you can do this:

    MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
    MBeanContainer mbeanContainer = new MBeanContainer(mbeanServer);
    httpClient.addBean(mbeanContainer);
    

    The code above will export the HttpClient components to JMX and there you can query the various components for the metrics you are interested in.