|
|
|
@ -27,7 +27,9 @@ import ( |
|
|
|
|
// Config for a Server
|
|
|
|
|
type Config struct { |
|
|
|
|
MetricsNamespace string `yaml:"-"` |
|
|
|
|
HTTPListenHost string `yaml:"http_listen_host"` |
|
|
|
|
HTTPListenPort int `yaml:"http_listen_port"` |
|
|
|
|
GRPCListenHost string `yaml:"grpc_listen_host"` |
|
|
|
|
GRPCListenPort int `yaml:"grpc_listen_port"` |
|
|
|
|
|
|
|
|
|
RegisterInstrumentation bool `yaml:"-"` |
|
|
|
@ -55,7 +57,9 @@ type Config struct { |
|
|
|
|
|
|
|
|
|
// RegisterFlags adds the flags required to config this to the given FlagSet
|
|
|
|
|
func (cfg *Config) RegisterFlags(f *flag.FlagSet) { |
|
|
|
|
f.StringVar(&cfg.HTTPListenHost, "server.http-listen-host", "", "HTTP server listen host.") |
|
|
|
|
f.IntVar(&cfg.HTTPListenPort, "server.http-listen-port", 80, "HTTP server listen port.") |
|
|
|
|
f.StringVar(&cfg.GRPCListenHost, "server.grpc-listen-host", "", "gRPC server listen host.") |
|
|
|
|
f.IntVar(&cfg.GRPCListenPort, "server.grpc-listen-port", 9095, "gRPC server listen port.") |
|
|
|
|
f.BoolVar(&cfg.RegisterInstrumentation, "server.register-instrumentation", true, "Register the intrumentation handlers (/metrics etc).") |
|
|
|
|
f.DurationVar(&cfg.ServerGracefulShutdownTimeout, "server.graceful-shutdown-timeout", 30*time.Second, "Timeout for graceful shutdowns") |
|
|
|
@ -87,12 +91,12 @@ type Server struct { |
|
|
|
|
// New makes a new Server
|
|
|
|
|
func New(cfg Config) (*Server, error) { |
|
|
|
|
// Setup listeners first, so we can fail early if the port is in use.
|
|
|
|
|
httpListener, err := net.Listen("tcp", fmt.Sprintf(":%d", cfg.HTTPListenPort)) |
|
|
|
|
httpListener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.HTTPListenHost, cfg.HTTPListenPort)) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
grpcListener, err := net.Listen("tcp", fmt.Sprintf(":%d", cfg.GRPCListenPort)) |
|
|
|
|
grpcListener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.GRPCListenHost, cfg.GRPCListenPort)) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
@ -150,12 +154,14 @@ func New(cfg Config) (*Server, error) { |
|
|
|
|
|
|
|
|
|
// Setup HTTP server
|
|
|
|
|
router := mux.NewRouter() |
|
|
|
|
if cfg.RegisterInstrumentation { |
|
|
|
|
RegisterInstrumentation(router) |
|
|
|
|
} |
|
|
|
|
if cfg.PathPrefix != "" { |
|
|
|
|
// Expect metrics and pprof handlers to be prefixed with server's path prefix.
|
|
|
|
|
// e.g. /loki/metrics or /loki/debug/pprof
|
|
|
|
|
router = router.PathPrefix(cfg.PathPrefix).Subrouter() |
|
|
|
|
} |
|
|
|
|
if cfg.RegisterInstrumentation { |
|
|
|
|
RegisterInstrumentation(router) |
|
|
|
|
} |
|
|
|
|
httpMiddleware := []middleware.Interface{ |
|
|
|
|
middleware.Tracer{ |
|
|
|
|
RouteMatcher: router, |
|
|
|
|