Skip to content

Commit

Permalink
Merge pull request prometheus#167 from ThoreKr/server-listen-addr
Browse files Browse the repository at this point in the history
Allow user to specify HTTP and gRPC bind addresses (not just ports.) (Rebase of prometheus#153)
  • Loading branch information
bboreham committed Nov 3, 2019
2 parents a2b2a63 + ca6b2a6 commit 0e7cefa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
14 changes: 9 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import (

// Config for a Server
type Config struct {
MetricsNamespace string `yaml:"-"`
HTTPListenPort int `yaml:"http_listen_port"`
GRPCListenPort int `yaml:"grpc_listen_port"`
MetricsNamespace string `yaml:"-"`
HTTPListenAddress string `yaml:"http_listen_address"`
HTTPListenPort int `yaml:"http_listen_port"`
GRPCListenAddress string `yaml:"grpc_listen_address"`
GRPCListenPort int `yaml:"grpc_listen_port"`

RegisterInstrumentation bool `yaml:"register_instrumentation"`
ExcludeRequestInLog bool `yaml:"-"`
Expand All @@ -56,7 +58,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.HTTPListenAddress, "server.http-listen-address", "", "HTTP server listen address.")
f.IntVar(&cfg.HTTPListenPort, "server.http-listen-port", 80, "HTTP server listen port.")
f.StringVar(&cfg.GRPCListenAddress, "server.grpc-listen-address", "", "gRPC server listen address.")
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")
Expand Down Expand Up @@ -88,12 +92,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.HTTPListenAddress, 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.GRPCListenAddress, cfg.GRPCListenPort))
if err != nil {
return nil, err
}
Expand Down
17 changes: 11 additions & 6 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (f FakeServer) Succeed(ctx context.Context, req *google_protobuf.Empty) (*g
func TestErrorInstrumentationMiddleware(t *testing.T) {
var cfg Config
cfg.RegisterFlags(flag.NewFlagSet("", flag.ExitOnError))
cfg.GRPCListenAddress = "localhost"
cfg.GRPCListenPort = 1234
server, err := New(cfg)
require.NoError(t, err)
Expand Down Expand Up @@ -102,8 +103,10 @@ func TestErrorInstrumentationMiddleware(t *testing.T) {

func TestRunReturnsError(t *testing.T) {
cfg := Config{
HTTPListenPort: 9190,
GRPCListenPort: 9191,
HTTPListenAddress: "localhost",
HTTPListenPort: 9190,
GRPCListenAddress: "localhost",
GRPCListenPort: 9191,
}
t.Run("http", func(t *testing.T) {
cfg.MetricsNamespace = "testing_http"
Expand Down Expand Up @@ -142,10 +145,12 @@ func TestMiddlewareLogging(t *testing.T) {
var level logging.Level
level.Set("info")
cfg := Config{
HTTPListenPort: 9192,
HTTPMiddleware: []middleware.Interface{middleware.Logging},
MetricsNamespace: "testing_logging",
LogLevel: level,
HTTPListenAddress: "localhost",
HTTPListenPort: 9192,
GRPCListenAddress: "localhost",
HTTPMiddleware: []middleware.Interface{middleware.Logging},
MetricsNamespace: "testing_logging",
LogLevel: level,
}
server, err := New(cfg)
require.NoError(t, err)
Expand Down

0 comments on commit 0e7cefa

Please sign in to comment.