The counter can be made through the interceptor, in the context to attach a value that will increase with each call and a new context to put into the handler, and already in the implemented method to retrieve this value.
Example:
// CounterUnaryServerInterseptor interseptor, который будет производить подсчет вызовов методов сервера func CounterUnaryServerInterseptor() grpc.UnaryServerInterceptor { methods := make(map[string]int64, 0) return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { var count int64 var ok bool if count, ok = methods[info.FullMethod]; ok { count += 1 } methods[info.FullMethod] = count newCtx := context.WithValue(ctx, "count", count) return handler(newCtx, req) } } // MethodGRPCServer реализация метода в котором извлекается значение `count` func (s *Server) MethodGRPCServer(ctx context.Context, req *pb.Request) (*pb.Response, error) { countInterface := ctx.Value("count") count, ok := countInterface.(int64) if !ok { log.Printf("Can't cast to int64, data: %+v", countInterface) } log.Print(count) // Ваш код return response, nil } func main() { // Инициализация gRPC сервера grpcServer := grpc.NewServer( CounterUnaryServerInterseptor(), ) // Регистрация методов // ... }