~bigbes/core-go

77b64d236d29c7bba49560652b5eb902d36253e7 — Conrad Hoffmann 9 months ago a72070e
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
1 files changed, 29 insertions(+), 19 deletions(-)

M server/server.go
M server/server.go => server/server.go +29 -19
@@ 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)