diff --git a/.chloggen/13252-configgrpc-keepalive-configoptional.yaml b/.chloggen/13252-configgrpc-keepalive-configoptional.yaml new file mode 100644 index 000000000000..c0da4240ca1c --- /dev/null +++ b/.chloggen/13252-configgrpc-keepalive-configoptional.yaml @@ -0,0 +1,28 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: breaking + +# 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: Update optional fields to use `configoptional.Optional` field for optional values. + +# One or more tracking issues or pull requests related to the change +issues: [13252, 13364] + +# (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: | + Specifically, the following fields have been updated to `configoptional`: + - `KeepaliveServerConfig.ServerParameters` (`KeepaliveServerParameters` type) + - `KeepaliveServerConfig.EnforcementPolicy` (`KeepaliveEnforcementPolicy` type) + +# 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] diff --git a/config/configgrpc/configgrpc.go b/config/configgrpc/configgrpc.go index 04b1662e7950..33b6b527024c 100644 --- a/config/configgrpc/configgrpc.go +++ b/config/configgrpc/configgrpc.go @@ -112,10 +112,6 @@ type ClientConfig struct { Middlewares []configmiddleware.Config `mapstructure:"middlewares,omitempty"` } -func ptr[T any](v T) *T { - return &v -} - // NewDefaultClientConfig returns a new instance of ClientConfig with default values. func NewDefaultClientConfig() ClientConfig { return ClientConfig{ @@ -127,8 +123,8 @@ func NewDefaultClientConfig() ClientConfig { // KeepaliveServerConfig is the configuration for keepalive. type KeepaliveServerConfig struct { - ServerParameters *KeepaliveServerParameters `mapstructure:"server_parameters,omitempty"` - EnforcementPolicy *KeepaliveEnforcementPolicy `mapstructure:"enforcement_policy,omitempty"` + ServerParameters configoptional.Optional[KeepaliveServerParameters] `mapstructure:"server_parameters,omitempty"` + EnforcementPolicy configoptional.Optional[KeepaliveEnforcementPolicy] `mapstructure:"enforcement_policy,omitempty"` // prevent unkeyed literal initialization _ struct{} } @@ -136,8 +132,8 @@ type KeepaliveServerConfig struct { // NewDefaultKeepaliveServerConfig returns a new instance of KeepaliveServerConfig with default values. func NewDefaultKeepaliveServerConfig() KeepaliveServerConfig { return KeepaliveServerConfig{ - ServerParameters: ptr(NewDefaultKeepaliveServerParameters()), - EnforcementPolicy: ptr(NewDefaultKeepaliveEnforcementPolicy()), + ServerParameters: configoptional.Some(NewDefaultKeepaliveServerParameters()), + EnforcementPolicy: configoptional.Some(NewDefaultKeepaliveEnforcementPolicy()), } } @@ -487,8 +483,8 @@ func (gss *ServerConfig) getGrpcServerOptions( // https://github.com/grpc/grpc-go/blob/120728e1f775e40a2a764341939b78d666b08260/internal/transport/http2_server.go#L184-L200 if gss.Keepalive.HasValue() { keepaliveConfig := gss.Keepalive.Get() - if keepaliveConfig.ServerParameters != nil { - svrParams := keepaliveConfig.ServerParameters + if keepaliveConfig.ServerParameters.HasValue() { + svrParams := keepaliveConfig.ServerParameters.Get() opts = append(opts, grpc.KeepaliveParams(keepalive.ServerParameters{ MaxConnectionIdle: svrParams.MaxConnectionIdle, MaxConnectionAge: svrParams.MaxConnectionAge, @@ -501,8 +497,8 @@ func (gss *ServerConfig) getGrpcServerOptions( // to apply them over zero/nil values before passing these as grpc.ServerOptions. // The following shows the server code for applying default grpc.ServerOptions. // https://github.com/grpc/grpc-go/blob/120728e1f775e40a2a764341939b78d666b08260/internal/transport/http2_server.go#L202-L205 - if keepaliveConfig.EnforcementPolicy != nil { - enfPol := keepaliveConfig.EnforcementPolicy + if keepaliveConfig.EnforcementPolicy.HasValue() { + enfPol := keepaliveConfig.EnforcementPolicy.Get() opts = append(opts, grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{ MinTime: enfPol.MinTime, PermitWithoutStream: enfPol.PermitWithoutStream, diff --git a/config/configgrpc/configgrpc_test.go b/config/configgrpc/configgrpc_test.go index d11b361d67fd..e80e6c3cbb20 100644 --- a/config/configgrpc/configgrpc_test.go +++ b/config/configgrpc/configgrpc_test.go @@ -90,8 +90,8 @@ func TestNewDefaultKeepaliveEnforcementPolicy(t *testing.T) { func TestNewDefaultKeepaliveServerConfig(t *testing.T) { expected := KeepaliveServerConfig{ - ServerParameters: ptr(NewDefaultKeepaliveServerParameters()), - EnforcementPolicy: ptr(NewDefaultKeepaliveEnforcementPolicy()), + ServerParameters: configoptional.Some(NewDefaultKeepaliveServerParameters()), + EnforcementPolicy: configoptional.Some(NewDefaultKeepaliveEnforcementPolicy()), } result := NewDefaultKeepaliveServerConfig() assert.Equal(t, expected, result) @@ -391,17 +391,17 @@ func TestAllGrpcServerSettingsExceptAuth(t *testing.T) { ReadBufferSize: 1024, WriteBufferSize: 1024, Keepalive: configoptional.Some(KeepaliveServerConfig{ - ServerParameters: &KeepaliveServerParameters{ + ServerParameters: configoptional.Some(KeepaliveServerParameters{ MaxConnectionIdle: time.Second, MaxConnectionAge: time.Second, MaxConnectionAgeGrace: time.Second, Time: time.Second, Timeout: time.Second, - }, - EnforcementPolicy: &KeepaliveEnforcementPolicy{ + }), + EnforcementPolicy: configoptional.Some(KeepaliveEnforcementPolicy{ MinTime: time.Second, PermitWithoutStream: true, - }, + }), }), } opts, err := gss.getGrpcServerOptions(context.Background(), componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings(), []ToServerOption{}) diff --git a/receiver/otlpreceiver/config_test.go b/receiver/otlpreceiver/config_test.go index b11dd2494875..5c8803fafb79 100644 --- a/receiver/otlpreceiver/config_test.go +++ b/receiver/otlpreceiver/config_test.go @@ -122,17 +122,17 @@ func TestUnmarshalConfig(t *testing.T) { ReadBufferSize: 1024, WriteBufferSize: 1024, Keepalive: configoptional.Some(configgrpc.KeepaliveServerConfig{ - ServerParameters: &configgrpc.KeepaliveServerParameters{ + ServerParameters: configoptional.Some(configgrpc.KeepaliveServerParameters{ MaxConnectionIdle: 11 * time.Second, MaxConnectionAge: 12 * time.Second, MaxConnectionAgeGrace: 13 * time.Second, Time: 30 * time.Second, Timeout: 5 * time.Second, - }, - EnforcementPolicy: &configgrpc.KeepaliveEnforcementPolicy{ + }), + EnforcementPolicy: configoptional.Some(configgrpc.KeepaliveEnforcementPolicy{ MinTime: 10 * time.Second, PermitWithoutStream: true, - }, + }), }), }), HTTP: configoptional.Some(HTTPConfig{