-
Notifications
You must be signed in to change notification settings - Fork 92
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
Integration zerolog #367
Integration zerolog #367
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some minor changes/questions.
Otherwise, it is a really great change 👍
@somaritane @k0da PR is huge, another pair of reviewing eyes will not hurt here :)
c8ae3ee
to
a7ba5f0
Compare
In order to integrate a zerolog, I had create log package and take a few steps. logpackage cntains three components: **factory, singleton and adapter**. Instead of passing logger into reconciler, providers assistant etc. I created logger [singleton](https://en.wikipedia.org/wiki/Singleton_pattern). This component is accessible from anywhere and contains one and only one instance of logger which is accessible through reader function `Logger()` Singleton must be initialised after depresolver reads configuration and internally calls factory which builds logger instance. Thats why factory is completely private now. To inject logger into package do following: ```go import "github.com/AbsaOSS/k8gb/controllers/log" var logger = log.Logger() func MyFunc(){ logger.Info().Msg("hello from logger") } ``` ControllerManager still calls logr internally. Unfortunately the problem is that the second logger completely ignores the settings of our logger, so plain text would appear between json records, etc. It also ignores our formatting. The solution provides [**Adapter** pattern](https://en.wikipedia.org/wiki/Adapter_pattern) which basically allows us to encapsulate zerolog instance into logr interface. So on the outside it looks like logr but internally it produces records by our zerolog instance. Here is how we inject adapter: ```go ctrl.SetLogger(log.NewAdapter(logger)) ``` - I had to remove rlog nad zapr from all components - passing logger by arguments was removed, now simpler injection via singleton - logging zerolog way: ```go logger.Info().Msg("hello from zerolog") logger.Err(err).Msg("error from zerolog") logger.Info().Msgf("hello from zerolog %s", stringvariable) logger.Err(err).Msgf("error from zerolog %s", stringvariable) ``` - We have places where we print Info messages within errors. I replaced them by Warnings ```go IPs, err := utils.Dig(r.edgeDNSServer, lbHostname) if err != nil { logger.Warn().Msgf("Can't dig k8gb-coredns-lb service loadbalancer fqdn %s (%s)", lbHostname, err) return nil, err } ``` - identify places for debug messages - We don't need to print everything on info level, some info messages would go to debug - We dont't want to generate errors like this: ```shell github.com/go-logr/zapr.(*zapLogger).Error /Users/ab011th/go/pkg/mod/github.com/go-logr/zapr@v0.4.0/zapr.go:132 sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler /Users/ab011th/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.6.4/pkg/internal/controller/controller.go:246 sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem /Users/ab011th/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.6.4/pkg/internal/controller/controller.go:218 sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).worker /Users/ab011th/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.6.4/pkg/internal/controller/controller.go:197 k8s.io/apimachinery/pkg/util/wait.BackoffUntil.func1 /Users/ab011th/go/pkg/mod/k8s.io/apimachinery@v0.20.4/pkg/util/wait/wait.go:155 k8s.io/apimachinery/pkg/util/wait.BackoffUntil /Users/ab011th/go/pkg/mod/k8s.io/apimachinery@v0.20.4/pkg/util/wait/wait.go:156 k8s.io/apimachinery/pkg/util/wait.JitterUntil /Users/ab011th/go/pkg/mod/k8s.io/apimachinery@v0.20.4/pkg/util/wait/wait.go:133 k8s.io/apimachinery/pkg/util/wait.Until /Users/ab011th/go/pkg/mod/k8s.io/apimachinery@v0.20.4/pkg/util/wait/wait.go:90 ``` Instead we want to have simplified view in JSON. I'll need to rewrite all error producement into `github.com/pkg/errors`.
a7ba5f0
to
973a250
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great stuff 👍
Related to #331
In order to integrate a zerolog, I had to create log package and take a few steps.
Log package
log package contains three components: factory, singleton and adapter.
loger factory, singleton
Instead of injecting
*zerolog.Logger
into each object (providers, assistant, etc). I created logger singleton.This component is accessible from anywhere and contains one and only one instance of logger, which is accessible through reader function
Logger()
Singleton must be initialised after resolving configuration and internally calls factory, which builds logger instance.
That's why logger factory is completely private now.
To inject logger into package do following:
Adapter
ControllerManager still calls logr internally. It can't use anything else. Unfortunately, the problem is that the logger inside ControllerManager completely ignores the settings of our logger, so plain text would appear between json records, etc.
It also ignores our formatting. The solution provides Adapter pattern
which basically allows us to encapsulate zerolog instance into logr interface. So on the outside it looks like logr but
internally it produces records by our zerolog instance. That's why logger within ControllerManager will print the same, expected way as we do in the rest of the application. Here is how we inject adapter:
logging messages
Output
Controller manager producing log with zapr
Controller manager producing logs with Adapter in json
Controller manager producing logs with Adapter in simple
TODO
Instead, we want to have simplified view in JSON. I'll need to rewrite all error producing into
github.com/pkg/errors
.