Search in sources :

Example 1 with RequestInfo

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");
}
Also used : ResponseInfo(io.airlift.http.client.jetty.HttpClientLogger.ResponseInfo) HttpFields(org.eclipse.jetty.http.HttpFields) DataSize(io.airlift.units.DataSize) Request(org.eclipse.jetty.client.api.Request) RequestInfo(io.airlift.http.client.jetty.HttpClientLogger.RequestInfo) URI(java.net.URI) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 2 with RequestInfo

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");
}
Also used : ResponseInfo(io.airlift.http.client.jetty.HttpClientLogger.ResponseInfo) Request(org.eclipse.jetty.client.api.Request) RequestInfo(io.airlift.http.client.jetty.HttpClientLogger.RequestInfo) URI(java.net.URI) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpFields(org.eclipse.jetty.http.HttpFields) DataSize(io.airlift.units.DataSize) Test(org.testng.annotations.Test)

Example 3 with RequestInfo

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;
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) RequestInfo(io.airlift.http.client.jetty.HttpClientLogger.RequestInfo) ByteBuffer(java.nio.ByteBuffer) TimeoutException(java.util.concurrent.TimeoutException) UncheckedIOException(java.io.UncheckedIOException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Response(org.eclipse.jetty.client.api.Response) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with RequestInfo

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;
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) RequestInfo(io.airlift.http.client.jetty.HttpClientLogger.RequestInfo) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Aggregations

RequestInfo (io.airlift.http.client.jetty.HttpClientLogger.RequestInfo)4 ResponseInfo (io.airlift.http.client.jetty.HttpClientLogger.ResponseInfo)2 DataSize (io.airlift.units.DataSize)2 URI (java.net.URI)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 HttpRequest (org.eclipse.jetty.client.HttpRequest)2 Request (org.eclipse.jetty.client.api.Request)2 Response (org.eclipse.jetty.client.api.Response)2 HttpFields (org.eclipse.jetty.http.HttpFields)2 Test (org.testng.annotations.Test)2 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 ByteBuffer (java.nio.ByteBuffer)1 ExecutionException (java.util.concurrent.ExecutionException)1 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)1 InputStreamResponseListener (org.eclipse.jetty.client.util.InputStreamResponseListener)1