Skip to content

Commit

Permalink
[confighttp] add confighttp.NewDefaultServerConfig()
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Jun 25, 2024
1 parent 227fb82 commit 55d18be
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 18 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]
7 changes: 7 additions & 0 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@ type ServerConfig struct {
CompressionAlgorithms []string `mapstructure:"compression_algorithms"`
}

// NewDefaultServerConfig returns ServerConfig type object with default values.
// Currently, config options are not added as they are initialized with 'zero value' by GoLang as default.
// We encourage to use this function to create an object of ServerConfig.
func NewDefaultServerConfig() ServerConfig {
return ServerConfig{}
}

// ToListener creates a net.Listener.
func (hss *ServerConfig) ToListener(ctx context.Context) (net.Listener, error) {
listener, err := net.Listen("tcp", hss.Endpoint)
Expand Down
11 changes: 5 additions & 6 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -998,9 +998,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 @@ -1266,9 +1266,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
6 changes: 3 additions & 3 deletions receiver/otlpreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ 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{
Expand All @@ -50,9 +52,7 @@ func createDefaultConfig() component.Config {
ReadBufferSize: 512 * 1024,
},
HTTP: &HTTPConfig{
ServerConfig: &confighttp.ServerConfig{
Endpoint: localhostgate.EndpointForPort(httpPort),
},
ServerConfig: &serverConfig,
TracesURLPath: defaultTracesURLPath,
MetricsURLPath: defaultMetricsURLPath,
LogsURLPath: defaultLogsURLPath,
Expand Down
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 55d18be

Please sign in to comment.