How to get the bytesIn/bytesOut on a request basis using Jetty Client

Viewed 105

I am using Jetty Client 11 and would like to compute for each request the sent/received bytes.

I am using High Level HttpClient . I have read this documentation but I don't see any information on how to do that:

I see there is a way to have this information by doing:

        ConnectionStatistics connectionStatistics = new ConnectionStatistics();
        httpClient.addBean(connectionStatistics);

Then:

        Request request = httpClient.newRequest(uri)
                .version(HttpVersion.HTTP_2)
                .timeout(CONNECTION_TIMEOUT_SECONDS, TimeUnit.SECONDS);
        request.send(listener);
        ConnectionStatistics connectionStatistics = httpClient.getBean(ConnectionStatistics.class);
        System.out.println(connectionStatistics.getSentBytes()+"/"+ connectionStatistics.getReceivedBytes());

And I must be doing something wrong as it does not even work on a global basis, it always gives 0.

And anyway, I don't see how to make it work on a request basis.

EDIT:

I tried @sbordet answer:

httpClient.newRequest(...)
  .onRequestContent((req, buf) -> requestBytes.add(buf.remaining()))
  ...
  .onResponseContent((res, buf) -> responseBytes.add(buf.remaining()))
  ...
  send(...);

and it works fine for response. BUT for request, I would like to measure the bytes sent, it always give 0 (Note I only have gets without Content) while I would expect to have the raw size (the Raw HTTP request bytes including header size ...)

1 Answers

You can count request bytes using Request.ContentListener and response bytes using Response.ContentListener:

httpClient.newRequest(...)
  .onRequestContent((req, buf) -> requestBytes.add(buf.remaining()))
  ...
  .onResponseContent((res, buf) -> responseBytes.add(buf.remaining()))
  ...
  send(...);
Related