Skip to content

Commit 7b141fd

Browse files
authored
feat: optionally enable pprof endpoint in extproc (#1395)
**Description** This optionally enables pprof endpoint in order to troubleshoot and debug the running application easily, including lock contentions. --------- Signed-off-by: Takeshi Yoneda <[email protected]>
1 parent dbd6e61 commit 7b141fd

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

cmd/extproc/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ package main
77

88
import (
99
"context"
10+
"errors"
1011
"log"
12+
"net/http"
13+
"net/http/pprof"
1114
"os"
1215
"os/signal"
1316
"syscall"
@@ -17,6 +20,27 @@ import (
1720
)
1821

1922
func main() {
23+
// Run the pprof server if the DISABLE_PPROF environment variable is not set.
24+
//
25+
// Enabling the pprof server by default helps with debugging performance issues in production.
26+
// The impact should be negligible when the actual pprof endpoints are not being accessed.
27+
if _, ok := os.LookupEnv("DISABLE_PPROF"); !ok {
28+
go func() {
29+
const pprofPort = "6060" // The same default port as in th Go pprof documentation.
30+
mux := http.NewServeMux()
31+
mux.HandleFunc("/debug/pprof/", pprof.Index)
32+
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
33+
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
34+
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
35+
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
36+
server := &http.Server{Addr: ":" + pprofPort, Handler: mux, ReadHeaderTimeout: 5 * time.Second}
37+
log.Printf("starting pprof server on port %s", pprofPort)
38+
if err := server.ListenAndServe(); err != nil && errors.Is(err, http.ErrServerClosed) {
39+
log.Printf("pprof server stopped: %v", err)
40+
}
41+
}()
42+
}
43+
2044
ctx, cancel := context.WithCancel(context.Background())
2145
signalsChan := make(chan os.Signal, 1)
2246
signal.Notify(signalsChan, syscall.SIGINT, syscall.SIGTERM)

manifests/charts/ai-gateway-helm/values.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ extProc:
3232
## value: "ai-gateway-extproc"
3333
## - name: OTEL_TRACES_EXPORTER
3434
## value: "otlp"
35+
## # This disables pprof endpoint at "localhost:6060/debug/pprof" that can be accessed via port-forwarding for profiling.
36+
## # Enabled by default for troubleshooting purposes given the impact is negligible when not in use.
37+
## - name: DISABLE_PPROF
38+
## value: "true"
3539
##
3640
extraEnvVars: []
3741

0 commit comments

Comments
 (0)