Skip to content

Commit 0011daa

Browse files
committed
Change metrics-httpclient to reflect changes in HttpClient 4.2.
1 parent 51894d9 commit 0011daa

File tree

4 files changed

+110
-96
lines changed

4 files changed

+110
-96
lines changed

metrics-httpclient/src/main/java/com/yammer/metrics/httpclient/InstrumentedClientConnManager.java

+49-21
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,72 @@
22

33
import com.yammer.metrics.Metrics;
44
import com.yammer.metrics.core.Gauge;
5+
import com.yammer.metrics.core.MetricsRegistry;
56
import org.apache.http.conn.ClientConnectionManager;
67
import org.apache.http.conn.scheme.SchemeRegistry;
8+
import org.apache.http.impl.conn.PoolingClientConnectionManager;
79
import org.apache.http.impl.conn.SchemeRegistryFactory;
8-
import org.apache.http.impl.conn.tsccm.ConnPoolByRoute;
9-
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
1010

1111
import java.util.concurrent.TimeUnit;
1212

1313
/**
1414
* A {@link ClientConnectionManager} which monitors the number of open connections.
1515
*/
16-
public class InstrumentedClientConnManager extends ThreadSafeClientConnManager {
17-
public InstrumentedClientConnManager(SchemeRegistry registry) {
18-
this(registry, -1, TimeUnit.MILLISECONDS);
19-
}
20-
16+
public class InstrumentedClientConnManager extends PoolingClientConnectionManager {
2117
public InstrumentedClientConnManager() {
2218
this(SchemeRegistryFactory.createDefault());
2319
}
2420

21+
public InstrumentedClientConnManager(SchemeRegistry registry) {
22+
this(registry, -1, TimeUnit.MILLISECONDS);
23+
}
24+
2525
public InstrumentedClientConnManager(SchemeRegistry registry,
2626
long connTTL,
2727
TimeUnit connTTLTimeUnit) {
28-
super(registry, connTTL, connTTLTimeUnit);
29-
Metrics.newGauge(ClientConnectionManager.class,
30-
"connections",
31-
new Gauge<Integer>() {
32-
@Override
33-
public Integer getValue() {
34-
// this acquires a lock on the connection pool; remove if contention sucks
35-
return getConnectionsInPool();
36-
}
37-
});
28+
this(Metrics.defaultRegistry(), registry, connTTL, connTTLTimeUnit);
3829
}
3930

40-
@Override
41-
protected ConnPoolByRoute createConnectionPool(long connTTL,
42-
TimeUnit connTTLTimeUnit) {
43-
return new InstrumentedConnByRoute(connOperator, connPerRoute, 20, connTTL, connTTLTimeUnit);
31+
public InstrumentedClientConnManager(MetricsRegistry metricsRegistry,
32+
SchemeRegistry registry,
33+
long connTTL,
34+
TimeUnit connTTLTimeUnit) {
35+
super(registry, connTTL, connTTLTimeUnit);
36+
metricsRegistry.newGauge(ClientConnectionManager.class,
37+
"available-connections",
38+
new Gauge<Integer>() {
39+
@Override
40+
public Integer getValue() {
41+
// this acquires a lock on the connection pool; remove if contention sucks
42+
return getTotalStats().getAvailable();
43+
}
44+
});
45+
metricsRegistry.newGauge(ClientConnectionManager.class,
46+
"leased-connections",
47+
new Gauge<Integer>() {
48+
@Override
49+
public Integer getValue() {
50+
// this acquires a lock on the connection pool; remove if contention sucks
51+
return getTotalStats().getLeased();
52+
}
53+
});
54+
metricsRegistry.newGauge(ClientConnectionManager.class,
55+
"max-connections",
56+
new Gauge<Integer>() {
57+
@Override
58+
public Integer getValue() {
59+
// this acquires a lock on the connection pool; remove if contention sucks
60+
return getTotalStats().getMax();
61+
}
62+
});
63+
metricsRegistry.newGauge(ClientConnectionManager.class,
64+
"pending-connections",
65+
new Gauge<Integer>() {
66+
@Override
67+
public Integer getValue() {
68+
// this acquires a lock on the connection pool; remove if contention sucks
69+
return getTotalStats().getPending();
70+
}
71+
});
4472
}
4573
}

metrics-httpclient/src/main/java/com/yammer/metrics/httpclient/InstrumentedConnByRoute.java

-36
This file was deleted.

metrics-httpclient/src/main/java/com/yammer/metrics/httpclient/InstrumentedHttpClient.java

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.yammer.metrics.httpclient;
22

3+
import com.yammer.metrics.Metrics;
4+
import com.yammer.metrics.core.MetricsRegistry;
35
import org.apache.commons.logging.Log;
46
import org.apache.commons.logging.LogFactory;
57
import org.apache.http.ConnectionReuseStrategy;
@@ -15,16 +17,25 @@
1517
public class InstrumentedHttpClient extends DefaultHttpClient {
1618
private final Log log = LogFactory.getLog(getClass());
1719

18-
public InstrumentedHttpClient(InstrumentedClientConnManager manager, HttpParams params) {
20+
private final MetricsRegistry registry;
21+
22+
public InstrumentedHttpClient(MetricsRegistry registry,
23+
InstrumentedClientConnManager manager,
24+
HttpParams params) {
1925
super(manager, params);
26+
this.registry = registry;
27+
}
28+
29+
public InstrumentedHttpClient(InstrumentedClientConnManager manager, HttpParams params) {
30+
this(Metrics.defaultRegistry(), manager, params);
2031
}
2132

2233
public InstrumentedHttpClient(HttpParams params) {
23-
super(new InstrumentedClientConnManager(), params);
34+
this(new InstrumentedClientConnManager(), params);
2435
}
2536

2637
public InstrumentedHttpClient() {
27-
super(new InstrumentedClientConnManager());
38+
this(null);
2839
}
2940

3041
@Override
@@ -36,11 +47,12 @@ protected RequestDirector createClientRequestDirector(HttpRequestExecutor reques
3647
HttpProcessor httpProcessor,
3748
HttpRequestRetryHandler retryHandler,
3849
RedirectStrategy redirectStrategy,
39-
AuthenticationHandler targetAuthHandler,
40-
AuthenticationHandler proxyAuthHandler,
41-
UserTokenHandler stateHandler,
50+
AuthenticationStrategy targetAuthStrategy,
51+
AuthenticationStrategy proxyAuthStrategy,
52+
UserTokenHandler userTokenHandler,
4253
HttpParams params) {
4354
return new InstrumentedRequestDirector(
55+
registry,
4456
log,
4557
requestExec,
4658
conman,
@@ -50,9 +62,9 @@ protected RequestDirector createClientRequestDirector(HttpRequestExecutor reques
5062
httpProcessor,
5163
retryHandler,
5264
redirectStrategy,
53-
targetAuthHandler,
54-
proxyAuthHandler,
55-
stateHandler,
65+
targetAuthStrategy,
66+
proxyAuthStrategy,
67+
userTokenHandler,
5668
params);
5769
}
5870
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.yammer.metrics.httpclient;
22

3-
import com.yammer.metrics.Metrics;
3+
import com.yammer.metrics.core.MetricsRegistry;
44
import com.yammer.metrics.core.Timer;
55
import com.yammer.metrics.core.TimerContext;
66
import org.apache.commons.logging.Log;
@@ -22,21 +22,20 @@ class InstrumentedRequestDirector extends DefaultRequestDirector {
2222
OPTIONS = "OPTIONS", DELETE = "DELETE", TRACE = "TRACE",
2323
CONNECT = "CONNECT", MOVE = "MOVE", PATCH = "PATCH";
2424

25-
private static final Timer GET_TIMER = Metrics.newTimer(HttpClient.class, "get-requests");
26-
private static final Timer POST_TIMER = Metrics.newTimer(HttpClient.class, "post-requests");
27-
private static final Timer HEAD_TIMER = Metrics.newTimer(HttpClient.class, "head-requests");
28-
private static final Timer PUT_TIMER = Metrics.newTimer(HttpClient.class, "put-requests");
29-
private static final Timer DELETE_TIMER = Metrics.newTimer(HttpClient.class, "delete-requests");
30-
private static final Timer OPTIONS_TIMER = Metrics.newTimer(HttpClient.class,
31-
"options-requests");
32-
private static final Timer TRACE_TIMER = Metrics.newTimer(HttpClient.class, "trace-requests");
33-
private static final Timer CONNECT_TIMER = Metrics.newTimer(HttpClient.class,
34-
"connect-requests");
35-
private static final Timer MOVE_TIMER = Metrics.newTimer(HttpClient.class, "move-requests");
36-
private static final Timer PATCH_TIMER = Metrics.newTimer(HttpClient.class, "patch-requests");
37-
private static final Timer OTHER_TIMER = Metrics.newTimer(HttpClient.class, "other-requests");
25+
private final Timer getTimer;
26+
private final Timer postTimer;
27+
private final Timer headTimer;
28+
private final Timer putTimer;
29+
private final Timer deleteTimer;
30+
private final Timer optionsTimer;
31+
private final Timer traceTimer;
32+
private final Timer connectTimer;
33+
private final Timer moveTimer;
34+
private final Timer patchTimer;
35+
private final Timer otherTimer;
3836

39-
InstrumentedRequestDirector(Log log,
37+
InstrumentedRequestDirector(MetricsRegistry registry,
38+
Log log,
4039
HttpRequestExecutor requestExec,
4140
ClientConnectionManager conman,
4241
ConnectionReuseStrategy reustrat,
@@ -45,8 +44,8 @@ class InstrumentedRequestDirector extends DefaultRequestDirector {
4544
HttpProcessor httpProcessor,
4645
HttpRequestRetryHandler retryHandler,
4746
RedirectStrategy redirectStrategy,
48-
AuthenticationHandler targetAuthHandler,
49-
AuthenticationHandler proxyAuthHandler,
47+
AuthenticationStrategy targetAuthStrategy,
48+
AuthenticationStrategy proxyAuthStrategy,
5049
UserTokenHandler userTokenHandler,
5150
HttpParams params) {
5251
super(log,
@@ -58,10 +57,21 @@ class InstrumentedRequestDirector extends DefaultRequestDirector {
5857
httpProcessor,
5958
retryHandler,
6059
redirectStrategy,
61-
targetAuthHandler,
62-
proxyAuthHandler,
60+
targetAuthStrategy,
61+
proxyAuthStrategy,
6362
userTokenHandler,
6463
params);
64+
getTimer = registry.newTimer(HttpClient.class, "get-requests");
65+
postTimer = registry.newTimer(HttpClient.class, "post-requests");
66+
headTimer = registry.newTimer(HttpClient.class, "head-requests");
67+
putTimer = registry.newTimer(HttpClient.class, "put-requests");
68+
deleteTimer = registry.newTimer(HttpClient.class, "delete-requests");
69+
optionsTimer = registry.newTimer(HttpClient.class, "options-requests");
70+
traceTimer = registry.newTimer(HttpClient.class, "trace-requests");
71+
connectTimer = registry.newTimer(HttpClient.class, "connect-requests");
72+
moveTimer = registry.newTimer(HttpClient.class, "move-requests");
73+
patchTimer = registry.newTimer(HttpClient.class, "patch-requests");
74+
otherTimer = registry.newTimer(HttpClient.class, "other-requests");
6575
}
6676

6777
@Override
@@ -77,26 +87,26 @@ public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext co
7787
private Timer timer(HttpRequest request) {
7888
final String method = request.getRequestLine().getMethod();
7989
if (GET.equalsIgnoreCase(method)) {
80-
return GET_TIMER;
90+
return getTimer;
8191
} else if (POST.equalsIgnoreCase(method)) {
82-
return POST_TIMER;
92+
return postTimer;
8393
} else if (PUT.equalsIgnoreCase(method)) {
84-
return PUT_TIMER;
94+
return putTimer;
8595
} else if (HEAD.equalsIgnoreCase(method)) {
86-
return HEAD_TIMER;
96+
return headTimer;
8797
} else if (DELETE.equalsIgnoreCase(method)) {
88-
return DELETE_TIMER;
98+
return deleteTimer;
8999
} else if (OPTIONS.equalsIgnoreCase(method)) {
90-
return OPTIONS_TIMER;
100+
return optionsTimer;
91101
} else if (TRACE.equalsIgnoreCase(method)) {
92-
return TRACE_TIMER;
102+
return traceTimer;
93103
} else if (CONNECT.equalsIgnoreCase(method)) {
94-
return CONNECT_TIMER;
104+
return connectTimer;
95105
} else if (PATCH.equalsIgnoreCase(method)) {
96-
return PATCH_TIMER;
106+
return patchTimer;
97107
} else if (MOVE.equalsIgnoreCase(method)) {
98-
return MOVE_TIMER;
108+
return moveTimer;
99109
}
100-
return OTHER_TIMER;
110+
return otherTimer;
101111
}
102112
}

0 commit comments

Comments
 (0)