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 java.lang.Object
    This standardizes a way to instrument http clients, particularly in a way that encourages use of portable customizations via HttpClientParser.

    This is an example of synchronous instrumentation:

    
     Span span = handler.handleSend(injector, request);
     Throwable error = null;
     try (Tracer.SpanInScope ws = tracer.withSpanInScope(span)) {
       // any downstream code can see Tracer.currentSpan() or use Tracer.currentSpanCustomizer()
       response = invoke(request);
     } catch (RuntimeException | Error e) {
       error = e;
       throw e;
     } finally {
       handler.handleReceive(response, error, span);
     }
     
    Since:
    4.3
    • Method Detail

      • handleSend

        public brave.Span handleSend​(brave.propagation.TraceContext.Injector<Req> injector,
                                     Req request)
        Starts the client span after assigning it a name and tags. This injects the trace context onto the request before returning.

        Call this before sending the request on the wire.

      • handleSend

        public brave.Span handleSend​(brave.propagation.TraceContext.Injector<Req> injector,
                                     Req request,
                                     brave.Span span)
        Like handleSend(TraceContext.Injector, Object), except explicitly controls the span representing the request.
        Since:
        4.4
      • handleSend

        public <C> brave.Span handleSend​(brave.propagation.TraceContext.Injector<C> injector,
                                         C carrier,
                                         Req request,
                                         brave.Span span)
        Like handleSend(TraceContext.Injector, Object, Object), except explicitly controls the span representing the request.
        Since:
        4.4
      • nextSpan

        public brave.Span nextSpan​(Req request)
        Creates a potentially noop span representing this request. This is used when you need to provision a span in a different scope than where the request is executed.
        Since:
        4.4
      • handleReceive

        public void handleReceive​(@Nullable
                                  Resp response,
                                  @Nullable
                                  java.lang.Throwable error,
                                  brave.Span span)
        Finishes the client span after assigning it tags according to the response or error.

        This is typically called once the response headers are received, and after the span is no longer in scope.

        See Also:
        HttpClientParser.response(HttpAdapter, Object, Throwable, SpanCustomizer)