Skip to content

Commit

Permalink
otelcol: carry factory func instead of object in settings
Browse files Browse the repository at this point in the history
Signed-off-by: Benedikt Bongartz <bongartz@klimlive.de>
  • Loading branch information
frzifus committed Oct 5, 2023
1 parent 1df2880 commit 2279213
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 135 deletions.
11 changes: 1 addition & 10 deletions cmd/builder/internal/builder/templates/main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,13 @@ import (
)

func main() {
if err := otelcol.NewCommandFeatureGate().Execute(); err != nil {
log.Fatalf("failed to validate featuregates: %v", err)
}

factories, err := components()
if err != nil {
log.Fatalf("failed to build components: %v", err)
}

info := component.BuildInfo{
Command: "{{ .Distribution.Name }}",
Description: "{{ .Distribution.Description }}",
Version: "{{ .Distribution.Version }}",
}

if err := run(otelcol.CollectorSettings{BuildInfo: info, Factories: factories}); err != nil {
if err := run(otelcol.CollectorSettings{BuildInfo: info, Factories: components}); err != nil {
log.Fatal(err)
}
}
Expand Down
11 changes: 1 addition & 10 deletions cmd/otelcorecol/main.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions otelcol/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (s State) String() string {
// CollectorSettings holds configuration for creating a new Collector.
type CollectorSettings struct {
// Factories service factories.
Factories Factories
Factories func() (Factories, error)

// BuildInfo provides collector start information.
BuildInfo component.BuildInfo
Expand Down Expand Up @@ -155,7 +155,11 @@ func (col *Collector) setupConfigurationComponents(ctx context.Context) error {
}
}

cfg, err := col.set.ConfigProvider.Get(ctx, col.set.Factories)
factories, err := col.set.Factories()
if err != nil {
return fmt.Errorf("failed to initialize factories: %w", err)
}
cfg, err := col.set.ConfigProvider.Get(ctx, factories)
if err != nil {
return fmt.Errorf("failed to get config: %w", err)
}
Expand All @@ -167,11 +171,11 @@ func (col *Collector) setupConfigurationComponents(ctx context.Context) error {
col.service, err = service.New(ctx, service.Settings{
BuildInfo: col.set.BuildInfo,
CollectorConf: conf,
Receivers: receiver.NewBuilder(cfg.Receivers, col.set.Factories.Receivers),
Processors: processor.NewBuilder(cfg.Processors, col.set.Factories.Processors),
Exporters: exporter.NewBuilder(cfg.Exporters, col.set.Factories.Exporters),
Connectors: connector.NewBuilder(cfg.Connectors, col.set.Factories.Connectors),
Extensions: extension.NewBuilder(cfg.Extensions, col.set.Factories.Extensions),
Receivers: receiver.NewBuilder(cfg.Receivers, factories.Receivers),
Processors: processor.NewBuilder(cfg.Processors, factories.Processors),
Exporters: exporter.NewBuilder(cfg.Exporters, factories.Exporters),
Connectors: connector.NewBuilder(cfg.Connectors, factories.Connectors),
Extensions: extension.NewBuilder(cfg.Extensions, factories.Extensions),
AsyncErrorChannel: col.asyncErrorChannel,
LoggingOptions: col.set.LoggingOptions,
}, cfg.Service)
Expand Down Expand Up @@ -207,7 +211,11 @@ func (col *Collector) reloadConfiguration(ctx context.Context) error {
}

func (col *Collector) DryRun(ctx context.Context) error {
cfg, err := col.set.ConfigProvider.Get(ctx, col.set.Factories)
factories, err := col.set.Factories()
if err != nil {
return fmt.Errorf("failed to initialize factories: %w", err)
}
cfg, err := col.set.ConfigProvider.Get(ctx, factories)
if err != nil {
return fmt.Errorf("failed to get config: %w", err)
}
Expand Down
64 changes: 13 additions & 51 deletions otelcol/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,12 @@ func TestStateString(t *testing.T) {
}

func TestCollectorStartAsGoRoutine(t *testing.T) {
factories, err := nopFactories()
require.NoError(t, err)

cfgProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-nop.yaml")}))
require.NoError(t, err)

set := CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
Factories: nopFactories,
ConfigProvider: cfgProvider,
}
col, err := NewCollector(set)
Expand All @@ -57,15 +54,12 @@ func TestCollectorStartAsGoRoutine(t *testing.T) {
}

func TestCollectorCancelContext(t *testing.T) {
factories, err := nopFactories()
require.NoError(t, err)

cfgProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-nop.yaml")}))
require.NoError(t, err)

set := CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
Factories: nopFactories,
ConfigProvider: cfgProvider,
}
col, err := NewCollector(set)
Expand Down Expand Up @@ -93,16 +87,13 @@ func (p mockCfgProvider) Watch() <-chan error {
}

func TestCollectorStateAfterConfigChange(t *testing.T) {
factories, err := nopFactories()
require.NoError(t, err)

provider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-nop.yaml")}))
require.NoError(t, err)

watcher := make(chan error, 1)
col, err := NewCollector(CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
Factories: nopFactories,
ConfigProvider: &mockCfgProvider{ConfigProvider: provider, watcher: watcher},
})
require.NoError(t, err)
Expand All @@ -126,15 +117,12 @@ func TestCollectorStateAfterConfigChange(t *testing.T) {
}

func TestCollectorReportError(t *testing.T) {
factories, err := nopFactories()
require.NoError(t, err)

cfgProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-nop.yaml")}))
require.NoError(t, err)

col, err := NewCollector(CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
Factories: nopFactories,
ConfigProvider: cfgProvider,
})
require.NoError(t, err)
Expand All @@ -152,15 +140,12 @@ func TestCollectorReportError(t *testing.T) {
}

func TestCollectorSendSignal(t *testing.T) {
factories, err := nopFactories()
require.NoError(t, err)

cfgProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-nop.yaml")}))
require.NoError(t, err)

col, err := NewCollector(CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
Factories: nopFactories,
ConfigProvider: cfgProvider,
})
require.NoError(t, err)
Expand All @@ -185,15 +170,13 @@ func TestCollectorSendSignal(t *testing.T) {

func TestCollectorFailedShutdown(t *testing.T) {
t.Skip("This test was using telemetry shutdown failure, switch to use a component that errors on shutdown.")
factories, err := nopFactories()
require.NoError(t, err)

cfgProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-nop.yaml")}))
require.NoError(t, err)

col, err := NewCollector(CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
Factories: nopFactories,
ConfigProvider: cfgProvider,
})
require.NoError(t, err)
Expand All @@ -216,15 +199,12 @@ func TestCollectorFailedShutdown(t *testing.T) {
}

func TestCollectorStartInvalidConfig(t *testing.T) {
factories, err := nopFactories()
require.NoError(t, err)

cfgProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-invalid.yaml")}))
require.NoError(t, err)

col, err := NewCollector(CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
Factories: nopFactories,
ConfigProvider: cfgProvider,
})
require.NoError(t, err)
Expand All @@ -243,15 +223,12 @@ func TestCollectorStartWithTraceContextPropagation(t *testing.T) {

for _, tt := range tests {
t.Run(tt.file, func(t *testing.T) {
factories, err := nopFactories()
require.NoError(t, err)

cfgProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", tt.file)}))
require.NoError(t, err)

set := CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
Factories: nopFactories,
ConfigProvider: cfgProvider,
}

Expand Down Expand Up @@ -281,15 +258,12 @@ func TestCollectorRun(t *testing.T) {

for _, tt := range tests {
t.Run(tt.file, func(t *testing.T) {
factories, err := nopFactories()
require.NoError(t, err)

cfgProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", tt.file)}))
require.NoError(t, err)

set := CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
Factories: nopFactories,
ConfigProvider: cfgProvider,
}
col, err := NewCollector(set)
Expand All @@ -305,15 +279,12 @@ func TestCollectorRun(t *testing.T) {
}

func TestCollectorShutdownBeforeRun(t *testing.T) {
factories, err := nopFactories()
require.NoError(t, err)

cfgProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-nop.yaml")}))
require.NoError(t, err)

set := CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
Factories: nopFactories,
ConfigProvider: cfgProvider,
}
col, err := NewCollector(set)
Expand All @@ -330,16 +301,13 @@ func TestCollectorShutdownBeforeRun(t *testing.T) {
}

func TestCollectorClosedStateOnStartUpError(t *testing.T) {
factories, err := nopFactories()
require.NoError(t, err)

cfgProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-invalid.yaml")}))
require.NoError(t, err)

// Load a bad config causing startup to fail
set := CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
Factories: nopFactories,
ConfigProvider: cfgProvider,
}
col, err := NewCollector(set)
Expand All @@ -353,16 +321,13 @@ func TestCollectorClosedStateOnStartUpError(t *testing.T) {
}

func TestCollectorDryRun(t *testing.T) {
factories, err := nopFactories()
require.NoError(t, err)

cfgProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-invalid.yaml")}))
require.NoError(t, err)

// Load a bad config causing startup to fail
set := CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
Factories: nopFactories,
ConfigProvider: cfgProvider,
}
col, err := NewCollector(set)
Expand All @@ -372,9 +337,6 @@ func TestCollectorDryRun(t *testing.T) {
}

func TestPassConfmapToServiceFailure(t *testing.T) {
factories, err := nopFactories()
require.NoError(t, err)

cfgProvider, err := NewConfigProvider(ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: []string{filepath.Join("testdata", "otelcol-invalid.yaml")},
Expand All @@ -386,7 +348,7 @@ func TestPassConfmapToServiceFailure(t *testing.T) {

set := CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
Factories: nopFactories,
ConfigProvider: cfgProvider,
}
col, err := NewCollector(set)
Expand Down
6 changes: 1 addition & 5 deletions otelcol/collector_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sys/windows/svc"

"go.opentelemetry.io/collector/component"
Expand All @@ -23,10 +22,7 @@ func TestNewSvcHandler(t *testing.T) {
defer func() { os.Args = oldArgs }()
os.Args = []string{"otelcol", "--config", filepath.Join("testdata", "otelcol-nop.yaml")}

factories, err := nopFactories()
require.NoError(t, err)

s := NewSvcHandler(CollectorSettings{BuildInfo: component.NewDefaultBuildInfo(), Factories: factories})
s := NewSvcHandler(CollectorSettings{BuildInfo: component.NewDefaultBuildInfo(), Factories: nopFactories})

colDone := make(chan struct{})
requests := make(chan svc.ChangeRequest)
Expand Down
Loading

0 comments on commit 2279213

Please sign in to comment.