diff --git a/config/confighttp/confighttp.go b/config/confighttp/confighttp.go index da803632e60a..5d07b3e50478 100644 --- a/config/confighttp/confighttp.go +++ b/config/confighttp/confighttp.go @@ -306,13 +306,51 @@ type ServerConfig struct { // CompressionAlgorithms configures the list of compression algorithms the server can accept. Default: ["", "gzip", "zstd", "zlib", "snappy", "deflate"] CompressionAlgorithms []string `mapstructure:"compression_algorithms"` + + // ReadTimeout is the maximum duration for reading the entire + // request, including the body. A zero or negative value means + // there will be no timeout. + // + // Because ReadTimeout does not let Handlers make per-request + // decisions on each request body's acceptable deadline or + // upload rate, most users will prefer to use + // ReadHeaderTimeout. It is valid to use them both. + ReadTimeout time.Duration + + // ReadHeaderTimeout is the amount of time allowed to read + // request headers. The connection's read deadline is reset + // after reading the headers and the Handler can decide what + // is considered too slow for the body. If ReadHeaderTimeout + // is zero, the value of ReadTimeout is used. If both are + // zero, there is no timeout. + ReadHeaderTimeout time.Duration + + // WriteTimeout is the maximum duration before timing out + // writes of the response. It is reset whenever a new + // request's header is read. Like ReadTimeout, it does not + // let Handlers make decisions on a per-request basis. + // A zero or negative value means there will be no timeout. + WriteTimeout time.Duration + + // IdleTimeout is the maximum amount of time to wait for the + // next request when keep-alives are enabled. If IdleTimeout + // is zero, the value of ReadTimeout is used. If both are + // zero, there is no timeout. + IdleTimeout time.Duration } // NewDefaultServerConfig returns ServerConfig type object with default values. -// Currently, config options are all initialized as zero values. // We encourage to use this function to create an object of ServerConfig. func NewDefaultServerConfig() ServerConfig { - return ServerConfig{} + tlsDefaultServerConfig := configtls.NewDefaultServerConfig() + return ServerConfig{ + ResponseHeaders: map[string]configopaque.String{}, + TLSSetting: &tlsDefaultServerConfig, + CORS: &CORSConfig{}, + WriteTimeout: 30 * time.Second, + ReadHeaderTimeout: 1 * time.Minute, + IdleTimeout: 1 * time.Minute, + } } // ToListener creates a net.Listener. @@ -435,9 +473,15 @@ func (hss *ServerConfig) ToServer(_ context.Context, host component.Host, settin includeMetadata: hss.IncludeMetadata, } - return &http.Server{ + server := &http.Server{ Handler: handler, - }, nil + } + server.ReadTimeout = hss.ReadTimeout + server.ReadHeaderTimeout = hss.ReadHeaderTimeout + server.WriteTimeout = hss.WriteTimeout + server.IdleTimeout = hss.IdleTimeout + + return server, nil } func responseHeadersHandler(handler http.Handler, headers map[string]configopaque.String) http.Handler { diff --git a/config/confighttp/confighttp_test.go b/config/confighttp/confighttp_test.go index 78b44c9f4bfb..7cea4242be54 100644 --- a/config/confighttp/confighttp_test.go +++ b/config/confighttp/confighttp_test.go @@ -1492,3 +1492,14 @@ func BenchmarkHttpRequest(b *testing.B) { }) } } + +func TestDefaultHTTPServerSettings(t *testing.T) { + httpServerSettings := NewDefaultServerConfig() + assert.NotNil(t, httpServerSettings.ResponseHeaders) + assert.NotNil(t, httpServerSettings.CORS) + assert.NotNil(t, httpServerSettings.TLSSetting) + assert.Equal(t, 1*time.Minute, httpServerSettings.IdleTimeout) + assert.Equal(t, 30*time.Second, httpServerSettings.WriteTimeout) + assert.Equal(t, time.Duration(0), httpServerSettings.ReadTimeout) + assert.Equal(t, 1*time.Minute, httpServerSettings.ReadHeaderTimeout) +} diff --git a/receiver/otlpreceiver/factory.go b/receiver/otlpreceiver/factory.go index 784426504397..221819298794 100644 --- a/receiver/otlpreceiver/factory.go +++ b/receiver/otlpreceiver/factory.go @@ -39,8 +39,6 @@ func NewFactory() receiver.Factory { // createDefaultConfig creates the default configuration for receiver. func createDefaultConfig() component.Config { - serverConfig := confighttp.NewDefaultServerConfig() - serverConfig.Endpoint = localhostgate.EndpointForPort(httpPort) return &Config{ Protocols: Protocols{ GRPC: &configgrpc.ServerConfig{ @@ -52,7 +50,9 @@ func createDefaultConfig() component.Config { ReadBufferSize: 512 * 1024, }, HTTP: &HTTPConfig{ - ServerConfig: &serverConfig, + ServerConfig: &confighttp.ServerConfig{ + Endpoint: localhostgate.EndpointForPort(httpPort), + }, TracesURLPath: defaultTracesURLPath, MetricsURLPath: defaultMetricsURLPath, LogsURLPath: defaultLogsURLPath, diff --git a/receiver/otlpreceiver/factory_test.go b/receiver/otlpreceiver/factory_test.go index ffbb08a1d9d4..ad22a41b45ff 100644 --- a/receiver/otlpreceiver/factory_test.go +++ b/receiver/otlpreceiver/factory_test.go @@ -155,7 +155,7 @@ func TestCreateMetricReceiver(t *testing.T) { defaultServerConfig := confighttp.NewDefaultServerConfig() defaultServerConfig.Endpoint = testutil.GetAvailableLocalAddress(t) defaultHTTPSettings := &HTTPConfig{ - ServerConfig: &defaultServerConfig, + ServerConfig: &confighttp.ServerConfig{}, TracesURLPath: defaultTracesURLPath, MetricsURLPath: defaultMetricsURLPath, LogsURLPath: defaultLogsURLPath,