Skip to content

2.3. Debugging

Rafał Lorenz edited this page Jun 10, 2020 · 2 revisions

Using debugger

To debug deployment you can simply use telepresence and swap kubernetes deployment for local go service or local docker image. For example to swap for local docker image run:

make telepresence-swap-docker BIN=user PORT=3001 DEPLOYMENT=go-api-boilerplate-user

This command should swap deployment giving similar output to the one below:

➜  go-api-boilerplate git:(master) ✗ make telepresence-swap-docker BIN=user PORT=3001 DEPLOYMENT=go-api-boilerplate-user
telepresence \
	--swap-deployment go-api-boilerplate-user \
	--docker-run -i -t --rm -p=3001:3001 --name="user" user:latest
T: Volumes are rooted at $TELEPRESENCE_ROOT. See https://telepresence.io/howto/volumes.html for
T: details.
.
.
.
2019/01/10 06:24:37.963561 INFO: tcp running at 0.0.0.0:3001
2019/01/10 06:24:37.964452 INFO: http running at 0.0.0.0:3000
^C
2019/01/10 06:30:16.266108 INFO: shutting down...
2019/01/10 06:30:16.283392 INFO: gracefully stopped
T: Exit cleanup in progress
# --docker-run --rm -it -v -p=3001:3001 user:latest

Profiling

go-api-boilerplate allows you to profile your services while running in development mode.

	if config.Env.App.Environment == "development" {
		app.AddAdapters(
			application.NewDebugAdapter(
				fmt.Sprintf("%s:%d", config.Env.Debug.Host, config.Env.Debug.Port),
			),
		)
	}

Boilerplate contains built in debug server running on port by default 4000. Debug adapter can be configured

It allows you to take advantage of:

Package pprof serves via its HTTP server runtime profiling data in the format expected by the pprof visualization tool. The package is typically only imported for the side effect of registering its HTTP handlers. The handled paths all begin with /debug/pprof/.

Package expvar provides a standardized interface to public variables, such as operation counters in servers. It exposes these variables via HTTP at /debug/vars in JSON format. The package is sometimes only imported for the side effect of registering its HTTP handler and the above variables.

Debug server exposes two routes:

  • /debug/pprof added to the default mux by importing the net/http/pprof package.
  • /debug/vars added to the default mux by importing the expvar package.

That are available under your service namespace, for example /users

Counters

To see exported counters of users open http://localhost:4000/users/debug/vars in your browser.

{
  "cmdline": [
    "/var/folders/_4/81_0n4l57_scy27jfcxx1j300000gn/T/go-build888334884/b001/exe/main"
  ],
  "goroutines": 0,
  "requests": 3,
  "memstats": {...}
}

pprof

Use the pprof tool to look at the heap profile:

To view all available profiles, open http://localhost:4000/users/debug/pprof/ in your browser.

We can see visualization using go tool as follow:

go tool pprof http://localhost:4000/users/debug/pprof/heap

To see graph in browser lets type web

(pprof) web
failed to execute dot. Is Graphviz installed? Error: exec: "dot": executable file not found in $PATH

If you see error you can install it with:

brew install graphviz

Repeat the sequence:

go tool pprof http://localhost:4000/users/debug/pprof/heap
(pprof) web

This will open browser with nice graph.

You can find more information about how Debug Adapter works on this blog post: Profiling Go HTTP service with pprof and expvar

Clone this wiki locally