Skip to content

Commit bcf633d

Browse files
Use standard endpoints for profiling (#892)
1 parent 83fcd72 commit bcf633d

2 files changed

Lines changed: 10 additions & 23 deletions

File tree

cmd/exporter/config/config.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ type Config struct {
3535
}
3636

3737
Server struct {
38-
Address string
39-
Path string
40-
Timeout time.Duration
41-
DebugAddress string
38+
Address string
39+
Path string
40+
Timeout time.Duration
4241
}
4342
LoggerOpts struct {
4443
Level string // Maps to slog levels: debug, info, warn, error

cmd/exporter/exporter.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"fmt"
88
"log/slog"
99
"net/http"
10-
_ "net/http/pprof"
10+
"net/http/pprof"
1111
"os"
1212
"os/signal"
1313
"strings"
@@ -96,7 +96,6 @@ func operationalFlags(cfg *config.Config) {
9696
flag.StringVar(&cfg.LoggerOpts.Level, "log.level", "info", "Log level: debug, info, warn, error")
9797
flag.StringVar(&cfg.LoggerOpts.Output, "log.output", "stdout", "Log output stream: stdout, stderr, file")
9898
flag.StringVar(&cfg.LoggerOpts.Type, "log.type", "text", "Log type: json, text")
99-
flag.StringVar(&cfg.Server.DebugAddress, "debug.address", ":6060", "Address for the pprof debug server (e.g. :6060). Disabled when empty.")
10099
}
101100

102101
// setupLogger is a helper method that is responsible for creating a structured logger that is used throughout the application.
@@ -106,27 +105,16 @@ func setupLogger(level string, output string, logtype string) *slog.Logger {
106105
return slog.New(handler)
107106
}
108107

109-
// startDebugServer starts a pprof HTTP server on cfg.Server.DebugAddress.
110-
// It is a no-op when DebugAddress is empty.
111-
func startDebugServer(ctx context.Context, cfg *config.Config, log *slog.Logger) {
112-
if cfg.Server.DebugAddress == "" {
113-
return
114-
}
115-
log.LogAttrs(ctx, slog.LevelInfo, "Starting pprof debug server", slog.String("address", cfg.Server.DebugAddress))
116-
go func() {
117-
// http.DefaultServeMux has pprof routes registered via the blank import.
118-
if err := http.ListenAndServe(cfg.Server.DebugAddress, http.DefaultServeMux); err != nil {
119-
log.LogAttrs(ctx, slog.LevelError, "pprof debug server stopped", slog.String("message", err.Error()))
120-
}
121-
}()
122-
}
123-
124108
// runServer is a helper method that is responsible for starting the metrics server and handling shutdown signals.
125109
func runServer(ctx context.Context, cfg *config.Config, csp provider.Provider, log *slog.Logger) error {
126-
startDebugServer(ctx, cfg, log)
127-
128110
mux := http.NewServeMux()
129111

112+
mux.HandleFunc("/debug/pprof/", pprof.Index)
113+
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
114+
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
115+
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
116+
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
117+
130118
mux.HandleFunc("/", web.HomePageHandler(cfg.Server.Path)) // landing page
131119

132120
registryHandler, err := createPromRegistryHandler(csp, regionFromConfig(cfg)) // prom metrics handler

0 commit comments

Comments
 (0)