Skip to content

Commit 42b0e65

Browse files
AkhigbeEromoTylerHelmuthcodeboten
authored andcommitted
Added default funcs for configgrpc (open-telemetry#9969)
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 <[email protected]> Co-authored-by: Alex Boten <[email protected]>
1 parent 7750f35 commit 42b0e65

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: configgrpc
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Adds `NewDefault*` functions for all the config structs.
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [9654]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]

config/configgrpc/configgrpc.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ type KeepaliveClientConfig struct {
4747
PermitWithoutStream bool `mapstructure:"permit_without_stream"`
4848
}
4949

50+
// NewDefaultKeepaliveClientConfig returns a new instance of KeepaliveClientConfig with default values.
51+
func NewDefaultKeepaliveClientConfig() *KeepaliveClientConfig {
52+
return &KeepaliveClientConfig{
53+
Time: time.Second * 10,
54+
Timeout: time.Second * 10,
55+
}
56+
}
57+
5058
// ClientConfig defines common settings for a gRPC client configuration.
5159
type ClientConfig struct {
5260
// The target to which the exporter is going to send traces or metrics,
@@ -91,12 +99,29 @@ type ClientConfig struct {
9199
Auth *configauth.Authentication `mapstructure:"auth"`
92100
}
93101

102+
// NewDefaultClientConfig returns a new instance of ClientConfig with default values.
103+
func NewDefaultClientConfig() *ClientConfig {
104+
return &ClientConfig{
105+
TLSSetting: configtls.NewDefaultClientConfig(),
106+
Keepalive: NewDefaultKeepaliveClientConfig(),
107+
Auth: configauth.NewDefaultAuthentication(),
108+
}
109+
}
110+
94111
// KeepaliveServerConfig is the configuration for keepalive.
95112
type KeepaliveServerConfig struct {
96113
ServerParameters *KeepaliveServerParameters `mapstructure:"server_parameters"`
97114
EnforcementPolicy *KeepaliveEnforcementPolicy `mapstructure:"enforcement_policy"`
98115
}
99116

117+
// NewDefaultKeepaliveServerConfig returns a new instance of KeepaliveServerConfig with default values.
118+
func NewDefaultKeepaliveServerConfig() *KeepaliveServerConfig {
119+
return &KeepaliveServerConfig{
120+
ServerParameters: NewDefaultKeepaliveServerParameters(),
121+
EnforcementPolicy: NewDefaultKeepaliveEnforcementPolicy(),
122+
}
123+
}
124+
100125
// KeepaliveServerParameters allow configuration of the keepalive.ServerParameters.
101126
// The same default values as keepalive.ServerParameters are applicable and get applied by the server.
102127
// See https://godoc.org/google.golang.org/grpc/keepalive#ServerParameters for details.
@@ -108,6 +133,11 @@ type KeepaliveServerParameters struct {
108133
Timeout time.Duration `mapstructure:"timeout"`
109134
}
110135

136+
// NewDefaultKeepaliveServerParameters creates and returns a new instance of KeepaliveServerParameters with default settings.
137+
func NewDefaultKeepaliveServerParameters() *KeepaliveServerParameters {
138+
return &KeepaliveServerParameters{}
139+
}
140+
111141
// KeepaliveEnforcementPolicy allow configuration of the keepalive.EnforcementPolicy.
112142
// The same default values as keepalive.EnforcementPolicy are applicable and get applied by the server.
113143
// See https://godoc.org/google.golang.org/grpc/keepalive#EnforcementPolicy for details.
@@ -116,6 +146,11 @@ type KeepaliveEnforcementPolicy struct {
116146
PermitWithoutStream bool `mapstructure:"permit_without_stream"`
117147
}
118148

149+
// NewDefaultKeepaliveEnforcementPolicy creates and returns a new instance of KeepaliveEnforcementPolicy with default settings.
150+
func NewDefaultKeepaliveEnforcementPolicy() *KeepaliveEnforcementPolicy {
151+
return &KeepaliveEnforcementPolicy{}
152+
}
153+
119154
// ServerConfig defines common settings for a gRPC server configuration.
120155
type ServerConfig struct {
121156
// Server net.Addr config. For transport only "tcp" and "unix" are valid options.
@@ -151,6 +186,14 @@ type ServerConfig struct {
151186
IncludeMetadata bool `mapstructure:"include_metadata"`
152187
}
153188

189+
// NewDefaultServerConfig returns a new instance of ServerConfig with default values.
190+
func NewDefaultServerConfig() *ServerConfig {
191+
return &ServerConfig{
192+
Keepalive: NewDefaultKeepaliveServerConfig(),
193+
Auth: configauth.NewDefaultAuthentication(),
194+
}
195+
}
196+
154197
// sanitizedEndpoint strips the prefix of either http:// or https:// from configgrpc.ClientConfig.Endpoint.
155198
func (gcs *ClientConfig) sanitizedEndpoint() string {
156199
switch {

config/configgrpc/configgrpc_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,60 @@ import (
3535
"go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp"
3636
)
3737

38+
func TestNewDefaultKeepaliveClientConfig(t *testing.T) {
39+
expectedKeepaliveClientConfig := &KeepaliveClientConfig{
40+
Time: time.Second * 10,
41+
Timeout: time.Second * 10,
42+
}
43+
keepaliveClientConfig := NewDefaultKeepaliveClientConfig()
44+
assert.Equal(t, expectedKeepaliveClientConfig, keepaliveClientConfig)
45+
}
46+
47+
func TestNewDefaultClientConfig(t *testing.T) {
48+
expected := &ClientConfig{
49+
TLSSetting: configtls.NewDefaultClientConfig(),
50+
Keepalive: NewDefaultKeepaliveClientConfig(),
51+
Auth: configauth.NewDefaultAuthentication(),
52+
}
53+
54+
result := NewDefaultClientConfig()
55+
56+
assert.Equal(t, expected, result)
57+
}
58+
func TestNewDefaultKeepaliveServerParameters(t *testing.T) {
59+
expectedParams := &KeepaliveServerParameters{}
60+
params := NewDefaultKeepaliveServerParameters()
61+
62+
assert.Equal(t, expectedParams, params)
63+
}
64+
func TestNewDefaultKeepaliveEnforcementPolicy(t *testing.T) {
65+
expectedPolicy := &KeepaliveEnforcementPolicy{}
66+
67+
policy := NewDefaultKeepaliveEnforcementPolicy()
68+
69+
assert.Equal(t, expectedPolicy, policy)
70+
}
71+
72+
func TestNewDefaultKeepaliveServerConfig(t *testing.T) {
73+
expected := &KeepaliveServerConfig{
74+
ServerParameters: NewDefaultKeepaliveServerParameters(),
75+
EnforcementPolicy: NewDefaultKeepaliveEnforcementPolicy(),
76+
}
77+
result := NewDefaultKeepaliveServerConfig()
78+
assert.Equal(t, expected, result)
79+
}
80+
81+
func TestNewDefaultServerConfig(t *testing.T) {
82+
expected := &ServerConfig{
83+
Keepalive: NewDefaultKeepaliveServerConfig(),
84+
Auth: configauth.NewDefaultAuthentication(),
85+
}
86+
87+
result := NewDefaultServerConfig()
88+
89+
assert.Equal(t, expected, result)
90+
}
91+
3892
// testBalancerBuilder facilitates testing validateBalancerName().
3993
type testBalancerBuilder struct{}
4094

0 commit comments

Comments
 (0)