Skip to content

Commit

Permalink
Added default funcs for configgrpc (open-telemetry#9969)
Browse files Browse the repository at this point in the history
Description:
Added newDefault methods for structs in configgrpc package

Closes open-telemetry#9654

Testing: Tests were added for the NewDefault functions

---------

Co-authored-by: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com>
Co-authored-by: Alex Boten <223565+codeboten@users.noreply.github.com>
  • Loading branch information
3 people authored and andrzej-stencel committed May 27, 2024
1 parent 7750f35 commit 42b0e65
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .chloggen/configgrpc-add-newdefault-funcs.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: configgrpc

# 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: [9654]

# (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]
43 changes: 43 additions & 0 deletions config/configgrpc/configgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ type KeepaliveClientConfig struct {
PermitWithoutStream bool `mapstructure:"permit_without_stream"`
}

// NewDefaultKeepaliveClientConfig returns a new instance of KeepaliveClientConfig with default values.
func NewDefaultKeepaliveClientConfig() *KeepaliveClientConfig {
return &KeepaliveClientConfig{
Time: time.Second * 10,
Timeout: time.Second * 10,
}
}

// ClientConfig defines common settings for a gRPC client configuration.
type ClientConfig struct {
// The target to which the exporter is going to send traces or metrics,
Expand Down Expand Up @@ -91,12 +99,29 @@ type ClientConfig struct {
Auth *configauth.Authentication `mapstructure:"auth"`
}

// NewDefaultClientConfig returns a new instance of ClientConfig with default values.
func NewDefaultClientConfig() *ClientConfig {
return &ClientConfig{
TLSSetting: configtls.NewDefaultClientConfig(),
Keepalive: NewDefaultKeepaliveClientConfig(),
Auth: configauth.NewDefaultAuthentication(),
}
}

// KeepaliveServerConfig is the configuration for keepalive.
type KeepaliveServerConfig struct {
ServerParameters *KeepaliveServerParameters `mapstructure:"server_parameters"`
EnforcementPolicy *KeepaliveEnforcementPolicy `mapstructure:"enforcement_policy"`
}

// NewDefaultKeepaliveServerConfig returns a new instance of KeepaliveServerConfig with default values.
func NewDefaultKeepaliveServerConfig() *KeepaliveServerConfig {
return &KeepaliveServerConfig{
ServerParameters: NewDefaultKeepaliveServerParameters(),
EnforcementPolicy: NewDefaultKeepaliveEnforcementPolicy(),
}
}

// KeepaliveServerParameters allow configuration of the keepalive.ServerParameters.
// The same default values as keepalive.ServerParameters are applicable and get applied by the server.
// See https://godoc.org/google.golang.org/grpc/keepalive#ServerParameters for details.
Expand All @@ -108,6 +133,11 @@ type KeepaliveServerParameters struct {
Timeout time.Duration `mapstructure:"timeout"`
}

// NewDefaultKeepaliveServerParameters creates and returns a new instance of KeepaliveServerParameters with default settings.
func NewDefaultKeepaliveServerParameters() *KeepaliveServerParameters {
return &KeepaliveServerParameters{}
}

// KeepaliveEnforcementPolicy allow configuration of the keepalive.EnforcementPolicy.
// The same default values as keepalive.EnforcementPolicy are applicable and get applied by the server.
// See https://godoc.org/google.golang.org/grpc/keepalive#EnforcementPolicy for details.
Expand All @@ -116,6 +146,11 @@ type KeepaliveEnforcementPolicy struct {
PermitWithoutStream bool `mapstructure:"permit_without_stream"`
}

// NewDefaultKeepaliveEnforcementPolicy creates and returns a new instance of KeepaliveEnforcementPolicy with default settings.
func NewDefaultKeepaliveEnforcementPolicy() *KeepaliveEnforcementPolicy {
return &KeepaliveEnforcementPolicy{}
}

// ServerConfig defines common settings for a gRPC server configuration.
type ServerConfig struct {
// Server net.Addr config. For transport only "tcp" and "unix" are valid options.
Expand Down Expand Up @@ -151,6 +186,14 @@ type ServerConfig struct {
IncludeMetadata bool `mapstructure:"include_metadata"`
}

// NewDefaultServerConfig returns a new instance of ServerConfig with default values.
func NewDefaultServerConfig() *ServerConfig {
return &ServerConfig{
Keepalive: NewDefaultKeepaliveServerConfig(),
Auth: configauth.NewDefaultAuthentication(),
}
}

// sanitizedEndpoint strips the prefix of either http:// or https:// from configgrpc.ClientConfig.Endpoint.
func (gcs *ClientConfig) sanitizedEndpoint() string {
switch {
Expand Down
54 changes: 54 additions & 0 deletions config/configgrpc/configgrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,60 @@ import (
"go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp"
)

func TestNewDefaultKeepaliveClientConfig(t *testing.T) {
expectedKeepaliveClientConfig := &KeepaliveClientConfig{
Time: time.Second * 10,
Timeout: time.Second * 10,
}
keepaliveClientConfig := NewDefaultKeepaliveClientConfig()
assert.Equal(t, expectedKeepaliveClientConfig, keepaliveClientConfig)
}

func TestNewDefaultClientConfig(t *testing.T) {
expected := &ClientConfig{
TLSSetting: configtls.NewDefaultClientConfig(),
Keepalive: NewDefaultKeepaliveClientConfig(),
Auth: configauth.NewDefaultAuthentication(),
}

result := NewDefaultClientConfig()

assert.Equal(t, expected, result)
}
func TestNewDefaultKeepaliveServerParameters(t *testing.T) {
expectedParams := &KeepaliveServerParameters{}
params := NewDefaultKeepaliveServerParameters()

assert.Equal(t, expectedParams, params)
}
func TestNewDefaultKeepaliveEnforcementPolicy(t *testing.T) {
expectedPolicy := &KeepaliveEnforcementPolicy{}

policy := NewDefaultKeepaliveEnforcementPolicy()

assert.Equal(t, expectedPolicy, policy)
}

func TestNewDefaultKeepaliveServerConfig(t *testing.T) {
expected := &KeepaliveServerConfig{
ServerParameters: NewDefaultKeepaliveServerParameters(),
EnforcementPolicy: NewDefaultKeepaliveEnforcementPolicy(),
}
result := NewDefaultKeepaliveServerConfig()
assert.Equal(t, expected, result)
}

func TestNewDefaultServerConfig(t *testing.T) {
expected := &ServerConfig{
Keepalive: NewDefaultKeepaliveServerConfig(),
Auth: configauth.NewDefaultAuthentication(),
}

result := NewDefaultServerConfig()

assert.Equal(t, expected, result)
}

// testBalancerBuilder facilitates testing validateBalancerName().
type testBalancerBuilder struct{}

Expand Down

0 comments on commit 42b0e65

Please sign in to comment.