From 77b64d236d29c7bba49560652b5eb902d36253e7 Mon Sep 17 00:00:00 2001 From: Conrad Hoffmann Date: Mon, 10 Nov 2025 12:53:40 +0100 Subject: [PATCH] server: allow setting ports for metrics, pprof With an eye towards Kubernetes, it is desirable to avoid having the API pick random ports for anything. This commit introduces additional flags for the API server to specify the metrics and pprof ports. This change is backwards-compatible insofar as the default behavior is preserved if the flags are not used. The log output is changed to include the full address that the respective listeners are listening on. Note that using "0.0.0.0:1234" might not quite work as expected, because Go [1]. But that does not change the default behavior, so I guess it's fine. [1] https://github.com/golang/go/issues/48723 --- server/server.go | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/server/server.go b/server/server.go index 302162a7cb278d615e2b2523cb0c88bd84e23768..126a39a013c0f97d86916f04f83b5790ee2be7b0 100644 --- a/server/server.go +++ b/server/server.go @@ -63,15 +63,17 @@ var ( type Server struct { Schema graphql.ExecutableSchema - addr string - conf ini.File - db *sql.DB - redis goRedis.UniversalClient - root chi.Router - router chi.Router - service string - queues []*work.Queue - email *email.Queue + addr string + metricsAddr string + pprofAddr string + conf ini.File + db *sql.DB + redis goRedis.UniversalClient + root chi.Router + router chi.Router + service string + queues []*work.Queue + email *email.Queue MaxComplexity int } @@ -81,8 +83,10 @@ type Server struct { // up the server and initializing the [crypto] subsystem. func New(service, defaultAddr string, conf ini.File, args []string) *Server { addr := defaultAddr + metricsAddr := ":0" + pprofAddr := "localhost:0" - opts, _, err := getopt.Getopts(args, "b:d") + opts, _, err := getopt.Getopts(args, "b:dm:p:") if err != nil { panic(err) } @@ -93,6 +97,10 @@ func New(service, defaultAddr string, conf ini.File, args []string) *Server { addr = opt.Value case 'd': debug = true + case 'm': + metricsAddr = opt.Value + case 'p': + pprofAddr = opt.Value } } @@ -100,11 +108,13 @@ func New(service, defaultAddr string, conf ini.File, args []string) *Server { root := chi.NewRouter() server := &Server{ - addr: addr, - conf: conf, - root: root, - router: root.Group(func(_ chi.Router) {}), - service: service, + addr: addr, + metricsAddr: metricsAddr, + pprofAddr: pprofAddr, + conf: conf, + root: root, + router: root.Group(func(_ chi.Router) {}), + service: service, } return server } @@ -363,18 +373,18 @@ func (server *Server) Run() { mux := &http.ServeMux{} mux.Handle("/metrics", promhttp.Handler()) pserver := &http.Server{Handler: mux} - plisten, err := net.Listen("tcp", ":0") + plisten, err := net.Listen("tcp", server.metricsAddr) if err != nil { panic(err) } - log.Printf("Prometheus listening on :%d", plisten.Addr().(*net.TCPAddr).Port) + log.Printf("Prometheus listening on %s", plisten.Addr().String()) go pserver.Serve(plisten) - pplisten, err := net.Listen("tcp", "localhost:0") + pplisten, err := net.Listen("tcp", server.pprofAddr) if err != nil { panic(err) } - log.Printf("pprof listening on :%d", pplisten.Addr().(*net.TCPAddr).Port) + log.Printf("pprof listening on %s", pplisten.Addr().String()) go http.Serve(pplisten, nil) sig := make(chan os.Signal, 1)