Package brave.rpc

Class 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:

    1. Extract any trace IDs from headers and start the span
    2. Put the span in scope so things like log integration works
    3. Process the request
    4. If there was a Throwable, add it to the span
    5. Complete the span
    
     RpcServerRequestWrapper requestWrapper = new RpcServerRequestWrapper(request);
     Span span = handler.handleReceive(requestWrapper); // 1.
     ServerResponse response = null;
     Throwable error = null;
     try (Scope scope = 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