Add optional Go pprof profiling serverUnverified
d629aca parent: be4bec9 modified
.env.example +3 -0 | @@ -24,3 +24,6 @@ GLEAN_EMBED_DIMENSION=2560 | ||
| 24 | 24 | GLEAN_LLM_BASE_URL=https://llms.example.com/v1 |
| 25 | 25 | GLEAN_LLM_API_KEY=API-KEY-001 |
| 26 | 26 | GLEAN_LLM_MODEL=qwen3.5-35b-a3b |
| 27 | +# Enable Go pprof profiling server (off by default). | |
| 28 | +# Set to a listen address like :6060 to enable, leave empty to disable. | |
| 29 | +# GLEAN_PPROF_ADDR=:6060 | |
| @@ -24,3 +24,6 @@ GLEAN_EMBED_DIMENSION=2560 | |||
| 24 | GLEAN_LLM_BASE_URL=https://llms.example.com/v1 | 24 | GLEAN_LLM_BASE_URL=https://llms.example.com/v1 |
| 25 | GLEAN_LLM_API_KEY=API-KEY-001 | 25 | GLEAN_LLM_API_KEY=API-KEY-001 |
| 26 | GLEAN_LLM_MODEL=qwen3.5-35b-a3b | 26 | GLEAN_LLM_MODEL=qwen3.5-35b-a3b |
| 27 | +# Enable Go pprof profiling server (off by default). | ||
| 28 | +# Set to a listen address like :6060 to enable, leave empty to disable. | ||
| 29 | +# GLEAN_PPROF_ADDR=:6060 | ||
modified
main.go +16 -0 | @@ -6,6 +6,7 @@ import ( | ||
| 6 | 6 | "fmt" |
| 7 | 7 | "log/slog" |
| 8 | 8 | "net/http" |
| 9 | + "net/http/pprof" | |
| 9 | 10 | "os" |
| 10 | 11 | "os/signal" |
| 11 | 12 | "strconv" |
| @@ -42,6 +43,21 @@ func main() { | ||
| 42 | 43 | |
| 43 | 44 | logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})) |
| 44 | 45 | |
| 46 | + if pprofAddr := envOr("GLEAN_PPROF_ADDR", ""); pprofAddr != "" { | |
| 47 | + mux := http.NewServeMux() | |
| 48 | + mux.HandleFunc("/debug/pprof/", pprof.Index) | |
| 49 | + mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) | |
| 50 | + mux.HandleFunc("/debug/pprof/profile", pprof.Profile) | |
| 51 | + mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) | |
| 52 | + mux.HandleFunc("/debug/pprof/trace", pprof.Trace) | |
| 53 | + go func() { | |
| 54 | + logger.Info("starting pprof server", "addr", pprofAddr) | |
| 55 | + if err := http.ListenAndServe(pprofAddr, mux); err != nil { | |
| 56 | + logger.Error("pprof server error", "error", err) | |
| 57 | + } | |
| 58 | + }() | |
| 59 | + } | |
| 60 | + | |
| 45 | 61 | vec.Auto() |
| 46 | 62 | dbs, err := db.Open(*dbPath) |
| 47 | 63 | if err != nil { |
| @@ -6,6 +6,7 @@ import ( | |||
| 6 | "fmt" | 6 | "fmt" |
| 7 | "log/slog" | 7 | "log/slog" |
| 8 | "net/http" | 8 | "net/http" |
| 9 | + "net/http/pprof" | ||
| 9 | "os" | 10 | "os" |
| 10 | "os/signal" | 11 | "os/signal" |
| 11 | "strconv" | 12 | "strconv" |
| @@ -42,6 +43,21 @@ func main() { | |||
| 42 | 43 | ||
| 43 | logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})) | 44 | logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})) |
| 44 | 45 | ||
| 46 | + if pprofAddr := envOr("GLEAN_PPROF_ADDR", ""); pprofAddr != "" { | ||
| 47 | + mux := http.NewServeMux() | ||
| 48 | + mux.HandleFunc("/debug/pprof/", pprof.Index) | ||
| 49 | + mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) | ||
| 50 | + mux.HandleFunc("/debug/pprof/profile", pprof.Profile) | ||
| 51 | + mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) | ||
| 52 | + mux.HandleFunc("/debug/pprof/trace", pprof.Trace) | ||
| 53 | + go func() { | ||
| 54 | + logger.Info("starting pprof server", "addr", pprofAddr) | ||
| 55 | + if err := http.ListenAndServe(pprofAddr, mux); err != nil { | ||
| 56 | + logger.Error("pprof server error", "error", err) | ||
| 57 | + } | ||
| 58 | + }() | ||
| 59 | + } | ||
| 60 | + | ||
| 45 | vec.Auto() | 61 | vec.Auto() |
| 46 | dbs, err := db.Open(*dbPath) | 62 | dbs, err := db.Open(*dbPath) |
| 47 | if err != nil { | 63 | if err != nil { |
modified
readme.md +1 -0 | @@ -72,6 +72,7 @@ Then open `http://localhost:8080`. | ||
| 72 | 72 | | `GLEAN_LLM_BASE_URL` | _(empty)_ | LLM API base URL for language detection (see below) | |
| 73 | 73 | | `GLEAN_LLM_API_KEY` | _(empty)_ | API key for the LLM endpoint | |
| 74 | 74 | | `GLEAN_LLM_MODEL` | `gpt-4o-mini` | LLM model name | |
| 75 | +| `GLEAN_PPROF_ADDR` | _(empty)_ | Enable pprof profiling server (e.g. `:6060`, off by default) | | |
| 75 | 76 | |
| 76 | 77 | For production: |
| 77 | 78 | |
| @@ -72,6 +72,7 @@ Then open `http://localhost:8080`. | |||
| 72 | | `GLEAN_LLM_BASE_URL` | _(empty)_ | LLM API base URL for language detection (see below) | | 72 | | `GLEAN_LLM_BASE_URL` | _(empty)_ | LLM API base URL for language detection (see below) | |
| 73 | | `GLEAN_LLM_API_KEY` | _(empty)_ | API key for the LLM endpoint | | 73 | | `GLEAN_LLM_API_KEY` | _(empty)_ | API key for the LLM endpoint | |
| 74 | | `GLEAN_LLM_MODEL` | `gpt-4o-mini` | LLM model name | | 74 | | `GLEAN_LLM_MODEL` | `gpt-4o-mini` | LLM model name | |
| 75 | +| `GLEAN_PPROF_ADDR` | _(empty)_ | Enable pprof profiling server (e.g. `:6060`, off by default) | | ||
| 75 | 76 | ||
| 76 | For production: | 77 | For production: |
| 77 | 78 | ||