title: Pass information from the RPC function to gRPC server interceptor in Go
body: <p>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?</p>
  <p>The RPC server:</p>
  <pre class="lang-golang prettyprint-override"><code>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 &amp;MyResponse{}, nil
  }
  </code></pre>
  <p>And the unary server interceptor:</p>
  <pre class="lang-golang prettyprint-override"><code>func (mi *metricsInterceptor) UnaryServerInterceptor() grpc.UnaryServerInterceptor {
      return mi.doServerUnary
  }
  </code></pre>
  <p>and</p>
  <pre><code>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)
  }
  </code></pre>