Package brave.http

Class HttpClientHandler<Req,​Resp>

  • Type Parameters:
    Req - the native http request type of the client.
    Resp - the native http response type of the client.

    public final class HttpClientHandler<Req,​Resp>
    extends Object
    This standardizes a way to instrument http clients, particularly in a way that encourages use of portable customizations via HttpRequestParser and HttpResponseParser.

    Synchronous interception is the most straight forward instrumentation.

    You generally need to:

    1. Start the span and add trace headers to the request
    2. Put the span in scope so things like log integration works
    3. Invoke the request
    4. If there was a Throwable, add it to the span
    5. Complete the span
    
     HttpClientRequestWrapper requestWrapper = new HttpClientRequestWrapper(request);
     Span span = handler.handleSend(requestWrapper); // 1.
     HttpClientResponse response = null;
     Throwable error = null;
     try (Scope ws = currentTraceContext.newScope(span.context())) { // 2.
       return response = invoke(request); // 3.
     } catch (Throwable e) {
       error = e; // 4.
       throw e;
     } finally {
       HttpClientResponseWrapper responseWrapper =
         new HttpClientResponseWrapper(requestWrapper, response, error);
       handler.handleReceive(responseWrapper, span); // 5.
     }
     
    Since:
    4.3