Skip to content
Closed
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
25 changes: 25 additions & 0 deletions .chloggen/confighttp-add-newdefault-func.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: Adds `NewDefault*` functions for all the config structs.

# 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]
15 changes: 15 additions & 0 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ type ClientConfig struct {
// We encourage to use this function to create an object of ClientConfig.
func NewDefaultClientConfig() ClientConfig {
// The default values are taken from the values of 'DefaultTransport' of 'http' package.

maxIdleConns := 100
idleConnTimeout := 90 * time.Second

return ClientConfig{
Headers: make(map[string]configopaque.String),
MaxIdleConns: &maxIdleConns,
IdleConnTimeout: &idleConnTimeout,
}
Expand Down Expand Up @@ -286,6 +288,14 @@ type ServerConfig struct {
ResponseHeaders map[string]configopaque.String `mapstructure:"response_headers"`
}

// NewDefaultServerConfig creates new ServerConfig with default values set
func NewDefaultServerConfig() ServerConfig {

return ServerConfig{
ResponseHeaders: make(map[string]configopaque.String),
}
}

// Deprecated: [v0.99.0] Use ToListener instead.
func (hss *ServerConfig) ToListenerContext(ctx context.Context) (net.Listener, error) {
return hss.ToListener(ctx)
Expand Down Expand Up @@ -447,6 +457,11 @@ type CORSConfig struct {
MaxAge int `mapstructure:"max_age"`
}

// NewDefaultCORSConfig creates a new CORSConfig with defaults value set
func NewDefaultCORSConfig() CORSConfig {
return CORSConfig{}
}

func authInterceptor(next http.Handler, server auth.Server) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx, err := server.Authenticate(r.Context(), r.Header)
Expand Down
15 changes: 15 additions & 0 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ var (
nonExistingID = component.MustNewID("nonexisting")
)

func TestNewDefaultServerConfig(t *testing.T) {
expectedServerConfig := ServerConfig{
ResponseHeaders: map[string]configopaque.String{},
}
serverConfig := NewDefaultServerConfig()
require.Equal(t, expectedServerConfig, serverConfig)
}

func TestNewDefaultCORSConfig(t *testing.T) {
expectedCORSConfig := CORSConfig{}
corsConfig := NewDefaultCORSConfig()
require.Equal(t, expectedCORSConfig, corsConfig)
}

func TestAllHTTPClientSettings(t *testing.T) {
host := &mockHost{
ext: map[component.ID]component.Component{
Expand Down Expand Up @@ -239,6 +253,7 @@ func TestPartialHTTPClientSettings(t *testing.T) {

func TestDefaultHTTPClientSettings(t *testing.T) {
httpClientSettings := NewDefaultClientConfig()
assert.EqualValues(t, map[string]configopaque.String{}, httpClientSettings.Headers)
assert.EqualValues(t, 100, *httpClientSettings.MaxIdleConns)
assert.EqualValues(t, 90*time.Second, *httpClientSettings.IdleConnTimeout)
}
Expand Down