Skip to content

Commit 2bee633

Browse files
committed
documentation about local debug support in Fn
1 parent d18cc2e commit 2bee633

13 files changed

Lines changed: 123 additions & 0 deletions

LocalDebug/README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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. ![User Input
13+
Icon](images/userinput.png) 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+
![User Input Icon](images/userinput.png)
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+
![User Input Icon](images/userinput.png)
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+
![User Input Icon](images/userinput.png)
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+
![Java Debug Configuration](images/javaDebugConfiguration.png)
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+
![Java Debug Button](images/javaDebugButton.png)
52+
53+
And you will see your breakpoint got triggered.
54+
![Java breakpoint](images/javaBreakpoint.png)
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+
![Go Debug Configuration](images/goDebugConfiguration.png)
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+
![Go Debug Button](images/goDebugButton.png)
68+
69+
And you will see your breakpoint got triggered.
70+
![Go breakpoint](images/goBreakpoint.png)
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+
![Python Debug Configuration](images/pythonDebugConfiguration.png)
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+
![Python Debug Button](images/pythonDebugButton.png)
89+
90+
And you will see your breakpoint got triggered.
91+
![Python breakpoint](images/pythonBreakpoint.png)
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+

LocalDebug/image.png

95.5 KB
Loading

LocalDebug/images/goBreakpoint.png

231 KB
Loading
5.77 KB
Loading
161 KB
Loading
134 KB
Loading
5.6 KB
Loading
87.8 KB
Loading
168 KB
Loading
4.58 KB
Loading

0 commit comments

Comments
 (0)