Skip to content

Commit

Permalink
chore: add pprof and debug docs (#72)
Browse files Browse the repository at this point in the history
* chore: add pprof and debug docs

Signed-off-by: Justin Alvarez <[email protected]>

* update docs

Signed-off-by: Justin Alvarez <[email protected]>

---------

Signed-off-by: Justin Alvarez <[email protected]>
  • Loading branch information
pendo324 authored Oct 30, 2024
1 parent ad26173 commit 9aafe95
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
30 changes: 27 additions & 3 deletions cmd/finch-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import (
"syscall"
"time"

// #nosec
// register HTTP handler for /debug/pprof on the DefaultServeMux.
_ "net/http/pprof"

"github.com/containerd/containerd"
"github.com/containerd/nerdctl/pkg/api/types"
"github.com/containerd/nerdctl/pkg/config"
Expand Down Expand Up @@ -47,9 +51,10 @@ const (
)

type DaemonOptions struct {
debug bool
socketAddr string
socketOwner int
debug bool
socketAddr string
socketOwner int
debugAddress string
}

var options = new(DaemonOptions)
Expand All @@ -65,6 +70,7 @@ func main() {
rootCmd.Flags().StringVar(&options.socketAddr, "socket-addr", defaultFinchAddr, "server listening Unix socket address")
rootCmd.Flags().BoolVar(&options.debug, "debug", false, "turn on debug log level")
rootCmd.Flags().IntVar(&options.socketOwner, "socket-owner", -1, "Uid and Gid of the server socket")
rootCmd.Flags().StringVar(&options.debugAddress, "debug-addr", "", "")
if err := rootCmd.Execute(); err != nil {
log.Printf("got error: %v", err)
log.Fatal(err)
Expand Down Expand Up @@ -99,6 +105,24 @@ func run(options *DaemonOptions) error {
if err := os.Chown(options.socketAddr, options.socketOwner, options.socketOwner); err != nil {
return fmt.Errorf("failed to chown the finch-daemon socket: %w", err)
}

if options.debugAddress != "" {
logger.Infof("Serving debugging endpoint on %q", options.debugAddress)
go func() {
debugListener, err := net.Listen("tcp", options.debugAddress)
if err != nil {
logger.Fatal(err)
}
debugServer := &http.Server{
Handler: http.DefaultServeMux,
ReadHeaderTimeout: 5 * time.Second,
}
if err := debugServer.Serve(debugListener); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Fatal(err)
}
}()
}

server := &http.Server{
Handler: r,
ReadHeaderTimeout: 5 * time.Minute,
Expand Down
42 changes: 42 additions & 0 deletions docs/debug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Debugging the finch daemon

This document outlines where to find/access logs and how to configure profiling tools for finch daemon.

## Logs

Logs are the first place to check when you suspect a problem with finch-daemon. If `finch-daemon` was started via `systemd` then you can obtain logs using `journalctl`:

```shell
sudo journalctl -u finch
```

> **Note**
> The command above assumes that you have used the unit file definition [finch.service](../finch.service) we have provided. If you have created your own unit file for `finch-daemon` and replace `finch-daemon` with the one you have made. Amazon Linux distributions of Finch also use the name `finch` for the finch-daemon service.
If you have started `finch-daemon` manually, logs will either be emitted to stderr/stdout.

## CPU Profiling

We can use Golangs `pprof` tool to profile the daemon. To enable profiling you must set the `--debug-addr` CLI parameter when invoking `finch-daemon`:

```shell
./finch-daemon --debug-addr localhost:6060
```

> **Note**
> Similarly to adding the command line option for a local run of finch-daemon, any systemd service file can also be modified to include the `--debug-addr` option.

Once you have configured the debug address you can send a `GET` to the `/debug/pprof/profile` endpoint to receive a CPU profile of the daemon. You can specify an optional argument `seconds` to limit the results to a certain time span:

```shell
curl http://localhost:6060/debug/pprof/profile?seconds=40 > out.pprof
```

You can use the `pprof` tool provided by the Go CLI to visualize the data within a web browser:

```shell
go tool pprof -http=:8080 out.pprof
```

For more information on pprof, [see its documentation here](https://pkg.go.dev/net/http/pprof).

0 comments on commit 9aafe95

Please sign in to comment.