mirror of https://github.com/coder/coder.git
feat: pprof is always on (#4951)
This commit is contained in:
parent
16384f8594
commit
bf4a6fb5b5
18
cli/agent.go
18
cli/agent.go
|
@ -4,7 +4,7 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
_ "net/http/pprof" //nolint: gosec
|
||||
"net/http/pprof"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -28,7 +28,6 @@ import (
|
|||
func workspaceAgent() *cobra.Command {
|
||||
var (
|
||||
auth string
|
||||
pprofEnabled bool
|
||||
pprofAddress string
|
||||
noReap bool
|
||||
)
|
||||
|
@ -82,15 +81,11 @@ func workspaceAgent() *cobra.Command {
|
|||
// Set a reasonable timeout so requests can't hang forever!
|
||||
client.HTTPClient.Timeout = 10 * time.Second
|
||||
|
||||
if pprofEnabled {
|
||||
srvClose := serveHandler(cmd.Context(), logger, nil, pprofAddress, "pprof")
|
||||
defer srvClose()
|
||||
} else {
|
||||
// If pprof wasn't enabled at startup, allow a
|
||||
// `kill -USR1 $agent_pid` to start it (on Unix).
|
||||
srvClose := agentStartPPROFOnUSR1(cmd.Context(), logger, pprofAddress)
|
||||
defer srvClose()
|
||||
}
|
||||
// Enable pprof handler
|
||||
// This prevents the pprof import from being accidentally deleted.
|
||||
_ = pprof.Handler
|
||||
pprofSrvClose := serveHandler(cmd.Context(), logger, nil, pprofAddress, "pprof")
|
||||
defer pprofSrvClose()
|
||||
|
||||
// exchangeToken returns a session token.
|
||||
// This is abstracted to allow for the same looping condition
|
||||
|
@ -177,7 +172,6 @@ func workspaceAgent() *cobra.Command {
|
|||
}
|
||||
|
||||
cliflag.StringVarP(cmd.Flags(), &auth, "auth", "", "CODER_AGENT_AUTH", "token", "Specify the authentication type to use for the agent")
|
||||
cliflag.BoolVarP(cmd.Flags(), &pprofEnabled, "pprof-enable", "", "CODER_AGENT_PPROF_ENABLE", false, "Enable serving pprof metrics on the address defined by --pprof-address.")
|
||||
cliflag.BoolVarP(cmd.Flags(), &noReap, "no-reap", "", "", false, "Do not start a process reaper.")
|
||||
cliflag.StringVarP(cmd.Flags(), &pprofAddress, "pprof-address", "", "CODER_AGENT_PPROF_ADDRESS", "127.0.0.1:6060", "The address to serve pprof.")
|
||||
return cmd
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
//go:build !windows
|
||||
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"cdr.dev/slog"
|
||||
)
|
||||
|
||||
func agentStartPPROFOnUSR1(ctx context.Context, logger slog.Logger, pprofAddress string) (srvClose func()) {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
|
||||
usr1 := make(chan os.Signal, 1)
|
||||
signal.Notify(usr1, syscall.SIGUSR1)
|
||||
go func() {
|
||||
defer close(usr1)
|
||||
defer signal.Stop(usr1)
|
||||
|
||||
select {
|
||||
case <-usr1:
|
||||
signal.Stop(usr1)
|
||||
srvClose := serveHandler(ctx, logger, nil, pprofAddress, "pprof")
|
||||
defer srvClose()
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
<-ctx.Done() // Prevent defer close until done.
|
||||
}()
|
||||
|
||||
return func() {
|
||||
cancel()
|
||||
<-usr1 // Wait until usr1 is closed, ensures srvClose was run.
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cdr.dev/slog"
|
||||
)
|
||||
|
||||
// agentStartPPROFOnUSR1 is no-op on Windows (no SIGUSR1 signal).
|
||||
func agentStartPPROFOnUSR1(ctx context.Context, logger slog.Logger, pprofAddress string) (srvClose func()) {
|
||||
return func() {}
|
||||
}
|
Loading…
Reference in New Issue