Pass information from the RPC function to gRPC server interceptor in Go

Viewed 107

I'm trying to pass some information about how an RPC call is performed from the RPC function itself to the unary server interceptor. How can I achieve this?

The RPC server:

func (s *myServer) Get(ctx context.Context, in *MyRequest) (*MyResponse, error) {
   // Do some work, 
   // But also generate some metrics that should be made available to the server interceptor after the work here is done,
   // probably through context
   return &MyResponse{}, nil
}

And the unary server interceptor:

func (mi *metricsInterceptor) UnaryServerInterceptor() grpc.UnaryServerInterceptor {
    return mi.doServerUnary
}

and

func (mi *metricsInterceptor) doServerUnary(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (out interface{}, err error) {
   defer func() {
      // get those metrics generated in the call, probably from the context, and do stuff with them
   }
   out, err = handler(ctx, req)
}
0 Answers
Related