Skip to content

Commit

Permalink
Fix loggers in composable module. (#21047) (#21053)
Browse files Browse the repository at this point in the history
(cherry picked from commit 5ebf4bf)
  • Loading branch information
blakerouse authored Sep 10, 2020
1 parent a914041 commit de26878
Show file tree
Hide file tree
Showing 20 changed files with 40 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion x-pack/elastic-agent/pkg/agent/application/local_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion x-pack/elastic-agent/pkg/agent/application/managed_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion x-pack/elastic-agent/pkg/composable/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
12 changes: 5 additions & 7 deletions x-pack/elastic-agent/pkg/composable/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"

"reflect"
"sort"
Expand Down Expand Up @@ -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
Expand All @@ -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))
}
Expand All @@ -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))
}
Expand Down
6 changes: 5 additions & 1 deletion x-pack/elastic-agent/pkg/composable/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion x-pack/elastic-agent/pkg/composable/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion x-pack/elastic-agent/pkg/composable/providers/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion x-pack/elastic-agent/pkg/composable/providers/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
8 changes: 2 additions & 6 deletions x-pack/elastic-agent/pkg/composable/providers/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ package host
import (
"context"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config"

"sync"
"testing"
"time"
Expand All @@ -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) {
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion x-pack/elastic-agent/pkg/composable/providers/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion x-pack/elastic-agent/pkg/composable/providers/path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit de26878

Please sign in to comment.