Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pprof endpoint #3686

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
build: while true; do make -qs bin/goalert || make bin/goalert || (echo '\033[0;31mBuild Failure'; sleep 3); sleep 0.1; done

@watch-file=./bin/goalert
goalert: ./bin/goalert -l=localhost:3030 --ui-dir=web/src/build --db-url=postgres://goalert@localhost --listen-sysapi=localhost:1234 --listen-prometheus=localhost:2112 --smtp-listen=localhost:9025 --email-integration-domain=localhost
goalert: ./bin/goalert -l=localhost:3030 --ui-dir=web/src/build --db-url=postgres://goalert@localhost --listen-sysapi=localhost:1234 --listen-prometheus=localhost:2112 --smtp-listen=localhost:9025 --email-integration-domain=localhost --listen-pprof=localhost:6060 --pprof-mutex-profile-fraction=1 --pprof-block-profile-rate=1000

smtp: go run github.com/mailhog/MailHog -ui-bind-addr=localhost:8025 -api-bind-addr=localhost:8025 -smtp-bind-addr=localhost:1025 | grep -v KEEPALIVE
prom: bin/tools/prometheus --log.level=warn --config.file=devtools/prometheus/prometheus.yml --storage.tsdb.path=bin/prom-data/ --web.listen-address=localhost:9090
Expand Down
2 changes: 1 addition & 1 deletion Procfile.prod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
build: while true; do make -qs bin/goalert BUNDLE=1 || make bin/goalert BUNDLE=1 || (echo '\033[0;31mBuild Failure'; sleep 3); sleep 0.1; done

@watch-file=./bin/goalert
goalert: ./bin/goalert -l=localhost:3030 --db-url=postgres://goalert@localhost --listen-sysapi=localhost:1234 --listen-prometheus=localhost:2112
goalert: ./bin/goalert -l=localhost:3030 --db-url=postgres://goalert@localhost --listen-sysapi=localhost:1234 --listen-prometheus=localhost:2112 --listen-pprof=localhost:6060 --pprof-mutex-profile-fraction=1 --pprof-block-profile-rate=1000

smtp: go run github.com/mailhog/MailHog -ui-bind-addr=localhost:8025 -api-bind-addr=localhost:8025 -smtp-bind-addr=localhost:1025 | grep -v KEEPALIVE
prom: bin/tools/prometheus --log.level=warn --config.file=devtools/prometheus/prometheus.yml --storage.tsdb.path=bin/prom-data/ --web.listen-address=localhost:9090
Expand Down
14 changes: 13 additions & 1 deletion app/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,12 @@ Available Flags:
if err != nil {
return err
}
ctx := cmd.Context()
err = initPprofServer()
if err != nil {
return err
}

ctx := cmd.Context()
cfg, err := getConfig(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -374,6 +378,11 @@ Migration: %s (#%d)
return err
}

err = initPprofServer()
if err != nil {
return err
}

mon, err := remotemonitor.NewMonitor(cfg)
if err != nil {
return err
Expand Down Expand Up @@ -733,6 +742,9 @@ func init() {
RootCmd.Flags().String("public-url", "", "Externally routable URL to the application. Used for validating callback requests, links, auth, and prefix calculation.")

RootCmd.PersistentFlags().StringP("listen-prometheus", "p", "", "Bind address for Prometheus metrics.")
RootCmd.PersistentFlags().String("listen-pprof", "", "Bind address for pprof.")
RootCmd.PersistentFlags().Int("pprof-block-profile-rate", 0, "Set the block profile rate in hz.")
RootCmd.PersistentFlags().Int("pprof-mutex-profile-fraction", 0, "Set the mutex profile fraction (rate is 1/this-value).")

RootCmd.Flags().String("tls-cert-file", "", "Specifies a path to a PEM-encoded certificate. Has no effect if --listen-tls is unset.")
RootCmd.Flags().String("tls-key-file", "", "Specifies a path to a PEM-encoded private key file. Has no effect if --listen-tls is unset.")
Expand Down
40 changes: 40 additions & 0 deletions app/pprof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package app

import (
"net"
"net/http"
"net/http/pprof"
"runtime"

"github.com/spf13/viper"
)

func initPprofServer() error {
addr := viper.GetString("listen-pprof")
if addr == "" {
return nil
}

l, err := net.Listen("tcp", addr)
if err != nil {
return err
}

mux := http.NewServeMux()

// Register pprof handlers (matches init() of net/http/pprof package)
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)

runtime.SetBlockProfileRate(viper.GetInt("pprof-block-profile-rate"))
runtime.SetMutexProfileFraction(viper.GetInt("pprof-mutex-profile-fraction"))

srv := http.Server{
Handler: mux,
}
go func() { _ = srv.Serve(l) }()
return nil
}
5 changes: 5 additions & 0 deletions web/src/app/localdev/LocalDev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ export default function LocalDev(): JSX.Element {
'OIDC.UserInfoNamePath': 'preferred_username',
}}
/>
<DevTool
name='pprof'
desc='Debug and profile the running server.'
url='http://localhost:6060/debug/pprof/'
/>
</Grid>

{updateConfig && (
Expand Down
Loading