Skip to content

Commit

Permalink
[confighttp] add confighttp.NewDefaultServerConfig() (open-telemetry#…
Browse files Browse the repository at this point in the history
…10275)

#### Description
add a new default method to instantiate the HTTP server config.

<!-- Issue number if applicable -->
#### Link to tracking issue
open-telemetry#9655
  • Loading branch information
atoulme authored Jul 22, 2024
1 parent 7d5b1ba commit 0e14c22
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 17 deletions.
25 changes: 25 additions & 0 deletions .chloggen/newdefaultserverconfig.yaml
Original file line number Diff line number Diff line change
@@ -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]
55 changes: 53 additions & 2 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 16 additions & 6 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
18 changes: 9 additions & 9 deletions receiver/otlpreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 0e14c22

Please sign in to comment.