Skip to content

Commit

Permalink
Refactor debug endpoint to use pprof package
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Oct 15, 2024
1 parent e4701f4 commit 4c80aa6
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"database/sql"
"log"
"net/http/pprof"
"os"
"path/filepath"
"strconv"
Expand All @@ -12,6 +13,7 @@ import (
_ "github.com/marcboeker/go-duckdb"
)

var debug bool = false
var refreshRateSeconds int = 5

var pushEnabled bool = true
Expand Down Expand Up @@ -49,6 +51,20 @@ func main() {
if gin.Mode() == gin.DebugMode {
metricsFile = "./db/metrics.duckdb"
}
debugFromEnv := os.Getenv("DEBUG")
if debugFromEnv != "" {
var err error
debug, err = strconv.ParseBool(debugFromEnv)
if err != nil {
log.Printf("Error parsing DEBUG: %v", err)
}
}
if debug {
log.Printf("Debug is enabled")
} else {
log.Printf("Debug is disabled")
}

tokenFromEnv := os.Getenv("TOKEN")
if tokenFromEnv == "" {
log.Fatal("TOKEN environment variable is required")
Expand Down Expand Up @@ -168,6 +184,32 @@ func main() {
} else {
setupPush()
}
if debug {
r.GET("/debug/pprof", func(c *gin.Context) {
pprof.Index(c.Writer, c.Request)
})
r.GET("/debug/cmdline", func(c *gin.Context) {
pprof.Cmdline(c.Writer, c.Request)
})
r.GET("/debug/profile", func(c *gin.Context) {
pprof.Profile(c.Writer, c.Request)
})
r.GET("/debug/symbol", func(c *gin.Context) {
pprof.Symbol(c.Writer, c.Request)
})
r.GET("/debug/trace", func(c *gin.Context) {
pprof.Trace(c.Writer, c.Request)
})
r.GET("/debug/heap", func(c *gin.Context) {
pprof.Handler("heap").ServeHTTP(c.Writer, c.Request)
})
r.GET("/debug/goroutine", func(c *gin.Context) {
pprof.Handler("goroutine").ServeHTTP(c.Writer, c.Request)
})
r.GET("/debug/block", func(c *gin.Context) {
pprof.Handler("block").ServeHTTP(c.Writer, c.Request)
})
}

// Collector
if collectorEnabled {
Expand Down

0 comments on commit 4c80aa6

Please sign in to comment.