Skip to content

Commit

Permalink
Move the pprof endpoints to the main HTTP server rather than starting…
Browse files Browse the repository at this point in the history
… a second server

Signed-off-by: Richard Wall <richard.wall@venafi.com>
  • Loading branch information
wallrj committed Oct 30, 2024
1 parent 1f00f09 commit 3da4d47
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pkg/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func InitAgentCmdFlags(c *cobra.Command, cfg *AgentCmdFlags) {
"enable-pprof",
"",
false,
"Enables the pprof profiling server on the agent (port: 6060).",
"Enables the pprof profiling endpoints on the agent server (port: 8081).",
)
c.PersistentFlags().BoolVarP(
&cfg.Prometheus,
Expand Down
21 changes: 10 additions & 11 deletions pkg/agent/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"github.com/jetstack/preflight/pkg/logs"
"github.com/jetstack/preflight/pkg/version"

_ "net/http/pprof"
"net/http/pprof"
)

var Flags AgentCmdFlags
Expand Down Expand Up @@ -74,19 +74,18 @@ func Run(cmd *cobra.Command, args []string) {
logs.Log.Fatalf("While evaluating configuration: %v", err)
}

if Flags.Profiling {
logs.Log.Printf("pprof profiling was enabled.\nRunning profiling on port :6060")
go func() {
err := http.ListenAndServe(":6060", nil)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
logs.Log.Fatalf("failed to run pprof profiler: %s", err)
}
}()
}

go func() {
server := http.NewServeMux()

if Flags.Profiling {
logs.Log.Printf("pprof profiling was enabled.")
server.HandleFunc("/debug/pprof/", pprof.Index)
server.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
server.HandleFunc("/debug/pprof/profile", pprof.Profile)
server.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
server.HandleFunc("/debug/pprof/trace", pprof.Trace)
}

if Flags.Prometheus {
logs.Log.Printf("Prometheus was enabled.\nRunning prometheus on port :8081")
prometheus.MustRegister(metricPayloadSize)
Expand Down

0 comments on commit 3da4d47

Please sign in to comment.