From 28d73a93b035302785d76eeea976a96ccabb6abc Mon Sep 17 00:00:00 2001 From: Blake Rouse Date: Wed, 9 Sep 2020 13:43:01 -0400 Subject: [PATCH] Fix loggers in composable module. --- .../pkg/agent/application/inspect_output_cmd.go | 2 +- .../pkg/agent/application/local_mode.go | 2 +- .../pkg/agent/application/managed_mode.go | 2 +- .../pkg/agent/application/managed_mode_test.go | 2 +- x-pack/elastic-agent/pkg/composable/context.go | 3 ++- x-pack/elastic-agent/pkg/composable/controller.go | 12 +++++------- .../elastic-agent/pkg/composable/controller_test.go | 6 +++++- x-pack/elastic-agent/pkg/composable/dynamic.go | 3 ++- .../pkg/composable/providers/agent/agent.go | 3 ++- .../pkg/composable/providers/agent/agent_test.go | 2 +- .../pkg/composable/providers/env/env.go | 3 ++- .../pkg/composable/providers/env/env_test.go | 2 +- .../pkg/composable/providers/host/host.go | 8 ++------ .../pkg/composable/providers/host/host_test.go | 8 +++++--- .../pkg/composable/providers/local/local.go | 3 ++- .../pkg/composable/providers/local/local_test.go | 2 +- .../providers/localdynamic/localdynamic.go | 3 ++- .../providers/localdynamic/localdynamic_test.go | 2 +- .../pkg/composable/providers/path/path.go | 3 ++- .../pkg/composable/providers/path/path_test.go | 2 +- 20 files changed, 40 insertions(+), 33 deletions(-) diff --git a/x-pack/elastic-agent/pkg/agent/application/inspect_output_cmd.go b/x-pack/elastic-agent/pkg/agent/application/inspect_output_cmd.go index c25966c6219..8f648887d10 100644 --- a/x-pack/elastic-agent/pkg/agent/application/inspect_output_cmd.go +++ b/x-pack/elastic-agent/pkg/agent/application/inspect_output_cmd.go @@ -178,7 +178,7 @@ func getProgramsFromConfig(log *logger.Logger, cfg *config.Config) (map[string][ router := &inmemRouter{} ctx, cancel := context.WithCancel(context.Background()) defer cancel() - composableCtrl, err := composable.New(cfg) + composableCtrl, err := composable.New(log, cfg) if err != nil { return nil, err } diff --git a/x-pack/elastic-agent/pkg/agent/application/local_mode.go b/x-pack/elastic-agent/pkg/agent/application/local_mode.go index e88f2734c75..5559089404e 100644 --- a/x-pack/elastic-agent/pkg/agent/application/local_mode.go +++ b/x-pack/elastic-agent/pkg/agent/application/local_mode.go @@ -103,7 +103,7 @@ func newLocal( } localApplication.router = router - composableCtrl, err := composable.New(rawConfig) + composableCtrl, err := composable.New(log, rawConfig) if err != nil { return nil, errors.New(err, "failed to initialize composable controller") } diff --git a/x-pack/elastic-agent/pkg/agent/application/managed_mode.go b/x-pack/elastic-agent/pkg/agent/application/managed_mode.go index 325d41e4726..2b5c517d970 100644 --- a/x-pack/elastic-agent/pkg/agent/application/managed_mode.go +++ b/x-pack/elastic-agent/pkg/agent/application/managed_mode.go @@ -148,7 +148,7 @@ func newManaged( } managedApplication.router = router - composableCtrl, err := composable.New(rawConfig) + composableCtrl, err := composable.New(log, rawConfig) if err != nil { return nil, errors.New(err, "failed to initialize composable controller") } diff --git a/x-pack/elastic-agent/pkg/agent/application/managed_mode_test.go b/x-pack/elastic-agent/pkg/agent/application/managed_mode_test.go index a636790b056..9b51016a126 100644 --- a/x-pack/elastic-agent/pkg/agent/application/managed_mode_test.go +++ b/x-pack/elastic-agent/pkg/agent/application/managed_mode_test.go @@ -32,7 +32,7 @@ func TestManagedModeRouting(t *testing.T) { log, _ := logger.New("") router, _ := newRouter(log, streamFn) - composableCtrl, _ := composable.New(nil) + composableCtrl, _ := composable.New(log, nil) emit, err := emitter(ctx, log, composableCtrl, router, &configModifiers{Decorators: []decoratorFunc{injectMonitoring}}) require.NoError(t, err) diff --git a/x-pack/elastic-agent/pkg/composable/context.go b/x-pack/elastic-agent/pkg/composable/context.go index 5e5eb4a9c6f..f77033d1d6d 100644 --- a/x-pack/elastic-agent/pkg/composable/context.go +++ b/x-pack/elastic-agent/pkg/composable/context.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" ) // ContextProviderComm is the interface that a context provider uses to communicate back to Elastic Agent. @@ -27,7 +28,7 @@ type ContextProvider interface { } // ContextProviderBuilder creates a new context provider based on the given config and returns it. -type ContextProviderBuilder func(config *config.Config) (ContextProvider, error) +type ContextProviderBuilder func(log *logger.Logger, config *config.Config) (ContextProvider, error) // AddContextProvider adds a new ContextProviderBuilder func (r *providerRegistry) AddContextProvider(name string, builder ContextProviderBuilder) error { diff --git a/x-pack/elastic-agent/pkg/composable/controller.go b/x-pack/elastic-agent/pkg/composable/controller.go index 21e08de7a6e..50cc9b69ead 100644 --- a/x-pack/elastic-agent/pkg/composable/controller.go +++ b/x-pack/elastic-agent/pkg/composable/controller.go @@ -8,6 +8,7 @@ import ( "context" "encoding/json" "fmt" + "strings" "reflect" "sort" @@ -38,11 +39,8 @@ type controller struct { } // New creates a new controller. -func New(c *config.Config) (Controller, error) { - l, err := logger.New("composable") - if err != nil { - return nil, err - } +func New(log *logger.Logger, c *config.Config) (Controller, error) { + l := log.Named("composable") l.Info("EXPERIMENTAL - Inputs with variables are currently experimental and should not be used in production") var providersCfg Config @@ -61,7 +59,7 @@ func New(c *config.Config) (Controller, error) { // explicitly disabled; skipping continue } - provider, err := builder(pCfg) + provider, err := builder(l, pCfg) if err != nil { return nil, errors.New(err, fmt.Sprintf("failed to build provider '%s'", name), errors.TypeConfig, errors.M("provider", name)) } @@ -78,7 +76,7 @@ func New(c *config.Config) (Controller, error) { // explicitly disabled; skipping continue } - provider, err := builder(pCfg) + provider, err := builder(l.Named(strings.Join([]string{"providers", name}, ".")), pCfg) if err != nil { return nil, errors.New(err, fmt.Sprintf("failed to build provider '%s'", name), errors.TypeConfig, errors.M("provider", name)) } diff --git a/x-pack/elastic-agent/pkg/composable/controller_test.go b/x-pack/elastic-agent/pkg/composable/controller_test.go index a68dc7da2b8..da05a41bfd3 100644 --- a/x-pack/elastic-agent/pkg/composable/controller_test.go +++ b/x-pack/elastic-agent/pkg/composable/controller_test.go @@ -9,6 +9,8 @@ import ( "sync" "testing" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/transpiler" "github.com/stretchr/testify/assert" @@ -73,7 +75,9 @@ func TestController(t *testing.T) { }) require.NoError(t, err) - c, err := composable.New(cfg) + log, err := logger.New("") + require.NoError(t, err) + c, err := composable.New(log, cfg) require.NoError(t, err) var wg sync.WaitGroup diff --git a/x-pack/elastic-agent/pkg/composable/dynamic.go b/x-pack/elastic-agent/pkg/composable/dynamic.go index 7a265ee80aa..33fbaca5a52 100644 --- a/x-pack/elastic-agent/pkg/composable/dynamic.go +++ b/x-pack/elastic-agent/pkg/composable/dynamic.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" ) // DynamicProviderComm is the interface that an dynamic provider uses to communicate back to Elastic Agent. @@ -29,7 +30,7 @@ type DynamicProvider interface { } // DynamicProviderBuilder creates a new dynamic provider based on the given config and returns it. -type DynamicProviderBuilder func(config *config.Config) (DynamicProvider, error) +type DynamicProviderBuilder func(log *logger.Logger, config *config.Config) (DynamicProvider, error) // AddDynamicProvider adds a new DynamicProviderBuilder func (r *providerRegistry) AddDynamicProvider(name string, builder DynamicProviderBuilder) error { diff --git a/x-pack/elastic-agent/pkg/composable/providers/agent/agent.go b/x-pack/elastic-agent/pkg/composable/providers/agent/agent.go index bf87833747d..c6f1a91e321 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/agent/agent.go +++ b/x-pack/elastic-agent/pkg/composable/providers/agent/agent.go @@ -9,6 +9,7 @@ import ( "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/composable" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/release" ) @@ -40,6 +41,6 @@ func (*contextProvider) Run(comm composable.ContextProviderComm) error { } // ContextProviderBuilder builds the context provider. -func ContextProviderBuilder(_ *config.Config) (composable.ContextProvider, error) { +func ContextProviderBuilder(_ *logger.Logger, _ *config.Config) (composable.ContextProvider, error) { return &contextProvider{}, nil } diff --git a/x-pack/elastic-agent/pkg/composable/providers/agent/agent_test.go b/x-pack/elastic-agent/pkg/composable/providers/agent/agent_test.go index 262cc891455..dccfc83dfca 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/agent/agent_test.go +++ b/x-pack/elastic-agent/pkg/composable/providers/agent/agent_test.go @@ -17,7 +17,7 @@ import ( func TestContextProvider(t *testing.T) { builder, _ := composable.Providers.GetContextProvider("agent") - provider, err := builder(nil) + provider, err := builder(nil, nil) require.NoError(t, err) comm := ctesting.NewContextComm(context.Background()) diff --git a/x-pack/elastic-agent/pkg/composable/providers/env/env.go b/x-pack/elastic-agent/pkg/composable/providers/env/env.go index 82bc2d0b1c9..1eefb7c2ff3 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/env/env.go +++ b/x-pack/elastic-agent/pkg/composable/providers/env/env.go @@ -11,6 +11,7 @@ import ( "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/composable" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" ) func init() { @@ -29,7 +30,7 @@ func (*contextProvider) Run(comm composable.ContextProviderComm) error { } // ContextProviderBuilder builds the context provider. -func ContextProviderBuilder(_ *config.Config) (composable.ContextProvider, error) { +func ContextProviderBuilder(_ *logger.Logger, _ *config.Config) (composable.ContextProvider, error) { return &contextProvider{}, nil } diff --git a/x-pack/elastic-agent/pkg/composable/providers/env/env_test.go b/x-pack/elastic-agent/pkg/composable/providers/env/env_test.go index 136b9dc8896..12d9e0c985a 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/env/env_test.go +++ b/x-pack/elastic-agent/pkg/composable/providers/env/env_test.go @@ -17,7 +17,7 @@ import ( func TestContextProvider(t *testing.T) { builder, _ := composable.Providers.GetContextProvider("env") - provider, err := builder(nil) + provider, err := builder(nil, nil) require.NoError(t, err) comm := ctesting.NewContextComm(context.Background()) diff --git a/x-pack/elastic-agent/pkg/composable/providers/host/host.go b/x-pack/elastic-agent/pkg/composable/providers/host/host.go index 2b15c470bfc..b8971adb477 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/host/host.go +++ b/x-pack/elastic-agent/pkg/composable/providers/host/host.go @@ -79,13 +79,9 @@ func (c *contextProvider) Run(comm composable.ContextProviderComm) error { } // ContextProviderBuilder builds the context provider. -func ContextProviderBuilder(c *config.Config) (composable.ContextProvider, error) { - logger, err := logger.New("composable.providers.host") - if err != nil { - return nil, err - } +func ContextProviderBuilder(log *logger.Logger, c *config.Config) (composable.ContextProvider, error) { p := &contextProvider{ - logger: logger, + logger: log, fetcher: getHostInfo, } if c != nil { diff --git a/x-pack/elastic-agent/pkg/composable/providers/host/host_test.go b/x-pack/elastic-agent/pkg/composable/providers/host/host_test.go index a60bea63344..c2e467c5b04 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/host/host_test.go +++ b/x-pack/elastic-agent/pkg/composable/providers/host/host_test.go @@ -7,8 +7,6 @@ package host import ( "context" - "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" - "sync" "testing" "time" @@ -18,6 +16,8 @@ import ( "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/composable" ctesting "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/composable/testing" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" ) func TestContextProvider(t *testing.T) { @@ -31,7 +31,9 @@ func TestContextProvider(t *testing.T) { }) require.NoError(t, err) builder, _ := composable.Providers.GetContextProvider("host") - provider, err := builder(c) + log, err := logger.New("host_test") + require.NoError(t, err) + provider, err := builder(log, c) require.NoError(t, err) hostProvider := provider.(*contextProvider) diff --git a/x-pack/elastic-agent/pkg/composable/providers/local/local.go b/x-pack/elastic-agent/pkg/composable/providers/local/local.go index ded3f57b883..62fd2b65480 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/local/local.go +++ b/x-pack/elastic-agent/pkg/composable/providers/local/local.go @@ -10,6 +10,7 @@ import ( "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/composable" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" ) func init() { @@ -30,7 +31,7 @@ func (c *contextProvider) Run(comm composable.ContextProviderComm) error { } // ContextProviderBuilder builds the context provider. -func ContextProviderBuilder(c *config.Config) (composable.ContextProvider, error) { +func ContextProviderBuilder(_ *logger.Logger, c *config.Config) (composable.ContextProvider, error) { p := &contextProvider{} if c != nil { err := c.Unpack(p) diff --git a/x-pack/elastic-agent/pkg/composable/providers/local/local_test.go b/x-pack/elastic-agent/pkg/composable/providers/local/local_test.go index 1a175178c4d..a73eb5cb6a8 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/local/local_test.go +++ b/x-pack/elastic-agent/pkg/composable/providers/local/local_test.go @@ -26,7 +26,7 @@ func TestContextProvider(t *testing.T) { }) require.NoError(t, err) builder, _ := composable.Providers.GetContextProvider("local") - provider, err := builder(cfg) + provider, err := builder(nil, cfg) require.NoError(t, err) comm := ctesting.NewContextComm(context.Background()) diff --git a/x-pack/elastic-agent/pkg/composable/providers/localdynamic/localdynamic.go b/x-pack/elastic-agent/pkg/composable/providers/localdynamic/localdynamic.go index 0e5297a9e10..240b7251aa0 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/localdynamic/localdynamic.go +++ b/x-pack/elastic-agent/pkg/composable/providers/localdynamic/localdynamic.go @@ -11,6 +11,7 @@ import ( "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/composable" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" ) func init() { @@ -37,7 +38,7 @@ func (c *dynamicProvider) Run(comm composable.DynamicProviderComm) error { } // DynamicProviderBuilder builds the dynamic provider. -func DynamicProviderBuilder(c *config.Config) (composable.DynamicProvider, error) { +func DynamicProviderBuilder(_ *logger.Logger, c *config.Config) (composable.DynamicProvider, error) { p := &dynamicProvider{} if c != nil { err := c.Unpack(p) diff --git a/x-pack/elastic-agent/pkg/composable/providers/localdynamic/localdynamic_test.go b/x-pack/elastic-agent/pkg/composable/providers/localdynamic/localdynamic_test.go index 79c107372e3..9e8bc7a394c 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/localdynamic/localdynamic_test.go +++ b/x-pack/elastic-agent/pkg/composable/providers/localdynamic/localdynamic_test.go @@ -60,7 +60,7 @@ func TestContextProvider(t *testing.T) { }) require.NoError(t, err) builder, _ := composable.Providers.GetDynamicProvider("local_dynamic") - provider, err := builder(cfg) + provider, err := builder(nil, cfg) require.NoError(t, err) comm := ctesting.NewDynamicComm(context.Background()) diff --git a/x-pack/elastic-agent/pkg/composable/providers/path/path.go b/x-pack/elastic-agent/pkg/composable/providers/path/path.go index 41f18b91deb..04e80a9ef41 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/path/path.go +++ b/x-pack/elastic-agent/pkg/composable/providers/path/path.go @@ -9,6 +9,7 @@ import ( "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/composable" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" ) func init() { @@ -32,6 +33,6 @@ func (*contextProvider) Run(comm composable.ContextProviderComm) error { } // ContextProviderBuilder builds the context provider. -func ContextProviderBuilder(_ *config.Config) (composable.ContextProvider, error) { +func ContextProviderBuilder(_ *logger.Logger, _ *config.Config) (composable.ContextProvider, error) { return &contextProvider{}, nil } diff --git a/x-pack/elastic-agent/pkg/composable/providers/path/path_test.go b/x-pack/elastic-agent/pkg/composable/providers/path/path_test.go index 46d5006cebd..662b693d8a0 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/path/path_test.go +++ b/x-pack/elastic-agent/pkg/composable/providers/path/path_test.go @@ -18,7 +18,7 @@ import ( func TestContextProvider(t *testing.T) { builder, _ := composable.Providers.GetContextProvider("path") - provider, err := builder(nil) + provider, err := builder(nil, nil) require.NoError(t, err) comm := ctesting.NewContextComm(context.Background())