vert.x icon indicating copy to clipboard operation
vert.x copied to clipboard

add a method in HttpClientReuqest to get the request metric

Open liubao68 opened this issue 4 years ago • 5 comments

Describe the feature

We are reporting metrics using VertxMetrics, and get metric object through

HttpClientRequest.connection().metric() .

This is fine to get TCPMetrics.

Request metric is accessable in HttpClientStream, but it is a protected member of HttpClientRequest, and we can not get the request metrics.

Use cases

Report request metrics like time taken in request begin and end.

Contribution

I am not sure if it is suitable to add a method Object metric() in HttpClientRequest, and I can create a PR if it is fine.

liubao68 avatar Jul 20 '21 09:07 liubao68

In vert.x 3.x, we get request begin & end by

    DefaultHttpSocketMetric httpSocketMetric = (DefaultHttpSocketMetric) ((ConnectionBase) clientRequest.connection())
        .metric();
    httpSocketMetric.getRequestBeginTime();
    httpSocketMetric.getRequestEndTime();

But can not find a way in 4.x.

Our usage is wrong in 3.x. And we find 4.x fixed this but can not get request metric.

liubao68 avatar Jul 20 '21 09:07 liubao68

Indeed in Vert.x 4 the metric object is held by HttpClientStream.

@vietj what do you think about exposing the metric object in HttpClientRequestBase? Something like:

  public Object metric() {
    return stream.metric();
  }

tsegismont avatar Jul 21 '21 14:07 tsegismont

what is the actual use case that is intended ?

vietj avatar Jul 22 '21 09:07 vietj

Get request metric can know request begin, request end, resonse begin and response end time, this is usefully for performance issues.

We need reporting time token between request send and response received and need access request metric to do this.

liubao68 avatar Jul 23 '21 01:07 liubao68

code snippet

        clientRequest.response().onComplete(asyncResult -> {
          ... ... after request succefullly, reporting metrics how long timeout between request end and response received
          handleResponse(asyncResult.result());
        });

protected void handleResponse(HttpClientResponse httpClientResponse) {
         ... ...
    clientResponse.bodyHandler(this::processResponseBody);
  }

  protected void processResponseBody(Buffer responseBuf) {
       DefaultHttpSocketMetric httpSocketMetric = (DefaultHttpSocketMetric) ((ConnectionBase) clientRequest.connection())
        .metric();  // TCPMetrics can be get from connection

    // TODO:  expect we can get request metric to calculate request end time
  }

liubao68 avatar Jul 23 '21 07:07 liubao68