hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: HttpConnection.getMetrics() per request?
Date Mon, 31 Mar 2014 09:28:14 GMT
On Sat, 2014-03-29 at 07:01 -0700, marceau wrote:
> I would like to get bytes sent & received for each get request in a service.
> 
> I am aware of the issue in
> https://issues.apache.org/jira/browse/HTTPCLIENT-1081 with HEAD requests,
> and any request where getMetrics() is called after the connection has been
> returned to the pool.
> 
> However, I get the impression that, as I extract the desired information
> even before I process the entity in the response, I am not affected by that
> problem.
> 
> I AM however affected by the fact that the metrics seem to represent the
> total for that connection, rather than the total for the request-response
> round that was just completed. That is a problem when working with
> concurrent system with reused connections.
> 
> I thought I might try to work around this by caching the metrics before the
> request as well as after, and saving the difference. This is, however, not
> possible. I get a connectionshutdownexception (Exception in thread "3"
> org.apache.http.impl.conn.ConnectionShutdownException), which I'm guessing
> is down to the fact that I'm basically trying to use a connection that is at
> that time still used by another thread (as it may have been leased to
> another thread by the pool).
> 
> I'd be grateful for any pointers or guidance. 
> 
> 

Connection metrics were initially designed to enable custom connection
re-use strategies (like re-use for n requests or n bytes transferred)
and as such are pretty adequate. They were never intended to be used for
per request stats.

Theoretically one can derive per request stats from connection metrics a
custom request interceptor and a custom re-use strategy (see code
snippet below) but unfortunately it only works with persistent
connections. With non-persistent connections metrics are discarded
before they could be read by the custom re-use strategy. Sadly this
approach cannot be utilized until HTTPCLIENT-1081 is resolved.

Oleg

---
CloseableHttpClient client = HttpClients.custom()
        .addInterceptorFirst(new HttpRequestInterceptor() {
            @Override
            public void process(
                    final HttpRequest request,
                    final HttpContext context) throws HttpException,
IOException {
                HttpClientContext clientContext =
HttpClientContext.adapt(context);
                HttpConnection conn = clientContext.getConnection();
                HttpHost targetHost = clientContext.getTargetHost();
                System.out.println("target:" + targetHost);
                if (conn != null) {
                    HttpConnectionMetrics metrics = conn.getMetrics();
                    System.out.println("bytes sent:" +
metrics.getSentBytesCount());
                    System.out.println("bytes received:" +
metrics.getReceivedBytesCount());
                }
            }
        })
        .setConnectionReuseStrategy(new DefaultConnectionReuseStrategy()
{

            @Override
            public boolean keepAlive(
                    final HttpResponse response, final HttpContext
context) {
                HttpClientContext clientContext =
HttpClientContext.adapt(context);
                HttpConnection conn = clientContext.getConnection();
                HttpConnectionMetrics metrics = conn.getMetrics();
                System.out.println("bytes sent:" +
metrics.getSentBytesCount());HTTPCLIENT-1081
                System.out.println("bytes received:" +
metrics.getReceivedBytesCount());
                return super.keepAlive(response, context);
            }
        })
        .build();
CloseableHttpResponse response = client.execute(new
HttpGet("http://www.google.com/"));
try {
} finally {
    response.close();
}
---



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message