Package brave.rpc
Class RpcServerHandler
java.lang.Object
brave.rpc.RpcServerHandler
public final class RpcServerHandler extends Object
This standardizes a way to instrument RPC servers, particularly in a way that encourages use of
portable customizations via
RpcRequestParser and RpcResponseParser.
Synchronous interception is the most straight forward instrumentation.
You generally need to:
- Extract any trace IDs from headers and start the span
- Put the span in scope so things like log integration works
- Process the request
- If there was a Throwable, add it to the span
- Complete the span
RpcServerRequestWrapper requestWrapper = new RpcServerRequestWrapper(request);
Span span = handler.handleReceive(requestWrapper); // 1.
ServerResponse response = null;
Throwable error = null;
try (Scope ws = currentTraceContext.newScope(span.context())) { // 2.
return response = process(request); // 3.
} catch (Throwable e) {
error = e; // 4.
throw e;
} finally {
RpcServerResponseWrapper responseWrapper =
new RpcServerResponseWrapper(requestWrapper, response, error);
handler.handleSend(responseWrapper, span); // 5.
}
- Since:
- 5.12
-
Method Summary
Modifier and Type Method Description static RpcServerHandlercreate(RpcTracing rpcTracing)SpanhandleReceive(RpcServerRequest request)Conditionally joins a span, or starts a new trace, depending on if a trace context was extracted from the request.voidhandleSend(RpcServerResponse response, Span span)Finishes the server span after assigning it tags according to the response or error.
-
Method Details
-
create
- Since:
- 5.12
-
handleReceive
Conditionally joins a span, or starts a new trace, depending on if a trace context was extracted from the request. Tags are added before the span is started.This is typically called before the request is processed by the actual library.
- Since:
- 5.12
- See Also:
RpcTracing.serverSampler(),RpcTracing.serverRequestParser()
-
handleSend
Finishes the server span after assigning it tags according to the response or error.This is typically called once the response headers are sent, and after the span is
no longer in scope.Note: It is valid to have a
RpcServerResponsethat only includes an error. However, it is better to also include the request.- Since:
- 5.12
- See Also:
RpcResponseParser.parse(RpcResponse, TraceContext, SpanCustomizer)
-