feat(observability): 为 API 和 worker 增加独立 pprof 诊断端口
This commit is contained in:
51
backend/internal/observability/pprof.go
Normal file
51
backend/internal/observability/pprof.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package observability
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/pprof"
|
||||
"time"
|
||||
"context"
|
||||
)
|
||||
func NewPprofMux() *http.ServeMux {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/debug/pprof/", pprof.Index)
|
||||
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
|
||||
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
|
||||
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
|
||||
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
|
||||
|
||||
return mux
|
||||
}
|
||||
|
||||
func StartPprofServer(name string, enabled bool, addr string) (*http.Server, error) {
|
||||
if !enabled || addr == "" {
|
||||
return nil, nil
|
||||
}
|
||||
ln, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to start %s pprof server on %s: %w", name, addr, err)
|
||||
}
|
||||
server := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: NewPprofMux(),
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
}
|
||||
go func() {
|
||||
log.Printf("%s pprof listening on %s", name, addr)
|
||||
if err := server.Serve(ln); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Printf("%s pprof server error: %v", name, err)
|
||||
}
|
||||
}()
|
||||
return server, nil
|
||||
}
|
||||
|
||||
func Shutdown(ctx context.Context, srv *http.Server) error{
|
||||
if srv == nil {
|
||||
return nil
|
||||
}
|
||||
return srv.Shutdown(ctx)
|
||||
}
|
||||
Reference in New Issue
Block a user