Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC for how to avoid duplicating WithStart and WithShutdown #12466

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,41 @@
return f(ctx)
}

type baseComponent struct {
StartFunc
ShutdownFunc
}

type Option interface {
apply(bc *baseComponent)
}

type optionFunc func(*baseComponent)

func (of optionFunc) apply(bc *baseComponent) {
of(bc)

Check warning on line 92 in component/component.go

View check run for this annotation

Codecov / codecov/patch

component/component.go#L91-L92

Added lines #L91 - L92 were not covered by tests
}

func WithStartFunc(start StartFunc) Option {
return optionFunc(func(o *baseComponent) {
o.StartFunc = start
})

Check warning on line 98 in component/component.go

View check run for this annotation

Codecov / codecov/patch

component/component.go#L95-L98

Added lines #L95 - L98 were not covered by tests
}

func WithShutdownFunc(shutdown ShutdownFunc) Option {
return optionFunc(func(o *baseComponent) {
o.ShutdownFunc = shutdown
})

Check warning on line 104 in component/component.go

View check run for this annotation

Codecov / codecov/patch

component/component.go#L101-L104

Added lines #L101 - L104 were not covered by tests
}

func NewComponent(opts ...Option) Component {
bc := baseComponent{}
for _, opt := range opts {
opt.apply(&bc)
}
return bc

Check warning on line 112 in component/component.go

View check run for this annotation

Codecov / codecov/patch

component/component.go#L107-L112

Added lines #L107 - L112 were not covered by tests
}

