use of io.airlift.http.client.jetty.HttpClientLogger.RequestInfo in project airlift by airlift.
the class TestHttpClientLogger method testClientLogNoResponse.
@Test
public void testClientLogNoResponse() throws Exception {
String method = "GET";
URI uri = new URI("http://www.google.com");
long requestTimestamp = System.currentTimeMillis();
HttpFields headers = new HttpFields();
headers.add(TRACETOKEN_HEADER, "test-token");
Request request = new TestRequest(HTTP_1_1, method, uri, headers);
DefaultHttpClientLogger logger = new DefaultHttpClientLogger(file.getAbsolutePath(), 1, 256, new DataSize(1, MEGABYTE), Long.MAX_VALUE, false);
RequestInfo requestInfo = RequestInfo.from(request, requestTimestamp);
ResponseInfo responseInfo = ResponseInfo.failed(Optional.empty(), Optional.of(new TimeoutException("timeout")));
logger.log(requestInfo, responseInfo);
logger.close();
String actual = Files.toString(file, UTF_8);
String[] columns = actual.trim().split("\\t");
assertEquals(columns[0], ISO_FORMATTER.format(Instant.ofEpochMilli(requestTimestamp)));
assertEquals(columns[1], HTTP_1_1.toString());
assertEquals(columns[2], method);
assertEquals(columns[3], uri.toString());
assertEquals(columns[4], getFailureReason(responseInfo).get());
assertEquals(columns[5], Integer.toString(NO_RESPONSE));
assertEquals(columns[6], Long.toString(responseInfo.getResponseTimestampMillis() - requestTimestamp));
assertEquals(columns[7], "test-token");
}
use of io.airlift.http.client.jetty.HttpClientLogger.RequestInfo in project airlift by airlift.
the class TestHttpClientLogger method testClientLog.
@Test
public void testClientLog() throws Exception {
String method = "GET";
URI uri = new URI("http://www.google.com");
int status = 200;
long responseSize = 345;
long requestTimestamp = System.currentTimeMillis();
HttpFields headers = new HttpFields();
headers.add(TRACETOKEN_HEADER, "test-token");
Request request = new TestRequest(HTTP_2, method, uri, headers);
Response response = new TestResponse(status);
DefaultHttpClientLogger logger = new DefaultHttpClientLogger(file.getAbsolutePath(), 1, 256, new DataSize(1, MEGABYTE), Long.MAX_VALUE, false);
RequestInfo requestInfo = RequestInfo.from(request, requestTimestamp);
ResponseInfo responseInfo = ResponseInfo.from(Optional.of(response), responseSize);
logger.log(requestInfo, responseInfo);
logger.close();
String actual = Files.toString(file, UTF_8);
String[] columns = actual.trim().split("\\t");
assertEquals(columns[0], ISO_FORMATTER.format(Instant.ofEpochMilli(requestTimestamp)));
assertEquals(columns[1], HTTP_2.toString());
assertEquals(columns[2], method);
assertEquals(columns[3], uri.toString());
assertEquals(columns[4], Integer.toString(status));
assertEquals(columns[5], Long.toString(responseSize));
assertEquals(columns[6], Long.toString(responseInfo.getResponseTimestampMillis() - requestTimestamp));
assertEquals(columns[7], "test-token");
}
use of io.airlift.http.client.jetty.HttpClientLogger.RequestInfo in project airlift by airlift.
the class JettyHttpClient method execute.
@Override
public <T, E extends Exception> T execute(Request request, ResponseHandler<T, E> responseHandler) throws E {
long requestStart = System.nanoTime();
// apply filters
request = applyRequestFilters(request);
// create jetty request and response listener
HttpRequest jettyRequest = buildJettyRequest(request);
InputStreamResponseListener listener = new InputStreamResponseListener() {
@Override
public void onContent(Response response, ByteBuffer content) {
// ignore empty blocks
if (content.remaining() == 0) {
return;
}
super.onContent(response, content);
}
};
RequestInfo requestInfo = RequestInfo.from(jettyRequest, System.currentTimeMillis());
// fire the request
jettyRequest.send(listener);
// wait for response to begin
Response response;
try {
response = listener.get(httpClient.getIdleTimeout(), MILLISECONDS);
} catch (InterruptedException e) {
stats.recordRequestFailed();
requestLogger.log(requestInfo, ResponseInfo.failed(Optional.empty(), Optional.of(e)));
jettyRequest.abort(e);
Thread.currentThread().interrupt();
return responseHandler.handleException(request, e);
} catch (TimeoutException e) {
stats.recordRequestFailed();
requestLogger.log(requestInfo, ResponseInfo.failed(Optional.empty(), Optional.of(e)));
jettyRequest.abort(e);
return responseHandler.handleException(request, e);
} catch (ExecutionException e) {
stats.recordRequestFailed();
requestLogger.log(requestInfo, ResponseInfo.failed(Optional.empty(), Optional.of(e)));
Throwable cause = e.getCause();
if (cause instanceof Exception) {
return responseHandler.handleException(request, (Exception) cause);
} else if ((cause instanceof NoClassDefFoundError) && cause.getMessage().endsWith("ALPNClientConnection")) {
return responseHandler.handleException(request, new RuntimeException("HTTPS cannot be used when HTTP/2 is enabled", cause));
} else {
return responseHandler.handleException(request, new RuntimeException(cause));
}
}
// process response
long responseStart = System.nanoTime();
JettyResponse jettyResponse = null;
T value;
try {
jettyResponse = new JettyResponse(response, listener.getInputStream());
value = responseHandler.handle(request, jettyResponse);
} finally {
if (recordRequestComplete) {
recordRequestComplete(stats, request, requestStart, jettyResponse, responseStart);
}
}
requestLogger.log(requestInfo, ResponseInfo.from(Optional.of(response), jettyResponse.getBytesRead()));
return value;
}
use of io.airlift.http.client.jetty.HttpClientLogger.RequestInfo in project airlift by airlift.
the class JettyHttpClient method executeAsync.
@Override
public <T, E extends Exception> HttpResponseFuture<T> executeAsync(Request request, ResponseHandler<T, E> responseHandler) {
requireNonNull(request, "request is null");
requireNonNull(responseHandler, "responseHandler is null");
request = applyRequestFilters(request);
HttpRequest jettyRequest = buildJettyRequest(request);
RequestInfo requestInfo = RequestInfo.from(jettyRequest, System.currentTimeMillis());
JettyResponseFuture<T, E> future = new JettyResponseFuture<>(request, jettyRequest, responseHandler, stats, recordRequestComplete);
BufferingResponseListener listener = new BufferingResponseListener(future, Ints.saturatedCast(maxContentLength), responseInfo -> requestLogger.log(requestInfo, responseInfo));
try {
jettyRequest.send(listener);
} catch (RuntimeException e) {
if (!(e instanceof RejectedExecutionException)) {
e = new RejectedExecutionException(e);
}
// normally this is a rejected execution exception because the client has been closed
future.failed(e);
requestLogger.log(requestInfo, ResponseInfo.failed(Optional.empty(), Optional.of(e)));
}
return future;
}
Aggregations