add pprof handler
Some checks failed
build container / build-container (push) Failing after 22s
run go test / test (push) Failing after 19s

This commit is contained in:
2025-03-03 00:26:29 +08:00
parent 629c095bbe
commit f9ff71f62a
6 changed files with 59 additions and 13 deletions

17
cmd/proxy/debug.go Normal file
View File

@ -0,0 +1,17 @@
//go:build pprof
package main
import (
"flag"
"os"
)
func init() {
flag.BoolVar(&pprofEnabled, "pprof", true, "")
if v, ok := os.LookupEnv("SENTRY_DSN"); ok {
sentrydsn = v
}
flag.StringVar(&sentrydsn, "sentry", sentrydsn, "sentry dsn to report errors")
}

View File

@ -4,6 +4,7 @@ import (
"flag"
"log/slog"
"net/http"
"net/http/pprof"
"os"
"path/filepath"
"time"
@ -22,6 +23,7 @@ var (
configFilePath = "config.yaml"
logLevel = "info"
sentrydsn = ""
pprofEnabled = false
)
func init() {
@ -31,12 +33,8 @@ func init() {
if v, ok := os.LookupEnv("LOG_LEVEL"); ok {
logLevel = v
}
if v, ok := os.LookupEnv("SENTRY_DSN"); ok {
sentrydsn = v
}
flag.StringVar(&configFilePath, "config", configFilePath, "path to config file")
flag.StringVar(&logLevel, "log-level", logLevel, "log level. (trace, debug, info, warn, error)")
flag.StringVar(&sentrydsn, "sentry", sentrydsn, "sentry dsn to report errors")
}
func configFromFile(path string) (*cacheproxy.Config, error) {
@ -124,6 +122,14 @@ func main() {
mux.HandleFunc("GET /", server.HandleRequestWithCache)
if pprofEnabled {
mux.HandleFunc("GET /debug/pprof/", pprof.Index)
mux.HandleFunc("GET /debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("GET /debug/pprof/profile", pprof.Profile)
mux.HandleFunc("GET /debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("GET /debug/pprof/trace", pprof.Trace)
}
slog.With("addr", ":8881").Info("serving app")
if err := http.ListenAndServe(":8881", middleware.Use(mux,
recover.Recover(),