This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
InstrumentedRequestDirector.java
128 lines (120 loc) · 5.65 KB
/
InstrumentedRequestDirector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Sonatype Nexus (TM) Open Source Version
* Copyright (c) 2007-2012 Sonatype, Inc.
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
*
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
* Eclipse Foundation. All other trademarks are the property of their respective owners.
*/
package org.sonatype.nexus.apachehttpclient;
import com.yammer.metrics.core.MetricsRegistry;
import com.yammer.metrics.core.Timer;
import com.yammer.metrics.core.TimerContext;
import org.apache.commons.logging.Log;
import org.apache.http.*;
import org.apache.http.client.*;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.impl.client.DefaultRequestDirector;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpProcessor;
import org.apache.http.protocol.HttpRequestExecutor;
import java.io.IOException;
// NOTE: Duplicated and unchanged from original 2.2.0 source to support change in InstrumentedHttpClient
// NOTE: Should get this changes into metrics-httpclient and avoid needing this
class InstrumentedRequestDirector
extends DefaultRequestDirector {
private final static String GET = "GET", POST = "POST", HEAD = "HEAD", PUT = "PUT",
OPTIONS = "OPTIONS", DELETE = "DELETE", TRACE = "TRACE",
CONNECT = "CONNECT", MOVE = "MOVE", PATCH = "PATCH";
private final Timer getTimer;
private final Timer postTimer;
private final Timer headTimer;
private final Timer putTimer;
private final Timer deleteTimer;
private final Timer optionsTimer;
private final Timer traceTimer;
private final Timer connectTimer;
private final Timer moveTimer;
private final Timer patchTimer;
private final Timer otherTimer;
InstrumentedRequestDirector(MetricsRegistry registry,
Log log,
HttpRequestExecutor requestExec,
ClientConnectionManager conman,
ConnectionReuseStrategy reustrat,
ConnectionKeepAliveStrategy kastrat,
HttpRoutePlanner rouplan,
HttpProcessor httpProcessor,
HttpRequestRetryHandler retryHandler,
RedirectStrategy redirectStrategy,
AuthenticationStrategy targetAuthStrategy,
AuthenticationStrategy proxyAuthStrategy,
UserTokenHandler userTokenHandler,
HttpParams params) {
super(log,
requestExec,
conman,
reustrat,
kastrat,
rouplan,
httpProcessor,
retryHandler,
redirectStrategy,
targetAuthStrategy,
proxyAuthStrategy,
userTokenHandler,
params);
getTimer = registry.newTimer(HttpClient.class, "get-requests");
postTimer = registry.newTimer(HttpClient.class, "post-requests");
headTimer = registry.newTimer(HttpClient.class, "head-requests");
putTimer = registry.newTimer(HttpClient.class, "put-requests");
deleteTimer = registry.newTimer(HttpClient.class, "delete-requests");
optionsTimer = registry.newTimer(HttpClient.class, "options-requests");
traceTimer = registry.newTimer(HttpClient.class, "trace-requests");
connectTimer = registry.newTimer(HttpClient.class, "connect-requests");
moveTimer = registry.newTimer(HttpClient.class, "move-requests");
patchTimer = registry.newTimer(HttpClient.class, "patch-requests");
otherTimer = registry.newTimer(HttpClient.class, "other-requests");
}
@Override
public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) throws HttpException, IOException {
final TimerContext timerContext = timer(request).time();
try {
return super.execute(target, request, context);
} finally {
timerContext.stop();
}
}
private Timer timer(HttpRequest request) {
final String method = request.getRequestLine().getMethod();
if (GET.equalsIgnoreCase(method)) {
return getTimer;
} else if (POST.equalsIgnoreCase(method)) {
return postTimer;
} else if (PUT.equalsIgnoreCase(method)) {
return putTimer;
} else if (HEAD.equalsIgnoreCase(method)) {
return headTimer;
} else if (DELETE.equalsIgnoreCase(method)) {
return deleteTimer;
} else if (OPTIONS.equalsIgnoreCase(method)) {
return optionsTimer;
} else if (TRACE.equalsIgnoreCase(method)) {
return traceTimer;
} else if (CONNECT.equalsIgnoreCase(method)) {
return connectTimer;
} else if (PATCH.equalsIgnoreCase(method)) {
return patchTimer;
} else if (MOVE.equalsIgnoreCase(method)) {
return moveTimer;
}
return otherTimer;
}
}