Skip to content

Commit

Permalink
metrics/exp: allow configuring metrics HTTP server on separate endpoi…
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan authored and JukLee0ira committed Dec 22, 2024
1 parent 1935685 commit 9893512
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
10 changes: 7 additions & 3 deletions internal/debug/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ func Setup(ctx *cli.Context) error {
// pprof server
if ctx.Bool(pprofFlag.Name) {
address := fmt.Sprintf("%s:%d", ctx.String(pprofAddrFlag.Name), ctx.Int(pprofPortFlag.Name))
StartPProf(address)
// This context value ("metrics-addr") represents the utils.MetricsHTTPFlag.Name.
// It cannot be imported because it will cause a cyclical dependency.
StartPProf(address, !ctx.IsSet("metrics-addr") && !ctx.IsSet("metrics.addr"))
}

if len(logFile) > 0 || rotation {
Expand All @@ -323,10 +325,12 @@ func Setup(ctx *cli.Context) error {
return nil
}

func StartPProf(address string) {
func StartPProf(address string, withMetrics bool) {
// Hook go-metrics into expvar on any /debug/metrics request, load all vars
// from the registry into expvar, and execute regular expvar handler.
exp.Exp(metrics.DefaultRegistry)
if withMetrics {
exp.Exp(metrics.DefaultRegistry)
}
log.Info("Starting pprof server", "addr", fmt.Sprintf("http://%s/debug/pprof", address))
go func() {
if err := http.ListenAndServe(address, nil); err != nil {
Expand Down
1 change: 1 addition & 0 deletions metrics/exp/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func ExpHandler(r metrics.Registry) http.Handler {
func Setup(address string) {
m := http.NewServeMux()
m.Handle("/debug/metrics", ExpHandler(metrics.DefaultRegistry))
m.Handle("/debug/metrics/prometheus", prometheus.Handler(metrics.DefaultRegistry))
log.Info("Starting metrics server", "addr", fmt.Sprintf("http://%s/debug/metrics", address))
go func() {
if err := http.ListenAndServe(address, m); err != nil {
Expand Down

0 comments on commit 9893512

Please sign in to comment.