// Kind represents component kinds.
type Kind struct {
name string
Expand Down
61 changes: 36 additions & 25 deletions extension/auth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ type Client interface {

// ClientOption represents the possible options for NewClient.
type ClientOption interface {
apply(*defaultClient)
apply(*clientOptions)
}

type clientOptionFunc func(*defaultClient)
type clientOptionFunc func(*clientOptions)

func (of clientOptionFunc) apply(e *defaultClient) {
func (of clientOptionFunc) apply(e *clientOptions) {
of(e)
}

Expand All @@ -56,52 +56,63 @@ func (f ClientPerRPCCredentialsFunc) PerRPCCredentials() (credentials.PerRPCCred
return f()
}

type defaultClient struct {
component.StartFunc
component.ShutdownFunc
ClientRoundTripperFunc
ClientPerRPCCredentialsFunc
type clientOptions struct {
componentOptions []component.Option
roundTripper ClientRoundTripperFunc
perRPCCredentials ClientPerRPCCredentialsFunc
}

// WithClientStart overrides the default `Start` function for a component.Component.
// The default always returns nil.
func WithClientStart(startFunc component.StartFunc) ClientOption {
return clientOptionFunc(func(o *defaultClient) {
o.StartFunc = startFunc
// WithClientComponentOptions overrides the default component.Component (start/shutdown) functions for a Client.
// The default functions do nothing and always returns nil.
func WithClientComponentOptions(option ...component.Option) ClientOption {
return clientOptionFunc(func(e *clientOptions) {
e.componentOptions = append(e.componentOptions, option...)
})
}

// WithClientShutdown overrides the default `Shutdown` function for a component.Component.
// The default always returns nil.
func WithClientShutdown(shutdownFunc component.ShutdownFunc) ClientOption {
return clientOptionFunc(func(o *defaultClient) {
o.ShutdownFunc = shutdownFunc
})
// Deprecated: [v0.121.0] use WithClientComponentOptions.
func WithClientStart(start component.StartFunc) ClientOption {
return WithClientComponentOptions(component.WithStartFunc(start))
}

// Deprecated: [v0.121.0] use WithClientComponentOptions.
func WithClientShutdown(shutdown component.ShutdownFunc) ClientOption {
return WithClientComponentOptions(component.WithShutdownFunc(shutdown))
}

// WithClientRoundTripper provides a `RoundTripper` function for this client authenticator.
// The default round tripper is no-op.
func WithClientRoundTripper(roundTripperFunc ClientRoundTripperFunc) ClientOption {
return clientOptionFunc(func(o *defaultClient) {
o.ClientRoundTripperFunc = roundTripperFunc
return clientOptionFunc(func(o *clientOptions) {
o.roundTripper = roundTripperFunc
})
}

// WithClientPerRPCCredentials provides a `PerRPCCredentials` function for this client authenticator.
// There's no default.
func WithClientPerRPCCredentials(perRPCCredentialsFunc ClientPerRPCCredentialsFunc) ClientOption {
return clientOptionFunc(func(o *defaultClient) {
o.ClientPerRPCCredentialsFunc = perRPCCredentialsFunc
return clientOptionFunc(func(o *clientOptions) {
o.perRPCCredentials = perRPCCredentialsFunc
})
}

type baseClient struct {
component.Component
ClientRoundTripperFunc
ClientPerRPCCredentialsFunc
}

// NewClient returns a Client configured with the provided options.
func NewClient(options ...ClientOption) Client {
bc := &defaultClient{}
bc := &clientOptions{}

for _, op := range options {
op.apply(bc)
}

return bc
return baseClient{
Component: component.NewComponent(bc.componentOptions...),
ClientRoundTripperFunc: bc.roundTripper,
ClientPerRPCCredentialsFunc: bc.perRPCCredentials,
}
}
55 changes: 32 additions & 23 deletions extension/auth/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,19 @@ type Server interface {
Authenticate(ctx context.Context, sources map[string][]string) (context.Context, error)
}

type defaultServer struct {
ServerAuthenticateFunc
component.StartFunc
component.ShutdownFunc
type serverOptions struct {
componentOptions []component.Option
serverAuth ServerAuthenticateFunc
}

// ServerOption represents the possible options for NewServer.
type ServerOption interface {
apply(*defaultServer)
apply(*serverOptions)
}

type serverOptionFunc func(*defaultServer)
type serverOptionFunc func(*serverOptions)

func (of serverOptionFunc) apply(e *defaultServer) {
func (of serverOptionFunc) apply(e *serverOptions) {
of(e)
}

Expand All @@ -59,34 +58,44 @@ func (f ServerAuthenticateFunc) Authenticate(ctx context.Context, sources map[st

// WithServerAuthenticate specifies which function to use to perform the authentication.
func WithServerAuthenticate(authFunc ServerAuthenticateFunc) ServerOption {
return serverOptionFunc(func(o *defaultServer) {
o.ServerAuthenticateFunc = authFunc
return serverOptionFunc(func(o *serverOptions) {
o.serverAuth = authFunc
})
}

// WithServerStart overrides the default `Start` function for a component.Component.
// The default always returns nil.
func WithServerStart(startFunc component.StartFunc) ServerOption {
return serverOptionFunc(func(o *defaultServer) {
o.StartFunc = startFunc
// WithServerComponentOptions overrides the default component.Component (start/shutdown) functions for a Server.
// The default functions do nothing and always returns nil.
func WithServerComponentOptions(option ...component.Option) ServerOption {
return serverOptionFunc(func(e *serverOptions) {
e.componentOptions = append(e.componentOptions, option...)
})
}

// WithServerShutdown overrides the default `Shutdown` function for a component.Component.
// The default always returns nil.
func WithServerShutdown(shutdownFunc component.ShutdownFunc) ServerOption {
return serverOptionFunc(func(o *defaultServer) {
o.ShutdownFunc = shutdownFunc
})
// Deprecated: [v0.121.0] use WithServerComponentOptions.
func WithServerStart(start component.StartFunc) ServerOption {
return WithServerComponentOptions(component.WithStartFunc(start))
}

// Deprecated: [v0.121.0] use WithServerComponentOptions.
func WithServerShutdown(shutdown component.ShutdownFunc) ServerOption {
return WithServerComponentOptions(component.WithShutdownFunc(shutdown))
}

type baseServer struct {
component.Component
ServerAuthenticateFunc
}

// NewServer returns a Server configured with the provided options.
func NewServer(options ...ServerOption) Server {
bc := &defaultServer{}
so := &serverOptions{}

for _, op := range options {
op.apply(bc)
op.apply(so)
}

return bc
return baseServer{
Component: component.NewComponent(so.componentOptions...),
ServerAuthenticateFunc: so.serverAuth,
}
}
8 changes: 3 additions & 5 deletions processor/processorhelper/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import (
type ProcessLogsFunc func(context.Context, plog.Logs) (plog.Logs, error)

type logs struct {
component.StartFunc
component.ShutdownFunc
component.Component
consumer.Logs
}

Expand Down Expand Up @@ -70,8 +69,7 @@ func NewLogs(
}

return &logs{
StartFunc: bs.StartFunc,
ShutdownFunc: bs.ShutdownFunc,
Logs: logsConsumer,
Component: component.NewComponent(bs.componentOptions...),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tiny nit: The abbreviated name of this "bs" variable isn't clear entirely clear here (metrics.go and traces.go also use it). The other files use names that are clearer from context.

Logs: logsConsumer,
}, nil
}
8 changes: 3 additions & 5 deletions processor/processorhelper/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import (
type ProcessMetricsFunc func(context.Context, pmetric.Metrics) (pmetric.Metrics, error)

type metrics struct {
component.StartFunc
component.ShutdownFunc
component.Component
consumer.Metrics
}

Expand Down Expand Up @@ -70,8 +69,7 @@ func NewMetrics(
}

return &metrics{
StartFunc: bs.StartFunc,
ShutdownFunc: bs.ShutdownFunc,
Metrics: metricsConsumer,
Component: component.NewComponent(bs.componentOptions...),
Metrics: metricsConsumer,
}, nil
}
27 changes: 14 additions & 13 deletions processor/processorhelper/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,22 @@ func (of optionFunc) apply(e *baseSettings) {
of(e)
}

// WithStart overrides the default Start function for an processor.
// The default shutdown function does nothing and always returns nil.
func WithStart(start component.StartFunc) Option {
return optionFunc(func(o *baseSettings) {
o.StartFunc = start
// WithComponentOptions overrides the default component (start/shutdown) functions for a processor.
// The default functions do nothing and always returns nil.
func WithComponentOptions(option ...component.Option) Option {
return optionFunc(func(e *baseSettings) {
e.componentOptions = append(e.componentOptions, option...)
})
}

// WithShutdown overrides the default Shutdown function for an processor.
// The default shutdown function does nothing and always returns nil.
// Deprecated: [v0.121.0] use WithComponentOptions.
func WithStart(start component.StartFunc) Option {
return WithComponentOptions(component.WithStartFunc(start))
}

// Deprecated: [v0.121.0] use WithComponentOptions.
func WithShutdown(shutdown component.ShutdownFunc) Option {
return optionFunc(func(o *baseSettings) {
o.ShutdownFunc = shutdown
})
return WithComponentOptions(component.WithShutdownFunc(shutdown))
}

// WithCapabilities overrides the default GetCapabilities function for an processor.
Expand All @@ -57,9 +59,8 @@ func WithCapabilities(capabilities consumer.Capabilities) Option {
}

type baseSettings struct {
component.StartFunc
component.ShutdownFunc
consumerOptions []consumer.Option
componentOptions []component.Option
consumerOptions []consumer.Option
}

// fromOptions returns the internal settings starting from the default and applying all options.
Expand Down
8 changes: 3 additions & 5 deletions processor/processorhelper/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import (
type ProcessTracesFunc func(context.Context, ptrace.Traces) (ptrace.Traces, error)

type traces struct {
component.StartFunc
component.ShutdownFunc
component.Component
consumer.Traces
}

Expand Down Expand Up @@ -70,8 +69,7 @@ func NewTraces(
}

return &traces{
StartFunc: bs.StartFunc,
ShutdownFunc: bs.ShutdownFunc,
Traces: traceConsumer,
Component: component.NewComponent(bs.componentOptions...),
Traces: traceConsumer,
}, nil
}
Loading