debugger

Debugging helper for Go

MIT License

Stars
64

Debugger Middleware

This package provides a debugging middleware for Go applications to enable better display of goroutines in the debugger.

It has nearly-zero performance penalty in production code when not actively used.

How this looks like in the IDE

Below you can see how this feature looks like in GoLand IDE:

How to use

Include it in your application using one of the patterns below.

Then, compile the application with -tags debugger, e.g.

go build -tags debugger

More details on how to use this can be found in this blog post: https://blog.jetbrains.com/go/2020/03/03/how-to-find-goroutines-during-debugging/

HTTP handlers

In your code, replace the HTTP handler with the Middleware function call.

Original:

router.HandleFunc("/", homeHandler)

Replacement:

router.HandleFunc("/", debugger.Middleware(homeHandler, func(r *http.Request) []string {
    return []string{
        "path", r.RequestURI,
    }
}))

Non-HTTP handlers

For normal functions/methods, you can use the SetLabels / SetLabelsCtx functions to set the debugger labels.

Original:

func sum(a, b int) int {
    return a+b
}

Replacement:

func sum(a, b int) int {
    debugger.SetLabels(func() []string {
        return []string{
            "a", strconv.Itoa(a),
            "b", strconv.Itoa(b),
        }
    })

    return a+b
}

Performance

You can asses the performance of this library by running the included benchmarks in your environment.

Here are the results from my own machine (Intel Core i7 6700HQ, 32GB RAM, Windows 10), when running with a -count=5 pass, averaged across runs.

Go 1.13.8

Without labels:

License

This project is provided under the MIT license.

Package Rankings
Top 4.82% on Proxy.golang.org
Related Projects