|
| 1 | +# Debug function locally |
| 2 | + |
| 3 | +Fn allows you to deploy your function locally and attach your favorite debugger to debug your function. |
| 4 | +In this tutorial, we will walk throught the setup. Currently, we support the local debug feature for |
| 5 | +Go, Python and Java function. Local debugging is also possible for users that are using custom docker file. |
| 6 | + |
| 7 | +## Before you Begin |
| 8 | +* Set aside about 15 minutes to complete this tutorial. |
| 9 | +* Make sure Fn server is up and running by completing the [Install and Start Fn Tutorial](../install/README.md). |
| 10 | + * Make sure you have set your Fn context registry value for local development. (for example, "fndemouser". [See here](https://github.com/fnproject/tutorials/blob/master/install/README.md#configure-your-context).) |
| 11 | + |
| 12 | +As you make your way through this tutorial, look out for this icon.  Whenever you see it, it's time for you to perform an |
| 14 | +action. |
| 15 | + |
| 16 | +## Start Fn in Debug mode |
| 17 | +In the terminal, type the following to start Fn in debug mode. |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +>```sh |
| 22 | +> fn start --local-debug --local-debug-port <port number> |
| 23 | +>``` |
| 24 | +
|
| 25 | +"local-debug-port" is an optional parameter. If you do not specify, it will use 5678 in your host machine. |
| 26 | +It is the port for your remote debugger to attach to. |
| 27 | +
|
| 28 | +## Local Debug for Java Function |
| 29 | +
|
| 30 | +Suppose you have already created your function "myfunc", you could deploy your function in debug mode by running the following: |
| 31 | +
|
| 32 | + |
| 33 | +>```sh |
| 34 | +> fn deploy --app myapp --local-debug --local-debug-port <e.g. 5678. Optional. 5678 by default> |
| 35 | +>``` |
| 36 | +
|
| 37 | +Once you have done that, the docker image will be built with debug capability. Debug port will be exposed and mapped to local host. |
| 38 | +If you trigger your function now, you will see the function paused and waiting for remote debugger to attach. |
| 39 | +
|
| 40 | + |
| 41 | +>```sh |
| 42 | +> fn invoke myapp myfunc |
| 43 | +>``` |
| 44 | +
|
| 45 | +Here we use Intellij as an example. Please open your function directory in Intellij and setup Debug configuration as shown below. |
| 46 | + |
| 47 | +
|
| 48 | +Please use the port 5678 or the custom port if you have specified in previous step. |
| 49 | +
|
| 50 | +Add a breakpoint to your code. Then you can click the debug button as shown below. |
| 51 | + |
| 52 | +
|
| 53 | +And you will see your breakpoint got triggered. |
| 54 | + |
| 55 | +
|
| 56 | +## Local Debug for Go Function |
| 57 | +
|
| 58 | +Debugging Go function is similar to Java. You could open your Go function in Intellij/Goland as a project and add a debug configuration. |
| 59 | +Here is the debug configuration you could setup in Intellij/Goland. |
| 60 | + |
| 61 | +
|
| 62 | +Then you could deploy the function and then invoke it. The steps are same as those in Java section. |
| 63 | +
|
| 64 | +Then you will see the function paused and waiting for a debugger to attach. |
| 65 | +
|
| 66 | +Add a breakpoint to your code. Now you could start the debug session by pressing the debug button in Intellij/Goland. |
| 67 | + |
| 68 | +
|
| 69 | +And you will see your breakpoint got triggered. |
| 70 | + |
| 71 | +
|
| 72 | +
|
| 73 | +## Local Debug for Python Function |
| 74 | +
|
| 75 | +Debugging Python is similar to Go and Java. In this example, we will use VSCode instead. |
| 76 | +
|
| 77 | +
|
| 78 | +Then you could deploy the function and then invoke it. The steps are same as those in Java section. |
| 79 | +
|
| 80 | +Then you will see the function paused and waiting for a debugger to attach. |
| 81 | +
|
| 82 | +Open your python function in VSCode. You could add a debug configuration as shown below: |
| 83 | + |
| 84 | +
|
| 85 | +Make sure the `localRoot` path is pointing to your python function directory. |
| 86 | +
|
| 87 | +Add a breakpoint to your code. Now you could start the debug session by pressing the debug button in VSCode. |
| 88 | + |
| 89 | +
|
| 90 | +And you will see your breakpoint got triggered. |
| 91 | + |
| 92 | +
|
| 93 | +
|
| 94 | +## Local Debug for custom docker file |
| 95 | +
|
| 96 | +For user that are using their custom docker file, they have to do the following to allow remote debugger to attach to |
| 97 | +the function container when it is started up by Fn. |
| 98 | +
|
| 99 | +1. Install language specific debugger in your function image. For go, it could be Delve. For Python, it could be debugpy. |
| 100 | +2. Start up the debugger in port 5678. It has to be 5678. Fn will map this port to `local-debug-port` in local host. |
| 101 | +The `local-debug-port` is specified when you run the `fn start --local-debug`. By default, it is also 5678. |
| 102 | +3. Add any language specific setting in your custom docker file. For example, for Go, you are required to pass specific |
| 103 | +build flags to enabling debugging. |
| 104 | +
|
| 105 | +Here is an example for Go function: |
| 106 | +``` |
| 107 | +FROM fnproject/go:1.24-dev as build-stage |
| 108 | +WORKDIR /function |
| 109 | +WORKDIR /go/src/func/ |
| 110 | +ENV GO111MODULE=on |
| 111 | +COPY . . |
| 112 | +RUN go mod tidy |
| 113 | +RUN go build -gcflags="all=-N -l" -o func -v |
| 114 | +RUN go install github.com/go-delve/delve/cmd/dlv@latest |
| 115 | +
|
| 116 | +FROM fnproject/go:1.24 |
| 117 | +WORKDIR /function |
| 118 | +COPY --from=build-stage /go/src/func/func /function/ |
| 119 | +COPY --from=build-stage /go/bin/dlv /function |
| 120 | +CMD ["/function/dlv", "--listen=:5678", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "./func"] |
| 121 | +``` |
| 122 | +
|
0 commit comments