Skip to content

Commit

Permalink
Merge pull request #24 from blackskad/go-build-version
Browse files Browse the repository at this point in the history
Add a build information page to the debug handler
  • Loading branch information
blackskad authored Sep 11, 2024
2 parents 923b087 + 46aae65 commit 6f37a15
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
39 changes: 39 additions & 0 deletions pkg/o11y/debug-info.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<html>
<head>
<title>Application build information</title>
<style>
dt {
font-weight: bold;
}
</style>
</head>
<body>
<h1>Application build information</h1>
<h2>Base info</h2>
<dl>
<dt>Go version</dt>
<dd>{{ .GoVersion }}</dd>
</dl>
<h2>Module</h2>
<dl>
<dt>Path</dt>
<dd>{{ .Main.Path }}</dd>
<dt>Version</dt>
<dd>{{ .Main.Version }}</dd>
<dt>Sum</dt>
<dd>{{ .Main.Sum }}</dd>
</dl>
<h2>Settings</h2>
<dl>
{{ range .Settings }}
<dt>{{ .Key }}</dt>
<dd>{{ .Value }}</dd>
{{ end }}
</dl>
<h2>Modules</h2>
<ul>
{{ range .Deps }}
<li>{{ .Path }} {{ .Version }}</li>
{{ end }}
</body>
</html>
22 changes: 22 additions & 0 deletions pkg/o11y/pprof.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
package o11y

import (
_ "embed"
"html/template"
"log/slog"
"net/http"
_ "net/http/pprof"
"runtime/debug"
)

func StartPProfServer() {
go runPProfServer()
}

//go:embed debug-info.html
var debugInfoPage string

var debugInfoTPL = template.Must(template.New("debug-info").Parse(debugInfoPage))

// runPProfServer starts an HTTP server on port 6060 that exposes /debug/pprof
// This function blocks until an error occurs
// This is a separate function to make stacktraces more readable
func runPProfServer() {
http.HandleFunc("/debug/info", func(w http.ResponseWriter, r *http.Request) {
debugInfo, ok := debug.ReadBuildInfo()
if !ok {
http.Error(w, "unable to load build info", http.StatusInternalServerError)
return
}

err := debugInfoTPL.ExecuteTemplate(w, "debug-info", debugInfo)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})

err := http.ListenAndServe(":6060", nil)
if err != nil {
slog.Warn("pprof http server shut down", slog.Any("error", err))
Expand Down

0 comments on commit 6f37a15

Please sign in to comment.