metrics/exp: allow configuring metrics HTTP server on separate endpoint (#21290)

This commit is contained in:
Daniel Liu 2024-12-13 14:00:12 +08:00
parent d7d54b00f7
commit 3dee6675d2
2 changed files with 8 additions and 3 deletions

View file

@ -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 {
@ -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 {

View file

@ -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 {