Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Injectable logging implementation #3204

Merged
merged 1 commit into from
Mar 5, 2021

Conversation

anderseknert
Copy link
Member

Refactor logging to allow providing custom logging implementations to plugin
manager. This should allow us to keep logging as it is when running OPA as a
server, while injecting noop-loggers or custom, provided loggers for SDK client
implementations.

Fixes #3180

Signed-off-by: Anders Eknert anders@eknert.com

@anderseknert
Copy link
Member Author

Honestly not sure what to do with regards to tests. Obviously fixed all tests where this would cause problems, but should we test logging itself somehow? I've just started the server with and without this to and verified it looks the same.

Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got some questions around adjusting the log level, see inline comments please :)

download/download.go Outdated Show resolved Hide resolved
sdk/logging.go Outdated Show resolved Hide resolved
sdk/logging.go Outdated Show resolved Hide resolved
sdk/logging.go Outdated Show resolved Hide resolved
plugins/status/plugin.go Show resolved Hide resolved
Copy link
Member

@tsandall tsandall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple minor comments.

@@ -47,10 +48,12 @@ type Plugin struct {
// New returns a new Plugin with the given config.
func New(parsedConfig *Config, manager *plugins.Manager) *Plugin {
initialStatus := map[string]*Status{}
loggers := make(map[string]sdk.Logger, len(parsedConfig.Bundles))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid instantiating multiple loggers here? We're going to run into issues if the plugin gets reconfigured to download different bundles. We could address that by instantiating new loggers on reconfiguration, but I don't think that's worth it.

A single logger that allows a set of key-value pairs to be passed to the log entry would be enough here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@@ -41,6 +43,7 @@ func TestPluginOneShot(t *testing.T) {
bundleName := "test-bundle"
plugin.status[bundleName] = &Status{Name: bundleName, Metrics: metrics.New()}
plugin.downloaders[bundleName] = download.New(download.Config{}, plugin.manager.Client(""), bundleName)
plugin.loggers[bundleName] = sdk.NewStandardLogger()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Having to instantiate loggers everywhere the plugin is intantiated is a suboptimal. If we exposed logging functions that accepted logger parameters, we could check for nil in the logging function and avoid this kind of thing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this would be avoided if these tests instantiated via the New constructor.. but 116da7809afbed2344a7c13bb8aa440eea928203 removes the need for this even if constructed this way.

)

// Level log level for Logger
type Level uint8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

srenatus
srenatus previously approved these changes Mar 5, 2021
Refactor logging to allow providing custom logging implementations to plugin
manager. This should allow us to keep logging as it is when running OPA as a
server, while injecting noop-loggers or custom, provided loggers for SDK client
implementations.

Fixes open-policy-agent#3180

Signed-off-by: Anders Eknert <anders@eknert.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sdk: allow better control of logging when integrating OPA as a library
4 participants