From 0e14c22e45fcef9561768753ccf829f03681d11b Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Mon, 22 Jul 2024 01:17:27 -0700 Subject: [PATCH] [confighttp] add confighttp.NewDefaultServerConfig() (#10275) #### Description add a new default method to instantiate the HTTP server config. #### Link to tracking issue #9655 --- .chloggen/newdefaultserverconfig.yaml | 25 ++++++++++++ config/confighttp/confighttp.go | 55 ++++++++++++++++++++++++++- config/confighttp/confighttp_test.go | 22 ++++++++--- receiver/otlpreceiver/factory_test.go | 18 ++++----- 4 files changed, 103 insertions(+), 17 deletions(-) create mode 100644 .chloggen/newdefaultserverconfig.yaml diff --git a/.chloggen/newdefaultserverconfig.yaml b/.chloggen/newdefaultserverconfig.yaml new file mode 100644 index 00000000000..2effa96730e --- /dev/null +++ b/.chloggen/newdefaultserverconfig.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: confighttp + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Add `confighttp.NewDefaultServerConfig()` to instantiate the default HTTP server configuration + +# One or more tracking issues or pull requests related to the change +issues: [9655] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [api] diff --git a/config/confighttp/confighttp.go b/config/confighttp/confighttp.go index f53bedbb008..2a1960e05fd 100644 --- a/config/confighttp/confighttp.go +++ b/config/confighttp/confighttp.go @@ -306,6 +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 `mapstructure:"read_timeout"` + + // 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 `mapstructure:"read_header_timeout"` + + // 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 `mapstructure:"write_timeout"` + + // 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 `mapstructure:"idle_timeout"` +} + +// NewDefaultServerConfig returns ServerConfig type object with default values. +// We encourage to use this function to create an object of ServerConfig. +func NewDefaultServerConfig() 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, + } } type AuthConfig struct { @@ -437,9 +482,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 04bafc5234a..57d82ca63d3 100644 --- a/config/confighttp/confighttp_test.go +++ b/config/confighttp/confighttp_test.go @@ -1009,9 +1009,9 @@ func verifyHeadersResp(t *testing.T, url string, expected map[string]configopaqu } func ExampleServerConfig() { - settings := ServerConfig{ - Endpoint: "localhost:443", - } + settings := NewDefaultServerConfig() + settings.Endpoint = "localhost:443" + s, err := settings.ToServer( context.Background(), componenttest.NewNopHost(), @@ -1283,9 +1283,8 @@ func TestServerWithErrorHandler(t *testing.T) { func TestServerWithDecoder(t *testing.T) { // prepare - hss := ServerConfig{ - Endpoint: "localhost:0", - } + hss := NewDefaultServerConfig() + hss.Endpoint = "localhost:0" decoder := func(body io.ReadCloser) (io.ReadCloser, error) { return body, nil } @@ -1552,3 +1551,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_test.go b/receiver/otlpreceiver/factory_test.go index 4a4477f38c4..ffbb08a1d9d 100644 --- a/receiver/otlpreceiver/factory_test.go +++ b/receiver/otlpreceiver/factory_test.go @@ -58,10 +58,10 @@ func TestCreateTracesReceiver(t *testing.T) { Transport: confignet.TransportTypeTCP, }, } + defaultServerConfig := confighttp.NewDefaultServerConfig() + defaultServerConfig.Endpoint = testutil.GetAvailableLocalAddress(t) defaultHTTPSettings := &HTTPConfig{ - ServerConfig: &confighttp.ServerConfig{ - Endpoint: testutil.GetAvailableLocalAddress(t), - }, + ServerConfig: &defaultServerConfig, TracesURLPath: defaultTracesURLPath, MetricsURLPath: defaultMetricsURLPath, LogsURLPath: defaultLogsURLPath, @@ -152,10 +152,10 @@ func TestCreateMetricReceiver(t *testing.T) { Transport: confignet.TransportTypeTCP, }, } + defaultServerConfig := confighttp.NewDefaultServerConfig() + defaultServerConfig.Endpoint = testutil.GetAvailableLocalAddress(t) defaultHTTPSettings := &HTTPConfig{ - ServerConfig: &confighttp.ServerConfig{ - Endpoint: testutil.GetAvailableLocalAddress(t), - }, + ServerConfig: &defaultServerConfig, TracesURLPath: defaultTracesURLPath, MetricsURLPath: defaultMetricsURLPath, LogsURLPath: defaultLogsURLPath, @@ -246,10 +246,10 @@ func TestCreateLogReceiver(t *testing.T) { Transport: confignet.TransportTypeTCP, }, } + defaultServerConfig := confighttp.NewDefaultServerConfig() + defaultServerConfig.Endpoint = testutil.GetAvailableLocalAddress(t) defaultHTTPSettings := &HTTPConfig{ - ServerConfig: &confighttp.ServerConfig{ - Endpoint: testutil.GetAvailableLocalAddress(t), - }, + ServerConfig: &defaultServerConfig, TracesURLPath: defaultTracesURLPath, MetricsURLPath: defaultMetricsURLPath, LogsURLPath: defaultLogsURLPath,