Package brave.rpc
Class RpcClientHandler
- java.lang.Object
-
- brave.rpc.RpcClientHandler
-
public final class RpcClientHandler extends Object
This standardizes a way to instrument RPC clients, particularly in a way that encourages use of portable customizations viaRpcRequestParser
andRpcResponseParser
.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
RpcClientRequestWrapper requestWrapper = new RpcClientRequestWrapper(request); Span span = handler.handleSend(requestWrapper); // 1. ClientResponse response = null; Throwable error = null; try (Scope scope = currentTraceContext.newScope(span.context())) { // 2. return response = invoke(request); // 3. } catch (Throwable e) { error = e; // 4. throw e; } finally { RpcClientResponseWrapper responseWrapper = new RpcClientResponseWrapper(requestWrapper, response, error); handler.handleReceive(responseWrapper, span); // 5. }
- Since:
- 5.12
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static RpcClientHandler
create(RpcTracing rpcTracing)
void
handleReceive(RpcClientResponse response, brave.Span span)
Finishes the client span after assigning it tags according to the response or error.brave.Span
handleSend(RpcClientRequest request)
Starts the client span after assigning it a name and tags.brave.Span
handleSend(RpcClientRequest request, brave.Span span)
LikehandleSend(RpcClientRequest)
, except explicitly controls the span representing the request.brave.Span
handleSendWithParent(RpcClientRequest request, brave.propagation.TraceContext parent)
LikehandleSend(RpcClientRequest)
, except explicitly controls the parent of the client span.
-
-
-
Method Detail
-
create
public static RpcClientHandler create(RpcTracing rpcTracing)
- Since:
- 5.12
-
handleSend
public brave.Span handleSend(RpcClientRequest 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(RpcClientRequest request, @Nullable brave.propagation.TraceContext parent)
LikehandleSend(RpcClientRequest)
, 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.12
- See Also:
Tracer.nextSpanWithParent(SamplerFunction, Object, TraceContext)
-
handleSend
public brave.Span handleSend(RpcClientRequest request, brave.Span span)
LikehandleSend(RpcClientRequest)
, except explicitly controls the span representing the request.- Since:
- 5.12
-
handleReceive
public void handleReceive(RpcClientResponse 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
RpcClientResponse
that only includes an error. However, it is better to also include the request.- Since:
- 5.12
- See Also:
RpcResponseParser.parse(RpcResponse, TraceContext, SpanCustomizer)
-
-