Skip to content

Commit

Permalink
Plugins logger respect generic logger settings
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok committed Jul 14, 2023
1 parent a1c420d commit f930f57
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/botkube-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func run(ctx context.Context) error {

collector := plugin.NewCollector(logger)
enabledPluginExecutors, enabledPluginSources := collector.GetAllEnabledAndUsedPlugins(conf)
pluginManager := plugin.NewManager(logger, conf.Plugins, enabledPluginExecutors, enabledPluginSources)
pluginManager := plugin.NewManager(logger, conf.Settings.Log, conf.Plugins, enabledPluginExecutors, enabledPluginSources)

err = pluginManager.Start(ctx)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/plugin/index_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/xeipuuv/gojsonschema"

"github.com/kubeshop/botkube/pkg/api"
"github.com/kubeshop/botkube/pkg/config"
"github.com/kubeshop/botkube/pkg/multierror"
)

Expand Down Expand Up @@ -170,7 +171,7 @@ func (i *IndexBuilder) getPluginMetadata(dir string, bins []pluginBinariesIndex)
bins := map[string]string{
item.Type.String(): filepath.Join(dir, item.BinaryPath),
}
clients, err := createGRPCClients[metadataGetter](i.log, bins, item.Type)
clients, err := createGRPCClients[metadataGetter](i.log, config.Logger{}, bins, item.Type)
if err != nil {
return nil, fmt.Errorf("while creating gRPC client: %w", err)
}
Expand Down
6 changes: 4 additions & 2 deletions internal/plugin/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ var specialCharsPattern = regexp.MustCompile(`(?i:[^A-Z0-9_])`)
// - hashicorp client logger always has the configured log level
// - binary standard output is logged only if debug level is set, otherwise it is discarded
// - binary standard error is logged always on error level
func NewPluginLoggers(bkLogger logrus.FieldLogger, pluginKey string, pluginType Type) (hclog.Logger, io.Writer, io.Writer) {
func NewPluginLoggers(bkLogger logrus.FieldLogger, logConfig config.Logger, pluginKey string, pluginType Type) (hclog.Logger, io.Writer, io.Writer) {
pluginLogLevel := getPluginLogLevel(bkLogger, pluginKey, pluginType)

cfg := config.Logger{
Level: pluginLogLevel.String(),
Level: pluginLogLevel.String(),
DisableColors: logConfig.DisableColors,
Formatter: logConfig.Formatter,
}
log := loggerx.New(cfg).WithField("plugin", pluginKey)

Expand Down
12 changes: 7 additions & 5 deletions internal/plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var pluginMap = map[string]plugin.Plugin{
type Manager struct {
isStarted atomic.Bool
log logrus.FieldLogger
logConfig config.Logger
cfg config.PluginManagement
httpClient *http.Client

Expand All @@ -58,7 +59,7 @@ type Manager struct {
}

// NewManager returns a new Manager instance.
func NewManager(logger logrus.FieldLogger, cfg config.PluginManagement, executors, sources []string) *Manager {
func NewManager(logger logrus.FieldLogger, logCfg config.Logger, cfg config.PluginManagement, executors, sources []string) *Manager {
return &Manager{
cfg: cfg,
httpClient: httpx.NewHTTPClient(),
Expand All @@ -67,6 +68,7 @@ func NewManager(logger logrus.FieldLogger, cfg config.PluginManagement, executor
sourcesToEnable: sources,
sourcesStore: newStore[source.Source](),
log: logger.WithField("component", "Plugin Manager"),
logConfig: logCfg, // used when we create on-demand loggers for plugins
}
}

Expand Down Expand Up @@ -106,7 +108,7 @@ func (m *Manager) start(ctx context.Context, forceUpdate bool) error {
return err
}

executorClients, err := createGRPCClients[executor.Executor](m.log, executorPlugins, TypeExecutor)
executorClients, err := createGRPCClients[executor.Executor](m.log, m.logConfig, executorPlugins, TypeExecutor)
if err != nil {
return fmt.Errorf("while creating executor plugins: %w", err)
}
Expand All @@ -116,7 +118,7 @@ func (m *Manager) start(ctx context.Context, forceUpdate bool) error {
if err != nil {
return err
}
sourcesClients, err := createGRPCClients[source.Source](m.log, sourcesPlugins, TypeSource)
sourcesClients, err := createGRPCClients[source.Source](m.log, m.logConfig, sourcesPlugins, TypeSource)
if err != nil {
return fmt.Errorf("while creating source plugins: %w", err)
}
Expand Down Expand Up @@ -324,11 +326,11 @@ func (m *Manager) fetchIndex(ctx context.Context, path, url string) error {
return nil
}

func createGRPCClients[C any](logger logrus.FieldLogger, bins map[string]string, pluginType Type) (map[string]enabledPlugins[C], error) {
func createGRPCClients[C any](logger logrus.FieldLogger, logConfig config.Logger, bins map[string]string, pluginType Type) (map[string]enabledPlugins[C], error) {
out := map[string]enabledPlugins[C]{}

for key, path := range bins {
pluginLogger, stdoutLogger, stderrLogger := NewPluginLoggers(logger, key, pluginType)
pluginLogger, stdoutLogger, stderrLogger := NewPluginLoggers(logger, logConfig, key, pluginType)

cli := plugin.NewClient(&plugin.ClientConfig{
Plugins: pluginMap,
Expand Down
2 changes: 1 addition & 1 deletion internal/plugin/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestCollectEnabledRepositories(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// given
manager := NewManager(loggerx.NewNoop(), config.PluginManagement{
manager := NewManager(loggerx.NewNoop(), config.Logger{}, config.PluginManagement{
Repositories: tc.definedRepositories,
}, tc.enabledExecutors, tc.enabledSources)

Expand Down

0 comments on commit f930f57

Please sign in to comment.