Package brave.http
Class HttpClientHandler<Req,Resp>
- java.lang.Object
-
- brave.http.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 viaHttpRequestParser
andHttpResponseParser
.Synchronous interception is the most straight forward instrumentation.
You generally need to:
- Start the span and add trace headers to the request
- Put the span in scope so things like log integration works
- Invoke the request
- If there was a Throwable, add it to the span
- 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
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static HttpClientHandler<HttpClientRequest,HttpClientResponse>
create(HttpTracing httpTracing)
static <Req,Resp>
HttpClientHandler<Req,Resp>create(HttpTracing httpTracing, HttpClientAdapter<Req,Resp> adapter)
Deprecated.Since 5.7, usecreate(HttpTracing)
as it is more portable.void
handleReceive(HttpClientResponse response, brave.Span span)
Finishes the client span after assigning it tags according to the response or error.void
handleReceive(Resp response, Throwable error, brave.Span span)
Deprecated.since 5.12 usehandleReceive(HttpClientResponse, Span)
brave.Span
handleSend(HttpClientRequest request)
Starts the client span after assigning it a name and tags.brave.Span
handleSend(HttpClientRequest request, brave.Span span)
LikehandleSend(HttpClientRequest)
, except explicitly controls the span representing the request.<C> brave.Span
handleSend(brave.propagation.TraceContext.Injector<C> injector, C carrier, Req request)
Deprecated.Since 5.7, usehandleSend(HttpClientRequest)
.<C> brave.Span
handleSend(brave.propagation.TraceContext.Injector<C> injector, C carrier, Req request, brave.Span span)
Deprecated.Since 5.7, usehandleSend(HttpClientRequest)
.brave.Span
handleSend(brave.propagation.TraceContext.Injector<Req> injector, Req request)
Deprecated.Since 5.7, usehandleSend(HttpClientRequest)
, as this allows more advanced samplers to be used.brave.Span
handleSend(brave.propagation.TraceContext.Injector<Req> injector, Req request, brave.Span span)
Deprecated.Since 5.7, usehandleSend(HttpClientRequest, Span)
.brave.Span
handleSendWithParent(HttpClientRequest request, brave.propagation.TraceContext parent)
LikehandleSend(HttpClientRequest)
, except explicitly controls the parent of the client span.brave.Span
nextSpan(Req request)
Deprecated.since 5.8 useTracer.nextSpan(SamplerFunction, Object)
-
-
-
Method Detail
-
create
public static HttpClientHandler<HttpClientRequest,HttpClientResponse> create(HttpTracing httpTracing)
- Since:
- 5.7
-
create
@Deprecated public static <Req,Resp> HttpClientHandler<Req,Resp> create(HttpTracing httpTracing, HttpClientAdapter<Req,Resp> adapter)
Deprecated.Since 5.7, usecreate(HttpTracing)
as it is more portable.- Since:
- 4.3
-
handleSend
public brave.Span handleSend(HttpClientRequest request)
Starts the client span after assigning it a name and tags. Thisinjects
the trace context onto the request before returning.Call this before sending the request on the wire.
-
handleSendWithParent
public brave.Span handleSendWithParent(HttpClientRequest request, @Nullable brave.propagation.TraceContext parent)
LikehandleSend(HttpClientRequest)
, except explicitly controls the parent of the client span.- Parameters:
parent
- the parent of the client span representing this request, or null for a new trace.- Since:
- 5.10
- See Also:
Tracer.nextSpanWithParent(SamplerFunction, Object, TraceContext)
-
handleSend
public brave.Span handleSend(HttpClientRequest request, brave.Span span)
LikehandleSend(HttpClientRequest)
, except explicitly controls the span representing the request.- Since:
- 5.7
-
handleSend
@Deprecated public brave.Span handleSend(brave.propagation.TraceContext.Injector<Req> injector, Req request)
Deprecated.Since 5.7, usehandleSend(HttpClientRequest)
, as this allows more advanced samplers to be used.- Since:
- 4.3
-
handleSend
@Deprecated public <C> brave.Span handleSend(brave.propagation.TraceContext.Injector<C> injector, C carrier, Req request)
Deprecated.Since 5.7, usehandleSend(HttpClientRequest)
.- Since:
- 4.3
-
handleSend
@Deprecated public brave.Span handleSend(brave.propagation.TraceContext.Injector<Req> injector, Req request, brave.Span span)
Deprecated.Since 5.7, usehandleSend(HttpClientRequest, Span)
.- Since:
- 4.4
-
handleSend
@Deprecated public <C> brave.Span handleSend(brave.propagation.TraceContext.Injector<C> injector, C carrier, Req request, brave.Span span)
Deprecated.Since 5.7, usehandleSend(HttpClientRequest)
.- Since:
- 4.4
-
nextSpan
@Deprecated public brave.Span nextSpan(Req request)
Deprecated.since 5.8 useTracer.nextSpan(SamplerFunction, Object)
- Since:
- 4.4
-
handleReceive
@Deprecated public void handleReceive(@Nullable Resp response, @Nullable Throwable error, brave.Span span)
Deprecated.since 5.12 usehandleReceive(HttpClientResponse, Span)
- Since:
- 4.3
-
handleReceive
public void handleReceive(HttpClientResponse response, 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
.Note: It is valid to have a
HttpClientResponse
that only includes an error. However, it is better to also include the request.- Since:
- 5.12
- See Also:
HttpResponseParser.parse(HttpResponse, TraceContext, SpanCustomizer)
-
-