This repository has been archived by the owner on Feb 13, 2019. It is now read-only.
/
RestMonitor.java
331 lines (295 loc) · 12.5 KB
/
RestMonitor.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
* This template file was generated by Dynatrace client.
* The Dynatrace community portal can be found here: http://community.dynatrace.com/
* For information how to publish a plugin please visit https://community.dynatrace.com/community/display/DL/How+to+add+a+new+plugin/
**/
package com.realdolmen.dynatrace.restmonitor;
import com.dynatrace.diagnostics.global.Constants;
import com.dynatrace.diagnostics.httpclient.api.DynaTraceHttpClient;
import com.dynatrace.diagnostics.httpclient.api.DynaTraceHttpClientBuilder;
import com.dynatrace.diagnostics.httpclient.api.DynaTraceHttpClientException;
import com.dynatrace.diagnostics.httpclient.api.enums.AuthMode;
import com.dynatrace.diagnostics.httpclient.api.enums.ContentTypeAndEncoding;
import com.dynatrace.diagnostics.httpclient.api.enums.RequestType;
import com.dynatrace.diagnostics.httpclient.api.enums.SSLCertificateMode;
import com.dynatrace.diagnostics.httpclient.api.enums.SSLHostnameVerification;
import com.dynatrace.diagnostics.httpclient.impl.CloseableDynaTraceHttpResponse;
import com.dynatrace.diagnostics.pdk.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.SSLException;
import org.apache.http.Header;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.util.EncodingUtils;
/**
* TODO: documentation
*
* @author heydenb
*/
public class RestMonitor implements Monitor {
private static final Logger log = Logger.getLogger(RestMonitor.class.getName());
private static final int READ_CHUNK_SIZE = 1024;
private static final String HTTP_1_0 = "1.0";
private static final int HTTP_CODE_MOVED_PERMANENT = 301;
private Config config;
private DynaTraceHttpClient httpClient;
@Override
public Status setup(MonitorEnvironment env) throws Exception {
Status status = new Status(Status.StatusCode.Success);
try {
config = new Config(env);
} catch (Exception ex) {
log.log(Level.FINE, "setup configuration failed", ex);
status.setStatusCode(Status.StatusCode.ErrorInternal);
status.setMessage("Setting up configuration failed: " + ex.getMessage() + "\nType: " + ex.getClass().getSimpleName() +
"\n");
status.setShortMessage("Setting up configuration failed: " + ex.getMessage());
status.setException(ex);
return status;
}
try {
setupHttpClient();
} catch (Exception ex) {
log.log(Level.FINE, "setup http client failed", ex);
status.setStatusCode(Status.StatusCode.ErrorInternal);
status.setMessage("Setting up HTTP client failed: " + ex.getMessage() + "\nType: " + ex.getClass().getSimpleName() +
"\n");
status.setShortMessage("Setting up HTTP client failed: " + ex.getMessage());
status.setException(ex);
return status;
}
try {
setupAuth(httpClient);
} catch (Exception ex) {
log.log(Level.FINE, "setup http authentication failed", ex);
status.setStatusCode(Status.StatusCode.ErrorInternal);
status.setMessage("Setting up HTTP authentication failed: " + ex.getMessage() + "\nType: " +
ex.getClass().getSimpleName() + "\n");
status.setShortMessage("HTTP authentication setup failed: " + ex.getMessage());
status.setException(ex);
return status;
}
try {
setupProxy(httpClient);
} catch (Exception ex) {
log.log(Level.FINE, "setup proxy failed", ex);
status.setStatusCode(Status.StatusCode.ErrorInternal);
status.setMessage("Settingproxy failed: " + ex.getMessage() + "\n");
status.setShortMessage("Proxy setup failed: " + ex.getMessage());
status.setException(ex);
return status;
}
return status;
}
@Override
public void teardown(MonitorEnvironment env) throws Exception {
if (httpClient == null)
return;
httpClient.close();
httpClient = null;
}
@Override
public Status execute(MonitorEnvironment env) throws Exception {
// propagate log level for execution, so no collector restart is needed
Logger.getLogger("org.apache.http.wire").setLevel(log.getLevel());
final Status status = new Status();
final MeasureConnection measureConnection = new MeasureConnection(env);
final StringBuilder messageBuffer = new StringBuilder("URL: ");
messageBuffer.append(config.url).append("\n");
// set dynaTrace tagging header (only timer name)
if (config.tagging) {
httpClient.addRequestHeader(Constants.HEADER_DYNATRACE, "NA=" + config.timerName);
}
CloseableDynaTraceHttpResponse response = null;
try {
log.info("Executing method: " + config.method + ", URI: " + config.url + ", with PostData: " +
(config.postData != null));
// connect
measureConnection.startMeasurement();
if (config.method == RequestType.POST && config.postData != null) {
response = httpClient.executeBigRequest(config.method, config.url, null, config.postData,
ContentTypeAndEncoding.TEXT_PLAIN_UTF8);
} else {
response = httpClient.executeBigRequest(config.method, config.url, null);
}
if (response.getStatusCode() == HTTP_CODE_MOVED_PERMANENT)
throw new DynaTraceHttpClientException(new ClientProtocolException(
"301 redirect reached, not supported for the REST Monitor."));
measureConnection.headerResponseReceived();
measureConnection.setHttpStatusCode(response.getStatusCode());
measureConnection.setHeaderSize(calculateHeaderSize(response));
log.info("http request succeed. code=" + response.getStatusCode());
// read response data (only if more than the header was requested)
if (config.method != RequestType.HEAD) {
try {
log.fine("start loading result content");
StringBuilder resultContent = loadResultContent(response, measureConnection);
log.fine("result content loaded ... ");
if (resultContent!=null){
if (log.isLoggable(Level.FINE)){
messageBuffer.append("content: \n" + resultContent.toString());
}
final MeasureCapturedValues measureCapturedValues = new MeasureCapturedValues(env);
measureCapturedValues.applyMeasuresToEnvironment(resultContent.toString(), config.format);
}
} catch (IOException e) {
log.log(Level.FINE, "reading content failed", e);
status.setException(e);
status.setStatusCode(Status.StatusCode.PartialSuccess);
status.setShortMessage("reading content failed: " + e.getMessage());
messageBuffer.append("Error while loading content: ").append(e.getClass().getSimpleName()).append(": ").append(
e.getMessage()).append("\n");
} catch (RestMonitorConfigurationException e) {
log.log(Level.FINE, "configuration fault", e);
status.setException(e);
status.setStatusCode(Status.StatusCode.ErrorInternalConfigurationProblem);
status.setShortMessage(e.getMessage());
messageBuffer.append("Error capturing measures: ").append(e.getClass().getSimpleName()).append(": ").append(
e.getMessage()).append("\n");
} catch (Exception e) {
log.log(Level.FINE, "configuration fault", e);
status.setException(e);
status.setStatusCode(Status.StatusCode.ErrorInternal);
status.setShortMessage(e.getMessage());
messageBuffer.append("Error internal: ").append(e.getClass().getSimpleName()).append(": ").append(
e.getMessage()).append("\n");
}
} else {
measureConnection.loadResponseContentFinished();
}
measureConnection.startClosing();
} catch (DynaTraceHttpClientException e) {
log.log(Level.FINE, "executing request failed", e);
status.setException(e);
status.setStatusCode(Status.StatusCode.PartialSuccess);
messageBuffer.append("Connection failed: ").append(e.getClass().getSimpleName()).append(": ").append(
e.getMessage()).append("\n");
if (e.getCause() != null) {
messageBuffer.append("Caused by: ").append(e.getCause().getClass().getSimpleName()).append(": ").append(
e.getCause().getMessage()).append("\n");
status.setShortMessage(e.getCause().getClass().getSimpleName() + ": " + e.getCause().getMessage());
if (e.getCause() instanceof SSLException) {
messageBuffer.append("SSL handshake failed, this may be caused by an incorrect certificate. Check 'Disable certificate validation' parameter to override this.\n");
} else if (e.getCause() instanceof ConnectTimeoutException) {
measureConnection.setConnectionTimedOut();
} else if (e.getCause() instanceof SocketTimeoutException) {
measureConnection.setSocketTimedOut();
}
} else {
status.setShortMessage(e.getClass().getSimpleName() + ": " + e.getMessage());
}
} catch (Exception e){
log.log(Level.SEVERE, "unknown error", e);
status.setException(e);
status.setStatusCode(Status.StatusCode.ErrorInternal);
messageBuffer.append("Caused by: ").append(e.getClass().getSimpleName()).append(": ").append(
e.getMessage()).append("\n");
e.fillInStackTrace();
for (StackTraceElement el : e.getStackTrace()){
messageBuffer.append(el.toString()).append("\n");
}
}
finally {
// always release the connection
if (response != null)
response.close();
measureConnection.closingFinished();
}
log.info("parsed!");
measureConnection.applyBinaryMeasuresToEnvironment();
if (status.getStatusCode() == Status.StatusCode.Success)
measureConnection.applyMeasuresToEnvironment();
status.setMessage(messageBuffer.toString());
return status;
}
/**
* loads the whole content out of the http response.
*
* @return null if there was no content in the http response OR config parameter contentMatch is disabled OR contentMatch is
* bytesMatch.
*/
private StringBuilder loadResultContent(CloseableDynaTraceHttpResponse response, MeasureConnection measureCollector) throws IOException {
InputStream inputStream = response.getResponseBody();
if (inputStream == null) {
return null;
}
int bytesRead;
byte[] data = new byte[READ_CHUNK_SIZE];
String charset = response.getCharset();
StringBuilder buf = new StringBuilder();
while ((bytesRead = inputStream.read(data)) > 0) {
buf.append(EncodingUtils.getString(data, 0, bytesRead, charset));
measureCollector.incrementInputSize(bytesRead);
}
inputStream.close();
measureCollector.loadResponseContentFinished();
return buf;
}
private void setupAuth(DynaTraceHttpClient httpClient) throws IllegalStateException, UnknownHostException {
if (config.serverAuth == AuthMethod.basic) {
httpClient.setUserCredentials(config.serverUsername, config.serverPassword);
httpClient.setPreemptiveAuthEnabled(config.serverAuthPreemptive);
if (config.serverAuthPreemptive) {
httpClient.setPreemptiveAuthMode(AuthMode.BASIC);
}
}
else if (config.serverAuth == AuthMethod.NTLM) {
String user = config.serverUsername;
String domain = "";
if (config.serverUsername != null) {
int idx = config.serverUsername.indexOf("\\");
if (idx > 0) {
user = config.serverUsername.substring(idx + 1);
domain = config.serverUsername.substring(0, idx);
}
}
httpClient.setNTLMUserCredentials(config.url.getHost(), config.url.getPort(), user, config.serverPassword, domain,
java.net.InetAddress.getLocalHost().getHostName());
}
}
private void setupProxy(DynaTraceHttpClient httpClient) {
if (!config.useProxy) {
return;
}
if (config.proxyAuth) {
httpClient.setProxy(config.proxyHost, config.proxyPort, config.proxyUsername, config.proxyPassword,
config.proxyAuthPreemptive);
} else {
httpClient.setProxy(config.proxyHost, config.proxyPort);
}
}
private void setupHttpClient() {
DynaTraceHttpClientBuilder builder = new DynaTraceHttpClientBuilder();
if (config.ignorecert) {
builder.certificateMode(SSLCertificateMode.TRUST_ALL);
builder.hostnameVerificationMode(SSLHostnameVerification.ALLOW_ALL);
} else {
builder.certificateMode(SSLCertificateMode.VERIFY_CHAIN);
builder.hostnameVerificationMode(SSLHostnameVerification.DEFAULT);
}
if (HTTP_1_0.equals(config.httpVersion)) {
builder.protocolVersion("HTTP", 1, 0);
} else {
builder.protocolVersion("HTTP", 1, 1);
}
builder.allowRedirects(false).maxRedirects(0);
builder.userAgent(config.userAgent);
builder.socketTimeout(config.socketTimeout);
builder.connectTimeout(config.connectionTimeout);
builder.connectionRequestTimeout(config.connectionTimeout);
httpClient = builder.build();
}
private int calculateHeaderSize(CloseableDynaTraceHttpResponse response) {
int headerLength = 0;
for (Header header : response.getResponseHeaders()) {
headerLength += header.getName().getBytes().length;
headerLength += header.getValue().getBytes().length;
}
return headerLength;
}
}