Skip to content

Commit

Permalink
Add the StartFunc/ShutdownFunc to component directly
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu committed Feb 3, 2022
1 parent 7cdf8c7 commit 9252f70
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 60 deletions.
22 changes: 22 additions & 0 deletions component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ type Component interface {
Shutdown(ctx context.Context) error
}

// StartFunc specifies the function invoked when the component.Component is being started.
type StartFunc func(context.Context, Host) error

// Start starts the component.
func (f StartFunc) Start(ctx context.Context, host Host) error {
if f == nil {
return nil
}
return f(ctx, host)
}

// ShutdownFunc specifies the function invoked when the component.Component is being shutdown.
type ShutdownFunc func(context.Context) error

// Shutdown shuts down the component.
func (f ShutdownFunc) Shutdown(ctx context.Context) error {
if f == nil {
return nil
}
return f(ctx)
}

// Kind represents component kinds.
type Kind int

Expand Down
34 changes: 8 additions & 26 deletions component/componenthelper/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,37 @@
package componenthelper // import "go.opentelemetry.io/collector/component/componenthelper"

import (
"context"

"go.opentelemetry.io/collector/component"
)

// StartFunc specifies the function invoked when the component.Component is being started.
type StartFunc func(context.Context, component.Host) error

// Start starts the component.
func (f StartFunc) Start(ctx context.Context, host component.Host) error {
if f == nil {
return nil
}
return f(ctx, host)
}
// Deprecated: use component.StartFunc.
type StartFunc = component.StartFunc

// ShutdownFunc specifies the function invoked when the component.Component is being shutdown.
type ShutdownFunc func(context.Context) error

// Shutdown shuts down the component.
func (f ShutdownFunc) Shutdown(ctx context.Context) error {
if f == nil {
return nil
}
return f(ctx)
}
// Deprecated: use component.ShutdownFunc.
type ShutdownFunc = component.ShutdownFunc

// Option represents the possible options for New.
type Option func(*baseComponent)

