Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI flags for controlling HTTP server timeouts #4167

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cmd/collector/app/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ const (

flagSuffixHostPort = "host-port"

flagSuffixHTTPReadTimeout = "read-timeout"
flagSuffixHTTPReadHeaderTimeout = "read-header-timeout"
flagSuffixHTTPIdleTimeout = "idle-timeout"

flagSuffixGRPCMaxReceiveMessageLength = "max-message-size"
flagSuffixGRPCMaxConnectionAge = "max-connection-age"
flagSuffixGRPCMaxConnectionAgeGrace = "max-connection-age-grace"
Expand Down Expand Up @@ -140,6 +144,12 @@ type HTTPOptions struct {
HostPort string
// TLS configures secure transport for HTTP endpoint
TLS tlscfg.Options
// ReadTimeout sets the respective parameter of http.Server
ReadTimeout time.Duration
// ReadHeaderTimeout sets the respective parameter of http.Server
ReadHeaderTimeout time.Duration
// IdleTimeout sets the respective parameter of http.Server
IdleTimeout time.Duration
}

// GRPCOptions defines options for a gRPC server
Expand Down Expand Up @@ -185,6 +195,9 @@ func AddFlags(flags *flag.FlagSet) {

func addHTTPFlags(flags *flag.FlagSet, cfg serverFlagsConfig, defaultHostPort string) {
flags.String(cfg.prefix+"."+flagSuffixHostPort, defaultHostPort, "The host:port (e.g. 127.0.0.1:12345 or :12345) of the collector's HTTP server")
flags.Duration(cfg.prefix+"."+flagSuffixHTTPIdleTimeout, 0, "See https://pkg.go.dev/net/http#Server")
flags.Duration(cfg.prefix+"."+flagSuffixHTTPReadTimeout, 0, "See https://pkg.go.dev/net/http#Server")
flags.Duration(cfg.prefix+"."+flagSuffixHTTPReadHeaderTimeout, 2*time.Second, "See https://pkg.go.dev/net/http#Server")
cfg.tls.AddFlags(flags)
}

Expand All @@ -210,6 +223,9 @@ func addGRPCFlags(flags *flag.FlagSet, cfg serverFlagsConfig, defaultHostPort st

func (opts *HTTPOptions) initFromViper(v *viper.Viper, logger *zap.Logger, cfg serverFlagsConfig) error {
opts.HostPort = ports.FormatHostPort(v.GetString(cfg.prefix + "." + flagSuffixHostPort))
opts.IdleTimeout = v.GetDuration(cfg.prefix + "." + flagSuffixHTTPIdleTimeout)
opts.ReadTimeout = v.GetDuration(cfg.prefix + "." + flagSuffixHTTPReadTimeout)
opts.ReadHeaderTimeout = v.GetDuration(cfg.prefix + "." + flagSuffixHTTPReadHeaderTimeout)
if tlsOpts, err := cfg.tls.InitFromViper(v); err == nil {
opts.TLS = tlsOpts
} else {
Expand Down
6 changes: 6 additions & 0 deletions cmd/collector/app/flags/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,18 @@ func TestCollectorOptionsWithFlags_CheckMaxConnectionAge(t *testing.T) {
command.ParseFlags([]string{
"--collector.grpc-server.max-connection-age=5m",
"--collector.grpc-server.max-connection-age-grace=1m",
"--collector.http-server.idle-timeout=5m",
"--collector.http-server.read-timeout=6m",
"--collector.http-server.read-header-timeout=5s",
})
_, err := c.InitFromViper(v, zap.NewNop())
require.NoError(t, err)

assert.Equal(t, 5*time.Minute, c.GRPC.MaxConnectionAge)
assert.Equal(t, time.Minute, c.GRPC.MaxConnectionAgeGrace)
assert.Equal(t, 5*time.Minute, c.HTTP.IdleTimeout)
assert.Equal(t, 6*time.Minute, c.HTTP.ReadTimeout)
assert.Equal(t, 5*time.Second, c.HTTP.ReadHeaderTimeout)
}

func TestCollectorOptionsWithFlags_CheckNoTenancy(t *testing.T) {
Expand Down
11 changes: 10 additions & 1 deletion cmd/collector/app/server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ type HTTPServerParams struct {
MetricsFactory metrics.Factory
HealthCheck *healthcheck.HealthCheck
Logger *zap.Logger

// ReadTimeout sets the respective parameter of http.Server
ReadTimeout time.Duration
// ReadHeaderTimeout sets the respective parameter of http.Server
ReadHeaderTimeout time.Duration
// IdleTimeout sets the respective parameter of http.Server
IdleTimeout time.Duration
}

// StartHTTPServer based on the given parameters
Expand All @@ -51,8 +58,10 @@ func StartHTTPServer(params *HTTPServerParams) (*http.Server, error) {
errorLog, _ := zap.NewStdLogAt(params.Logger, zapcore.ErrorLevel)
server := &http.Server{
Addr: params.HostPort,
ReadTimeout: params.ReadTimeout,
ReadHeaderTimeout: params.ReadHeaderTimeout,
IdleTimeout: params.IdleTimeout,
ErrorLog: errorLog,
ReadHeaderTimeout: 2 * time.Second,
}
if params.TLSConfig.Enabled {
tlsCfg, err := params.TLSConfig.Config(params.Logger) // This checks if the certificates are correctly provided
Expand Down
24 changes: 23 additions & 1 deletion cmd/collector/app/server/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"

"github.com/jaegertracing/jaeger/cmd/collector/app/handler"
"github.com/jaegertracing/jaeger/internal/metricstest"
Expand Down Expand Up @@ -201,7 +202,7 @@ func TestSpanCollectorHTTPS(t *testing.T) {

for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
logger, _ := zap.NewDevelopment()
logger := zaptest.NewLogger(t)
params := &HTTPServerParams{
HostPort: fmt.Sprintf(":%d", ports.CollectorHTTP),
Handler: handler.NewJaegerSpanHandler(logger, &mockSpanProcessor{}),
Expand Down Expand Up @@ -257,3 +258,24 @@ func TestSpanCollectorHTTPS(t *testing.T) {
})
}
}

func TestStartHTTPServerParams(t *testing.T) {
logger := zaptest.NewLogger(t)
params := &HTTPServerParams{
HostPort: fmt.Sprintf(":%d", ports.CollectorHTTP),
Handler: handler.NewJaegerSpanHandler(logger, &mockSpanProcessor{}),
SamplingStore: &mockSamplingStore{},
MetricsFactory: metricstest.NewFactory(time.Hour),
HealthCheck: healthcheck.New(),
Logger: logger,
IdleTimeout: 5 * time.Minute,
ReadTimeout: 6 * time.Minute,
ReadHeaderTimeout: 7 * time.Second,
}

server, err := StartHTTPServer(params)
require.NoError(t, err)
assert.Equal(t, 5*time.Minute, server.IdleTimeout)
assert.Equal(t, 6*time.Minute, server.ReadTimeout)
assert.Equal(t, 7*time.Second, server.ReadHeaderTimeout)
}