diff --git a/.chloggen/jackgopack4_configgrpc-configoptional.yaml b/.chloggen/jackgopack4_configgrpc-configoptional.yaml new file mode 100644 index 00000000000..8719e33f413 --- /dev/null +++ b/.chloggen/jackgopack4_configgrpc-configoptional.yaml @@ -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: 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: [13250, 13252] + +# (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: Components using `configgrpc` package may need to update config values. + +# 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/client_middleware_test.go b/config/configgrpc/client_middleware_test.go index 4bd3dcf8cfc..846dcc18581 100644 --- a/config/configgrpc/client_middleware_test.go +++ b/config/configgrpc/client_middleware_test.go @@ -18,6 +18,7 @@ import ( "go.opentelemetry.io/collector/component/componenttest" "go.opentelemetry.io/collector/config/configmiddleware" "go.opentelemetry.io/collector/config/confignet" + "go.opentelemetry.io/collector/config/configoptional" "go.opentelemetry.io/collector/config/configtls" "go.opentelemetry.io/collector/extension" "go.opentelemetry.io/collector/extension/extensionmiddleware" @@ -103,12 +104,12 @@ func TestClientMiddlewareOrdering(t *testing.T) { // Start a gRPC server that will record the incoming metadata server := &grpcTraceServer{} - srv, addr := server.startTestServer(t, ServerConfig{ + srv, addr := server.startTestServer(t, configoptional.Some(ServerConfig{ NetAddr: confignet.AddrConfig{ Endpoint: "localhost:0", Transport: confignet.TransportTypeTCP, }, - }) + })) defer srv.Stop() // Create client config with middleware extensions diff --git a/config/configgrpc/configgrpc.go b/config/configgrpc/configgrpc.go index c55cbe2a87e..04b1662e795 100644 --- a/config/configgrpc/configgrpc.go +++ b/config/configgrpc/configgrpc.go @@ -34,6 +34,7 @@ import ( "go.opentelemetry.io/collector/config/configmiddleware" "go.opentelemetry.io/collector/config/confignet" "go.opentelemetry.io/collector/config/configopaque" + "go.opentelemetry.io/collector/config/configoptional" "go.opentelemetry.io/collector/config/configtls" "go.opentelemetry.io/collector/extension/extensionauth" ) @@ -79,7 +80,7 @@ type ClientConfig struct { // The keepalive parameters for gRPC client. See grpc.WithKeepaliveParams. // (https://godoc.org/google.golang.org/grpc#WithKeepaliveParams). - Keepalive *KeepaliveClientConfig `mapstructure:"keepalive,omitempty"` + Keepalive configoptional.Optional[KeepaliveClientConfig] `mapstructure:"keepalive,omitempty"` // ReadBufferSize for gRPC client. See grpc.WithReadBufferSize. // (https://godoc.org/google.golang.org/grpc#WithReadBufferSize). @@ -105,7 +106,7 @@ type ClientConfig struct { Authority string `mapstructure:"authority,omitempty"` // Auth configuration for outgoing RPCs. - Auth *configauth.Config `mapstructure:"auth,omitempty"` + Auth configoptional.Optional[configauth.Config] `mapstructure:"auth,omitempty"` // Middlewares for the gRPC client. Middlewares []configmiddleware.Config `mapstructure:"middlewares,omitempty"` @@ -119,7 +120,7 @@ func ptr[T any](v T) *T { func NewDefaultClientConfig() ClientConfig { return ClientConfig{ TLS: configtls.NewDefaultClientConfig(), - Keepalive: ptr(NewDefaultKeepaliveClientConfig()), + Keepalive: configoptional.Some(NewDefaultKeepaliveClientConfig()), BalancerName: BalancerName(), } } @@ -180,7 +181,7 @@ type ServerConfig struct { // Configures the protocol to use TLS. // The default value is nil, which will cause the protocol to not use TLS. - TLS *configtls.ServerConfig `mapstructure:"tls,omitempty"` + TLS configoptional.Optional[configtls.ServerConfig] `mapstructure:"tls,omitempty"` // MaxRecvMsgSizeMiB sets the maximum size (in MiB) of messages accepted by the server. MaxRecvMsgSizeMiB int `mapstructure:"max_recv_msg_size_mib,omitempty"` @@ -198,10 +199,10 @@ type ServerConfig struct { WriteBufferSize int `mapstructure:"write_buffer_size,omitempty"` // Keepalive anchor for all the settings related to keepalive. - Keepalive *KeepaliveServerConfig `mapstructure:"keepalive,omitempty"` + Keepalive configoptional.Optional[KeepaliveServerConfig] `mapstructure:"keepalive,omitempty"` // Auth for this receiver - Auth *configauth.Config `mapstructure:"auth,omitempty"` + Auth configoptional.Optional[configauth.Config] `mapstructure:"auth,omitempty"` // Include propagates the incoming connection's metadata to downstream consumers. IncludeMetadata bool `mapstructure:"include_metadata,omitempty"` @@ -216,7 +217,7 @@ type ServerConfig struct { // NewDefaultServerConfig returns a new instance of ServerConfig with default values. func NewDefaultServerConfig() ServerConfig { return ServerConfig{ - Keepalive: ptr(NewDefaultKeepaliveServerConfig()), + Keepalive: configoptional.Some(NewDefaultKeepaliveServerConfig()), } } @@ -330,21 +331,22 @@ func (gcs *ClientConfig) getGrpcDialOptions( opts = append(opts, grpc.WithWriteBufferSize(gcs.WriteBufferSize)) } - if gcs.Keepalive != nil { + if gcs.Keepalive.HasValue() { + keepaliveConfig := gcs.Keepalive.Get() keepAliveOption := grpc.WithKeepaliveParams(keepalive.ClientParameters{ - Time: gcs.Keepalive.Time, - Timeout: gcs.Keepalive.Timeout, - PermitWithoutStream: gcs.Keepalive.PermitWithoutStream, + Time: keepaliveConfig.Time, + Timeout: keepaliveConfig.Timeout, + PermitWithoutStream: keepaliveConfig.PermitWithoutStream, }) opts = append(opts, keepAliveOption) } - if gcs.Auth != nil { + if gcs.Auth.HasValue() { if host.GetExtensions() == nil { return nil, errors.New("no extensions configuration available") } - grpcAuthenticator, cerr := gcs.Auth.GetGRPCClientAuthenticator(ctx, host.GetExtensions()) + grpcAuthenticator, cerr := gcs.Auth.Get().GetGRPCClientAuthenticator(ctx, host.GetExtensions()) if cerr != nil { return nil, cerr } @@ -455,8 +457,8 @@ func (gss *ServerConfig) getGrpcServerOptions( ) ([]grpc.ServerOption, error) { var opts []grpc.ServerOption - if gss.TLS != nil { - tlsCfg, err := gss.TLS.LoadTLSConfig(context.Background()) + if gss.TLS.HasValue() { + tlsCfg, err := gss.TLS.Get().LoadTLSConfig(context.Background()) if err != nil { return nil, err } @@ -483,9 +485,10 @@ 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#L184-L200 - if gss.Keepalive != nil { - if gss.Keepalive.ServerParameters != nil { - svrParams := gss.Keepalive.ServerParameters + if gss.Keepalive.HasValue() { + keepaliveConfig := gss.Keepalive.Get() + if keepaliveConfig.ServerParameters != nil { + svrParams := keepaliveConfig.ServerParameters opts = append(opts, grpc.KeepaliveParams(keepalive.ServerParameters{ MaxConnectionIdle: svrParams.MaxConnectionIdle, MaxConnectionAge: svrParams.MaxConnectionAge, @@ -498,8 +501,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 gss.Keepalive.EnforcementPolicy != nil { - enfPol := gss.Keepalive.EnforcementPolicy + if keepaliveConfig.EnforcementPolicy != nil { + enfPol := keepaliveConfig.EnforcementPolicy opts = append(opts, grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{ MinTime: enfPol.MinTime, PermitWithoutStream: enfPol.PermitWithoutStream, @@ -510,8 +513,8 @@ func (gss *ServerConfig) getGrpcServerOptions( var uInterceptors []grpc.UnaryServerInterceptor var sInterceptors []grpc.StreamServerInterceptor - if gss.Auth != nil { - authenticator, err := gss.Auth.GetServerAuthenticator(context.Background(), host.GetExtensions()) + if gss.Auth.HasValue() { + authenticator, err := gss.Auth.Get().GetServerAuthenticator(context.Background(), host.GetExtensions()) if err != nil { return nil, err } diff --git a/config/configgrpc/configgrpc_test.go b/config/configgrpc/configgrpc_test.go index 59ead833b1b..d11b361d67f 100644 --- a/config/configgrpc/configgrpc_test.go +++ b/config/configgrpc/configgrpc_test.go @@ -28,6 +28,7 @@ import ( "go.opentelemetry.io/collector/config/configcompression" "go.opentelemetry.io/collector/config/confignet" "go.opentelemetry.io/collector/config/configopaque" + "go.opentelemetry.io/collector/config/configoptional" "go.opentelemetry.io/collector/config/configtls" "go.opentelemetry.io/collector/extension" "go.opentelemetry.io/collector/extension/extensionauth" @@ -63,7 +64,7 @@ func TestNewDefaultClientConfig(t *testing.T) { keepalive := NewDefaultKeepaliveClientConfig() expected := ClientConfig{ TLS: configtls.NewDefaultClientConfig(), - Keepalive: &keepalive, + Keepalive: configoptional.Some(keepalive), BalancerName: BalancerName(), } @@ -98,7 +99,7 @@ func TestNewDefaultKeepaliveServerConfig(t *testing.T) { func TestNewDefaultServerConfig(t *testing.T) { expected := ServerConfig{ - Keepalive: ptr(NewDefaultKeepaliveServerConfig()), + Keepalive: configoptional.Some(NewDefaultKeepaliveServerConfig()), } result := NewDefaultServerConfig() @@ -167,17 +168,17 @@ func TestAllGrpcClientSettings(t *testing.T) { TLS: configtls.ClientConfig{ Insecure: false, }, - Keepalive: &KeepaliveClientConfig{ + Keepalive: configoptional.Some(KeepaliveClientConfig{ Time: time.Second, Timeout: time.Second, PermitWithoutStream: true, - }, + }), ReadBufferSize: 1024, WriteBufferSize: 1024, WaitForReady: true, BalancerName: "round_robin", Authority: "pseudo-authority", - Auth: &configauth.Config{AuthenticatorID: testAuthID}, + Auth: configoptional.Some(configauth.Config{AuthenticatorID: testAuthID}), }, host: &mockHost{ ext: map[component.ID]component.Component{ @@ -196,17 +197,17 @@ func TestAllGrpcClientSettings(t *testing.T) { TLS: configtls.ClientConfig{ Insecure: false, }, - Keepalive: &KeepaliveClientConfig{ + Keepalive: configoptional.Some(KeepaliveClientConfig{ Time: time.Second, Timeout: time.Second, PermitWithoutStream: true, - }, + }), ReadBufferSize: 1024, WriteBufferSize: 1024, WaitForReady: true, BalancerName: "round_robin", Authority: "pseudo-authority", - Auth: &configauth.Config{AuthenticatorID: testAuthID}, + Auth: configoptional.Some(configauth.Config{AuthenticatorID: testAuthID}), }, host: &mockHost{ ext: map[component.ID]component.Component{ @@ -225,17 +226,17 @@ func TestAllGrpcClientSettings(t *testing.T) { TLS: configtls.ClientConfig{ Insecure: false, }, - Keepalive: &KeepaliveClientConfig{ + Keepalive: configoptional.Some(KeepaliveClientConfig{ Time: time.Second, Timeout: time.Second, PermitWithoutStream: true, - }, + }), ReadBufferSize: 1024, WriteBufferSize: 1024, WaitForReady: true, BalancerName: "round_robin", Authority: "pseudo-authority", - Auth: &configauth.Config{AuthenticatorID: testAuthID}, + Auth: configoptional.Some(configauth.Config{AuthenticatorID: testAuthID}), }, host: &mockHost{ ext: map[component.ID]component.Component{ @@ -267,12 +268,12 @@ func TestAllGrpcClientSettings(t *testing.T) { func TestHeaders(t *testing.T) { traceServer := &grpcTraceServer{} - server, addr := traceServer.startTestServer(t, ServerConfig{ + server, addr := traceServer.startTestServer(t, configoptional.Some(ServerConfig{ NetAddr: confignet.AddrConfig{ Endpoint: "localhost:0", Transport: confignet.TransportTypeTCP, }, - }) + })) defer server.Stop() // Create client and send request to server with headers @@ -381,15 +382,15 @@ func TestAllGrpcServerSettingsExceptAuth(t *testing.T) { Endpoint: "localhost:1234", Transport: confignet.TransportTypeTCP, }, - TLS: &configtls.ServerConfig{ + TLS: configoptional.Some(configtls.ServerConfig{ Config: configtls.Config{}, ClientCAFile: "", - }, + }), MaxRecvMsgSizeMiB: 1, MaxConcurrentStreams: 1024, ReadBufferSize: 1024, WriteBufferSize: 1024, - Keepalive: &KeepaliveServerConfig{ + Keepalive: configoptional.Some(KeepaliveServerConfig{ ServerParameters: &KeepaliveServerParameters{ MaxConnectionIdle: time.Second, MaxConnectionAge: time.Second, @@ -401,7 +402,7 @@ func TestAllGrpcServerSettingsExceptAuth(t *testing.T) { MinTime: time.Second, PermitWithoutStream: true, }, - }, + }), } opts, err := gss.getGrpcServerOptions(context.Background(), componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings(), []ToServerOption{}) require.NoError(t, err) @@ -414,9 +415,9 @@ func TestGrpcServerAuthSettings(t *testing.T) { Endpoint: "0.0.0.0:1234", }, } - gss.Auth = &configauth.Config{ + gss.Auth = configoptional.Some(configauth.Config{ AuthenticatorID: mockID, - } + }) host := &mockHost{ ext: map[component.ID]component.Component{ @@ -438,11 +439,11 @@ func TestGrpcClientConfigInvalidBalancer(t *testing.T) { TLS: configtls.ClientConfig{ Insecure: false, }, - Keepalive: &KeepaliveClientConfig{ + Keepalive: configoptional.Some(KeepaliveClientConfig{ Time: time.Second, Timeout: time.Second, PermitWithoutStream: true, - }, + }), ReadBufferSize: 1024, WriteBufferSize: 1024, WaitForReady: true, @@ -470,7 +471,6 @@ func TestGRPCClientSettingsError(t *testing.T) { Insecure: false, ServerName: "", }, - Keepalive: nil, }, }, { @@ -486,14 +486,13 @@ func TestGRPCClientSettingsError(t *testing.T) { Insecure: false, ServerName: "", }, - Keepalive: nil, }, }, { err: "failed to resolve authenticator \"doesntexist\": authenticator not found", settings: ClientConfig{ Endpoint: "localhost:1234", - Auth: &configauth.Config{AuthenticatorID: doesntExistID}, + Auth: configoptional.Some(configauth.Config{AuthenticatorID: doesntExistID}), }, host: &mockHost{ext: map[component.ID]component.Component{}}, }, @@ -501,7 +500,7 @@ func TestGRPCClientSettingsError(t *testing.T) { err: "no extensions configuration available", settings: ClientConfig{ Endpoint: "localhost:1234", - Auth: &configauth.Config{AuthenticatorID: doesntExistID}, + Auth: configoptional.Some(configauth.Config{AuthenticatorID: doesntExistID}), }, host: &mockHost{}, }, @@ -555,7 +554,6 @@ func TestUseSecure(t *testing.T) { Endpoint: "", Compression: "", TLS: configtls.ClientConfig{}, - Keepalive: nil, } dialOpts, err := gcs.getGrpcDialOptions(context.Background(), componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings(), []ToClientConnOption{}) require.NoError(t, err) @@ -574,11 +572,11 @@ func TestGRPCServerSettingsError(t *testing.T) { Endpoint: "127.0.0.1:1234", Transport: confignet.TransportTypeTCP, }, - TLS: &configtls.ServerConfig{ + TLS: configoptional.Some(configtls.ServerConfig{ Config: configtls.Config{ CAFile: "/doesnt/exist", }, - }, + }), }, }, { @@ -588,11 +586,11 @@ func TestGRPCServerSettingsError(t *testing.T) { Endpoint: "127.0.0.1:1234", Transport: confignet.TransportTypeTCP, }, - TLS: &configtls.ServerConfig{ + TLS: configoptional.Some(configtls.ServerConfig{ Config: configtls.Config{ CertFile: "/doesnt/exist", }, - }, + }), }, }, { @@ -602,9 +600,9 @@ func TestGRPCServerSettingsError(t *testing.T) { Endpoint: "127.0.0.1:1234", Transport: confignet.TransportTypeTCP, }, - TLS: &configtls.ServerConfig{ + TLS: configoptional.Some(configtls.ServerConfig{ ClientCAFile: "/doesnt/exist", - }, + }), }, }, } @@ -630,103 +628,103 @@ func TestGRPCServerSettings_ToListener_Error(t *testing.T) { func TestHttpReception(t *testing.T) { tests := []struct { name string - tlsServerCreds *configtls.ServerConfig - tlsClientCreds *configtls.ClientConfig + tlsServerCreds configoptional.Optional[configtls.ServerConfig] + tlsClientCreds configoptional.Optional[configtls.ClientConfig] hasError bool }{ { name: "noTLS", - tlsServerCreds: nil, - tlsClientCreds: &configtls.ClientConfig{ + tlsServerCreds: configoptional.None[configtls.ServerConfig](), + tlsClientCreds: configoptional.Some(configtls.ClientConfig{ Insecure: true, - }, + }), }, { name: "TLS", - tlsServerCreds: &configtls.ServerConfig{ + tlsServerCreds: configoptional.Some(configtls.ServerConfig{ Config: configtls.Config{ CAFile: filepath.Join("testdata", "ca.crt"), CertFile: filepath.Join("testdata", "server.crt"), KeyFile: filepath.Join("testdata", "server.key"), }, - }, - tlsClientCreds: &configtls.ClientConfig{ + }), + tlsClientCreds: configoptional.Some(configtls.ClientConfig{ Config: configtls.Config{ CAFile: filepath.Join("testdata", "ca.crt"), }, ServerName: "localhost", - }, + }), }, { name: "NoServerCertificates", - tlsServerCreds: &configtls.ServerConfig{ + tlsServerCreds: configoptional.Some(configtls.ServerConfig{ Config: configtls.Config{ CAFile: filepath.Join("testdata", "ca.crt"), }, - }, - tlsClientCreds: &configtls.ClientConfig{ + }), + tlsClientCreds: configoptional.Some(configtls.ClientConfig{ Config: configtls.Config{ CAFile: filepath.Join("testdata", "ca.crt"), }, ServerName: "localhost", - }, + }), hasError: true, }, { name: "mTLS", - tlsServerCreds: &configtls.ServerConfig{ + tlsServerCreds: configoptional.Some(configtls.ServerConfig{ Config: configtls.Config{ CAFile: filepath.Join("testdata", "ca.crt"), CertFile: filepath.Join("testdata", "server.crt"), KeyFile: filepath.Join("testdata", "server.key"), }, ClientCAFile: filepath.Join("testdata", "ca.crt"), - }, - tlsClientCreds: &configtls.ClientConfig{ + }), + tlsClientCreds: configoptional.Some(configtls.ClientConfig{ Config: configtls.Config{ CAFile: filepath.Join("testdata", "ca.crt"), CertFile: filepath.Join("testdata", "client.crt"), KeyFile: filepath.Join("testdata", "client.key"), }, ServerName: "localhost", - }, + }), }, { name: "NoClientCertificate", - tlsServerCreds: &configtls.ServerConfig{ + tlsServerCreds: configoptional.Some(configtls.ServerConfig{ Config: configtls.Config{ CAFile: filepath.Join("testdata", "ca.crt"), CertFile: filepath.Join("testdata", "server.crt"), KeyFile: filepath.Join("testdata", "server.key"), }, ClientCAFile: filepath.Join("testdata", "ca.crt"), - }, - tlsClientCreds: &configtls.ClientConfig{ + }), + tlsClientCreds: configoptional.Some(configtls.ClientConfig{ Config: configtls.Config{ CAFile: filepath.Join("testdata", "ca.crt"), }, ServerName: "localhost", - }, + }), hasError: true, }, { name: "WrongClientCA", - tlsServerCreds: &configtls.ServerConfig{ + tlsServerCreds: configoptional.Some(configtls.ServerConfig{ Config: configtls.Config{ CAFile: filepath.Join("testdata", "ca.crt"), CertFile: filepath.Join("testdata", "server.crt"), KeyFile: filepath.Join("testdata", "server.key"), }, ClientCAFile: filepath.Join("testdata", "server.crt"), - }, - tlsClientCreds: &configtls.ClientConfig{ + }), + tlsClientCreds: configoptional.Some(configtls.ClientConfig{ Config: configtls.Config{ CAFile: filepath.Join("testdata", "ca.crt"), CertFile: filepath.Join("testdata", "client.crt"), KeyFile: filepath.Join("testdata", "client.key"), }, ServerName: "localhost", - }, + }), hasError: true, }, } @@ -734,18 +732,18 @@ func TestHttpReception(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - s, addr := (&grpcTraceServer{}).startTestServer(t, ServerConfig{ + s, addr := (&grpcTraceServer{}).startTestServer(t, configoptional.Some(ServerConfig{ NetAddr: confignet.AddrConfig{ Endpoint: "localhost:0", Transport: confignet.TransportTypeTCP, }, TLS: test.tlsServerCreds, - }) + })) defer s.Stop() resp, errResp := sendTestRequest(t, ClientConfig{ Endpoint: addr, - TLS: *test.tlsClientCreds, + TLS: *test.tlsClientCreds.Get(), }) if test.hasError { require.Error(t, errResp) @@ -763,12 +761,12 @@ func TestReceiveOnUnixDomainSocket(t *testing.T) { } socketName := tempSocketName(t) - srv, addr := (&grpcTraceServer{}).startTestServer(t, ServerConfig{ + srv, addr := (&grpcTraceServer{}).startTestServer(t, configoptional.Some(ServerConfig{ NetAddr: confignet.AddrConfig{ Endpoint: socketName, Transport: confignet.TransportTypeUnix, }, - }) + })) defer srv.Stop() resp, errResp := sendTestRequest(t, ClientConfig{ @@ -943,12 +941,12 @@ func TestClientInfoInterceptors(t *testing.T) { // prepare the server { var srv *grpc.Server - srv, addr = mock.startTestServer(t, ServerConfig{ + srv, addr = mock.startTestServer(t, configoptional.Some(ServerConfig{ NetAddr: confignet.AddrConfig{ Endpoint: "localhost:0", Transport: confignet.TransportTypeTCP, }, - }) + })) defer srv.Stop() } @@ -1145,14 +1143,14 @@ func (gts *grpcTraceServer) Export(ctx context.Context, _ ptraceotlp.ExportReque return ptraceotlp.NewExportResponse(), nil } -func (gts *grpcTraceServer) startTestServer(t *testing.T, gss ServerConfig) (*grpc.Server, string) { +func (gts *grpcTraceServer) startTestServer(t *testing.T, gss configoptional.Optional[ServerConfig]) (*grpc.Server, string) { return gts.startTestServerWithHost(t, gss, componenttest.NewNopHost()) } -func (gts *grpcTraceServer) startTestServerWithHost(t *testing.T, gss ServerConfig, host component.Host, opts ...ToServerOption) (*grpc.Server, string) { - listener, err := gss.NetAddr.Listen(context.Background()) +func (gts *grpcTraceServer) startTestServerWithHost(t *testing.T, gss configoptional.Optional[ServerConfig], host component.Host, opts ...ToServerOption) (*grpc.Server, string) { + listener, err := gss.Get().NetAddr.Listen(context.Background()) require.NoError(t, err) - server, err := gss.ToServer(context.Background(), host, componenttest.NewNopTelemetrySettings(), opts...) + server, err := gss.Get().ToServer(context.Background(), host, componenttest.NewNopTelemetrySettings(), opts...) require.NoError(t, err) ptraceotlp.RegisterGRPCServer(server, gts) go func() { diff --git a/config/configgrpc/go.mod b/config/configgrpc/go.mod index 6da2472e0f8..63a4f83afef 100644 --- a/config/configgrpc/go.mod +++ b/config/configgrpc/go.mod @@ -13,6 +13,7 @@ require ( go.opentelemetry.io/collector/config/configmiddleware v0.129.0 go.opentelemetry.io/collector/config/confignet v1.35.0 go.opentelemetry.io/collector/config/configopaque v1.35.0 + go.opentelemetry.io/collector/config/configoptional v0.129.0 go.opentelemetry.io/collector/config/configtls v1.35.0 go.opentelemetry.io/collector/extension v1.35.0 go.opentelemetry.io/collector/extension/extensionauth v1.35.0 @@ -33,6 +34,8 @@ require ( github.com/fsnotify/fsnotify v1.9.0 // indirect github.com/go-logr/logr v1.4.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect + github.com/go-viper/mapstructure/v2 v2.3.0 // indirect + github.com/gobwas/glob v0.2.3 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/google/go-tpm v0.9.5 // indirect @@ -40,10 +43,16 @@ require ( github.com/hashicorp/go-version v1.7.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.17.9 // indirect + github.com/knadh/koanf/maps v0.1.2 // indirect + github.com/knadh/koanf/providers/confmap v1.0.0 // indirect + github.com/knadh/koanf/v2 v2.2.1 // indirect + github.com/mitchellh/copystructure v1.2.0 // indirect + github.com/mitchellh/reflectwalk v1.0.2 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect go.opentelemetry.io/auto/sdk v1.1.0 // indirect + go.opentelemetry.io/collector/confmap v1.35.0 // indirect go.opentelemetry.io/collector/featuregate v1.35.0 // indirect go.opentelemetry.io/collector/internal/telemetry v0.129.0 // indirect go.opentelemetry.io/collector/pdata/pprofile v0.129.0 // indirect @@ -55,6 +64,7 @@ require ( go.opentelemetry.io/otel/trace v1.37.0 // indirect go.uber.org/multierr v1.11.0 // indirect go.uber.org/zap v1.27.0 // indirect + go.yaml.in/yaml/v3 v3.0.4 // indirect golang.org/x/crypto v0.39.0 // indirect golang.org/x/net v0.41.0 // indirect golang.org/x/sys v0.33.0 // indirect @@ -74,6 +84,8 @@ replace go.opentelemetry.io/collector/config/confignet => ../confignet replace go.opentelemetry.io/collector/config/configopaque => ../configopaque +replace go.opentelemetry.io/collector/config/configoptional => ../configoptional + replace go.opentelemetry.io/collector/config/configtls => ../configtls replace go.opentelemetry.io/collector/extension => ../../extension @@ -105,3 +117,5 @@ replace go.opentelemetry.io/collector/pipeline => ../../pipeline replace go.opentelemetry.io/collector/featuregate => ../../featuregate replace go.opentelemetry.io/collector/extension/extensionmiddleware/extensionmiddlewaretest => ../../extension/extensionmiddleware/extensionmiddlewaretest + +replace go.opentelemetry.io/collector/confmap => ../../confmap diff --git a/config/configgrpc/go.sum b/config/configgrpc/go.sum index a393d7c01c7..8ccc9026bdb 100644 --- a/config/configgrpc/go.sum +++ b/config/configgrpc/go.sum @@ -12,6 +12,10 @@ github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/go-viper/mapstructure/v2 v2.3.0 h1:27XbWsHIqhbdR5TIC911OfYvgSaW93HM+dX7970Q7jk= +github.com/go-viper/mapstructure/v2 v2.3.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= +github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= +github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= @@ -35,10 +39,20 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= +github.com/knadh/koanf/maps v0.1.2 h1:RBfmAW5CnZT+PJ1CVc1QSJKf4Xu9kxfQgYVQSu8hpbo= +github.com/knadh/koanf/maps v0.1.2/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI= +github.com/knadh/koanf/providers/confmap v1.0.0 h1:mHKLJTE7iXEys6deO5p6olAiZdG5zwp8Aebir+/EaRE= +github.com/knadh/koanf/providers/confmap v1.0.0/go.mod h1:txHYHiI2hAtF0/0sCmcuol4IDcuQbKTybiB1nOcUo1A= +github.com/knadh/koanf/v2 v2.2.1 h1:jaleChtw85y3UdBnI0wCqcg1sj1gPoz6D3caGNHtrNE= +github.com/knadh/koanf/v2 v2.2.1/go.mod h1:PSFru3ufQgTsI7IF+95rf9s8XA1+aHxKuO/W+dPoHEY= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= +github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= +github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ= +github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= diff --git a/config/configgrpc/server_middleware_test.go b/config/configgrpc/server_middleware_test.go index 0a0b736dff8..1fc47231a13 100644 --- a/config/configgrpc/server_middleware_test.go +++ b/config/configgrpc/server_middleware_test.go @@ -15,6 +15,7 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config/configmiddleware" "go.opentelemetry.io/collector/config/confignet" + "go.opentelemetry.io/collector/config/configoptional" "go.opentelemetry.io/collector/config/configtls" "go.opentelemetry.io/collector/extension" "go.opentelemetry.io/collector/extension/extensionmiddleware" @@ -75,7 +76,7 @@ func TestGrpcServerUnaryInterceptor(t *testing.T) { // Create the server with middleware interceptors { var srv *grpc.Server - srv, addr = server.startTestServerWithHost(t, ServerConfig{ + srv, addr = server.startTestServerWithHost(t, configoptional.Some(ServerConfig{ NetAddr: confignet.AddrConfig{ Endpoint: "localhost:0", Transport: confignet.TransportTypeTCP, @@ -84,7 +85,7 @@ func TestGrpcServerUnaryInterceptor(t *testing.T) { newTestMiddlewareConfig("test1"), newTestMiddlewareConfig("test2"), }, - }, host) + }), host) defer srv.Stop() } diff --git a/exporter/otlpexporter/config_test.go b/exporter/otlpexporter/config_test.go index e1df7667163..6a6f1e30c54 100644 --- a/exporter/otlpexporter/config_test.go +++ b/exporter/otlpexporter/config_test.go @@ -15,6 +15,7 @@ import ( "go.opentelemetry.io/collector/config/configauth" "go.opentelemetry.io/collector/config/configgrpc" "go.opentelemetry.io/collector/config/configopaque" + "go.opentelemetry.io/collector/config/configoptional" "go.opentelemetry.io/collector/config/configretry" "go.opentelemetry.io/collector/config/configtls" "go.opentelemetry.io/collector/confmap" @@ -75,14 +76,14 @@ func TestUnmarshalConfig(t *testing.T) { }, Insecure: false, }, - Keepalive: &configgrpc.KeepaliveClientConfig{ + Keepalive: configoptional.Some(configgrpc.KeepaliveClientConfig{ Time: 20 * time.Second, PermitWithoutStream: true, Timeout: 30 * time.Second, - }, + }), WriteBufferSize: 512 * 1024, BalancerName: "round_robin", - Auth: &configauth.Config{AuthenticatorID: component.MustNewID("nop")}, + Auth: configoptional.Some(configauth.Config{AuthenticatorID: component.MustNewID("nop")}), }, }, cfg) } diff --git a/exporter/otlpexporter/factory.go b/exporter/otlpexporter/factory.go index 7ad4eb2f15e..6ed5fe0db5d 100644 --- a/exporter/otlpexporter/factory.go +++ b/exporter/otlpexporter/factory.go @@ -9,6 +9,7 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config/configcompression" "go.opentelemetry.io/collector/config/configgrpc" + "go.opentelemetry.io/collector/config/configoptional" "go.opentelemetry.io/collector/config/configretry" "go.opentelemetry.io/collector/consumer" "go.opentelemetry.io/collector/exporter" @@ -37,7 +38,7 @@ func createDefaultConfig() component.Config { // We almost read 0 bytes, so no need to tune ReadBufferSize. clientCfg.WriteBufferSize = 512 * 1024 // For backward compatibility: - clientCfg.Keepalive = nil + clientCfg.Keepalive = configoptional.None[configgrpc.KeepaliveClientConfig]() clientCfg.BalancerName = "" return &Config{ diff --git a/exporter/otlpexporter/factory_test.go b/exporter/otlpexporter/factory_test.go index 64a7b79b7b9..eb612c05485 100644 --- a/exporter/otlpexporter/factory_test.go +++ b/exporter/otlpexporter/factory_test.go @@ -16,6 +16,7 @@ import ( "go.opentelemetry.io/collector/config/configcompression" "go.opentelemetry.io/collector/config/configgrpc" "go.opentelemetry.io/collector/config/configopaque" + "go.opentelemetry.io/collector/config/configoptional" "go.opentelemetry.io/collector/config/configretry" "go.opentelemetry.io/collector/config/configtls" "go.opentelemetry.io/collector/exporter/exporterhelper" @@ -71,11 +72,11 @@ func TestCreateTraces(t *testing.T) { config: &Config{ ClientConfig: configgrpc.ClientConfig{ Endpoint: endpoint, - Keepalive: &configgrpc.KeepaliveClientConfig{ + Keepalive: configoptional.Some(configgrpc.KeepaliveClientConfig{ Time: 30 * time.Second, Timeout: 25 * time.Second, PermitWithoutStream: true, - }, + }), }, }, }, @@ -222,11 +223,11 @@ func TestCreateProfiles(t *testing.T) { config: &Config{ ClientConfig: configgrpc.ClientConfig{ Endpoint: endpoint, - Keepalive: &configgrpc.KeepaliveClientConfig{ + Keepalive: configoptional.Some(configgrpc.KeepaliveClientConfig{ Time: 30 * time.Second, Timeout: 25 * time.Second, PermitWithoutStream: true, - }, + }), }, }, }, diff --git a/exporter/otlpexporter/go.mod b/exporter/otlpexporter/go.mod index 9f2976f4fe8..d383cc8034f 100644 --- a/exporter/otlpexporter/go.mod +++ b/exporter/otlpexporter/go.mod @@ -11,6 +11,7 @@ require ( go.opentelemetry.io/collector/config/configcompression v1.35.0 go.opentelemetry.io/collector/config/configgrpc v0.129.0 go.opentelemetry.io/collector/config/configopaque v1.35.0 + go.opentelemetry.io/collector/config/configoptional v0.129.0 go.opentelemetry.io/collector/config/configretry v1.35.0 go.opentelemetry.io/collector/config/configtls v1.35.0 go.opentelemetry.io/collector/confmap v1.35.0 @@ -178,3 +179,5 @@ replace go.opentelemetry.io/collector/config/configmiddleware => ../../config/co replace go.opentelemetry.io/collector/extension/extensionmiddleware/extensionmiddlewaretest => ../../extension/extensionmiddleware/extensionmiddlewaretest replace go.opentelemetry.io/collector/pdata/xpdata => ../../pdata/xpdata + +replace go.opentelemetry.io/collector/config/configoptional => ../../config/configoptional diff --git a/receiver/otlpreceiver/config_test.go b/receiver/otlpreceiver/config_test.go index 1ee9285f557..b11dd249487 100644 --- a/receiver/otlpreceiver/config_test.go +++ b/receiver/otlpreceiver/config_test.go @@ -111,17 +111,17 @@ func TestUnmarshalConfig(t *testing.T) { Endpoint: "localhost:4317", Transport: confignet.TransportTypeTCP, }, - TLS: &configtls.ServerConfig{ + TLS: configoptional.Some(configtls.ServerConfig{ Config: configtls.Config{ CertFile: "test.crt", KeyFile: "test.key", }, - }, + }), MaxRecvMsgSizeMiB: 32, MaxConcurrentStreams: 16, ReadBufferSize: 1024, WriteBufferSize: 1024, - Keepalive: &configgrpc.KeepaliveServerConfig{ + Keepalive: configoptional.Some(configgrpc.KeepaliveServerConfig{ ServerParameters: &configgrpc.KeepaliveServerParameters{ MaxConnectionIdle: 11 * time.Second, MaxConnectionAge: 12 * time.Second, @@ -133,7 +133,7 @@ func TestUnmarshalConfig(t *testing.T) { MinTime: 10 * time.Second, PermitWithoutStream: true, }, - }, + }), }), HTTP: configoptional.Some(HTTPConfig{ ServerConfig: confighttp.ServerConfig{ @@ -163,10 +163,6 @@ func TestUnmarshalConfig(t *testing.T) { }, cfg) } -func ptr[T any](v T) *T { - return &v -} - func TestUnmarshalConfigUnix(t *testing.T) { cm, err := confmaptest.LoadConf(filepath.Join("testdata", "uds.yaml")) require.NoError(t, err) @@ -182,7 +178,7 @@ func TestUnmarshalConfigUnix(t *testing.T) { Transport: confignet.TransportTypeUnix, }, ReadBufferSize: 512 * 1024, - Keepalive: ptr(configgrpc.NewDefaultKeepaliveServerConfig()), + Keepalive: configoptional.Some(configgrpc.NewDefaultKeepaliveServerConfig()), }), HTTP: configoptional.Some(HTTPConfig{ ServerConfig: confighttp.ServerConfig{ diff --git a/receiver/otlpreceiver/otlp_test.go b/receiver/otlpreceiver/otlp_test.go index dba45741c4f..6180eafaade 100644 --- a/receiver/otlpreceiver/otlp_test.go +++ b/receiver/otlpreceiver/otlp_test.go @@ -694,11 +694,11 @@ func TestGRPCInvalidTLSCredentials(t *testing.T) { Endpoint: testutil.GetAvailableLocalAddress(t), Transport: confignet.TransportTypeTCP, }, - TLS: &configtls.ServerConfig{ + TLS: configoptional.Some(configtls.ServerConfig{ Config: configtls.Config{ CertFile: "willfail", }, - }, + }), }), }, }