// WithStart overrides the default `Start` function for a component.Component.
// The default always returns nil.
func WithStart(startFunc StartFunc) Option {
func WithStart(startFunc component.StartFunc) Option {
return func(o *baseComponent) {
o.StartFunc = startFunc
}
}

// WithShutdown overrides the default `Shutdown` function for a component.Component.
// The default always returns nil.
func WithShutdown(shutdownFunc ShutdownFunc) Option {
func WithShutdown(shutdownFunc component.ShutdownFunc) Option {
return func(o *baseComponent) {
o.ShutdownFunc = shutdownFunc
}
}

type baseComponent struct {
StartFunc
ShutdownFunc
component.StartFunc
component.ShutdownFunc
}

// New returns a component.Component configured with the provided options.
Expand Down
6 changes: 3 additions & 3 deletions component/componenttest/nop_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
package componenttest // import "go.opentelemetry.io/collector/component/componenttest"

import (
"go.opentelemetry.io/collector/component/componenthelper"
"go.opentelemetry.io/collector/component"
)

type nopComponent struct {
componenthelper.StartFunc
componenthelper.ShutdownFunc
component.StartFunc
component.ShutdownFunc
}
9 changes: 4 additions & 5 deletions config/configauth/default_serverauthenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenthelper"
)

var _ ServerAuthenticator = (*defaultServerAuthenticator)(nil)
Expand All @@ -28,8 +27,8 @@ type Option func(*defaultServerAuthenticator)

type defaultServerAuthenticator struct {
AuthenticateFunc
componenthelper.StartFunc
componenthelper.ShutdownFunc
component.StartFunc
component.ShutdownFunc
}

// WithAuthenticate specifies which function to use to perform the authentication.
Expand All @@ -41,15 +40,15 @@ func WithAuthenticate(authenticateFunc AuthenticateFunc) Option {

// WithStart overrides the default `Start` function for a component.Component.
// The default always returns nil.
func WithStart(startFunc componenthelper.StartFunc) Option {
func WithStart(startFunc component.StartFunc) Option {
return func(o *defaultServerAuthenticator) {
o.StartFunc = startFunc
}
}

// WithShutdown overrides the default `Shutdown` function for a component.Component.
// The default always returns nil.
func WithShutdown(shutdownFunc componenthelper.ShutdownFunc) Option {
func WithShutdown(shutdownFunc component.ShutdownFunc) Option {
return func(o *defaultServerAuthenticator) {
o.ShutdownFunc = shutdownFunc
}
Expand Down
13 changes: 6 additions & 7 deletions exporter/exporterhelper/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"time"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenthelper"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumerhelper"
Expand Down Expand Up @@ -88,8 +87,8 @@ func (req *baseRequest) OnProcessingFinished() {

// baseSettings represents all the options that users can configure.
type baseSettings struct {
componenthelper.StartFunc
componenthelper.ShutdownFunc
component.StartFunc
component.ShutdownFunc
consumerOptions []consumerhelper.Option
TimeoutSettings
QueueSettings
Expand Down Expand Up @@ -119,15 +118,15 @@ type Option func(*baseSettings)

// WithStart overrides the default Start function for an exporter.
// The default start function does nothing and always returns nil.
func WithStart(start componenthelper.StartFunc) Option {
func WithStart(start component.StartFunc) Option {
return func(o *baseSettings) {
o.StartFunc = start
}
}

// WithShutdown overrides the default Shutdown function for an exporter.
// The default shutdown function does nothing and always returns nil.
func WithShutdown(shutdown componenthelper.ShutdownFunc) Option {
func WithShutdown(shutdown component.ShutdownFunc) Option {
return func(o *baseSettings) {
o.ShutdownFunc = shutdown
}
Expand Down Expand Up @@ -168,8 +167,8 @@ func WithCapabilities(capabilities consumer.Capabilities) Option {

// baseExporter contains common fields between different exporter types.
type baseExporter struct {
componenthelper.StartFunc
componenthelper.ShutdownFunc
component.StartFunc
component.ShutdownFunc
obsrep *obsExporter
sender requestSender
qrSender *queuedRetrySender
Expand Down
5 changes: 2 additions & 3 deletions processor/processorhelper/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/component/componenthelper"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumerhelper"
Expand All @@ -34,8 +33,8 @@ import (
type ProcessLogsFunc func(context.Context, pdata.Logs) (pdata.Logs, error)

type logProcessor struct {
componenthelper.StartFunc
componenthelper.ShutdownFunc
component.StartFunc
component.ShutdownFunc
consumer.Logs
}

Expand Down
5 changes: 2 additions & 3 deletions processor/processorhelper/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/component/componenthelper"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumerhelper"
Expand All @@ -34,8 +33,8 @@ import (
type ProcessMetricsFunc func(context.Context, pdata.Metrics) (pdata.Metrics, error)

type metricsProcessor struct {
componenthelper.StartFunc
componenthelper.ShutdownFunc
component.StartFunc
component.ShutdownFunc
consumer.Metrics
}

Expand Down
10 changes: 5 additions & 5 deletions processor/processorhelper/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"

"go.opentelemetry.io/collector/component/componenthelper"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumerhelper"
Expand All @@ -37,15 +37,15 @@ type Option func(*baseSettings)

// WithStart overrides the default Start function for an processor.
// The default shutdown function does nothing and always returns nil.
func WithStart(start componenthelper.StartFunc) Option {
func WithStart(start component.StartFunc) Option {
return func(o *baseSettings) {
o.StartFunc = start
}
}

// WithShutdown overrides the default Shutdown function for an processor.
// The default shutdown function does nothing and always returns nil.
func WithShutdown(shutdown componenthelper.ShutdownFunc) Option {
func WithShutdown(shutdown component.ShutdownFunc) Option {
return func(o *baseSettings) {
o.ShutdownFunc = shutdown
}
Expand All @@ -60,8 +60,8 @@ func WithCapabilities(capabilities consumer.Capabilities) Option {
}

type baseSettings struct {
componenthelper.StartFunc
componenthelper.ShutdownFunc
component.StartFunc
component.ShutdownFunc
consumerOptions []consumerhelper.Option
}

Expand Down
5 changes: 2 additions & 3 deletions processor/processorhelper/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/component/componenthelper"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumerhelper"
Expand All @@ -34,8 +33,8 @@ import (
type ProcessTracesFunc func(context.Context, pdata.Traces) (pdata.Traces, error)

type tracesProcessor struct {
componenthelper.StartFunc
componenthelper.ShutdownFunc
component.StartFunc
component.ShutdownFunc
consumer.Traces
}

Expand Down
9 changes: 4 additions & 5 deletions receiver/scraperhelper/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"errors"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenthelper"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/model/pdata"
)
Expand All @@ -46,14 +45,14 @@ type Scraper interface {
type ScraperOption func(*baseScraper)

// WithStart sets the function that will be called on startup.
func WithStart(start componenthelper.StartFunc) ScraperOption {
func WithStart(start component.StartFunc) ScraperOption {
return func(o *baseScraper) {
o.StartFunc = start
}
}

// WithShutdown sets the function that will be called on shutdown.
func WithShutdown(shutdown componenthelper.ShutdownFunc) ScraperOption {
func WithShutdown(shutdown component.ShutdownFunc) ScraperOption {
return func(o *baseScraper) {
o.ShutdownFunc = shutdown
}
Expand All @@ -62,8 +61,8 @@ func WithShutdown(shutdown componenthelper.ShutdownFunc) ScraperOption {
var _ Scraper = (*baseScraper)(nil)

type baseScraper struct {
componenthelper.StartFunc
componenthelper.ShutdownFunc
component.StartFunc
component.ShutdownFunc
ScrapeFunc
id config.ComponentID
}
Expand Down

0 comments on commit 9252f70

Please sign in to comment.