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

Integration zerolog #367

Merged
merged 1 commit into from
Mar 15, 2021
Merged

Integration zerolog #367

merged 1 commit into from
Mar 15, 2021

Conversation

kuritka
Copy link
Collaborator

@kuritka kuritka commented Mar 15, 2021

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:

import "github.com/AbsaOSS/k8gb/controllers/log"

// injection
var logger = log.Logger()

func MyFunc(){
 logger.Info().Msg("hello from logger")
}

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:

// main.go
ctrl.SetLogger(log.NewAdapter(logger))
// controller manager has logger instance injected, and that's all. We don't need to do anything else.

logging messages

  • I had to remove rlog and zapr from all components
  • passing logger between components by arguments or structure fields was removed, now injection via singleton
  • The way how we use zerolog:
logger.Info().Msg("hello from zerolog")
logger.Err(err).Msg("error from zerolog")  // prints "error from zerolog" and error
logger.Info().Msgf("hello from zerolog %s", stringvariable)
logger.Err(err).Msgf("error from zerolog %s", stringvariable)
  • I made many replacements like this:
log.Info(fmt.Sprintf("%s annotation is missing, skipping Gslb creation...", primaryGeoTagAnnotation))
# I replaced with
logger.Info().Msgf("%s annotation is missing, skipping Gslb creation...", primaryGeoTagAnnotation)
  • We had places where we printed Info messages within errors. I replaced them by Warnings.
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
	}

Output

Screenshot 2021-03-12 at 11 38 35
Controller manager producing log with zapr

Screenshot 2021-03-12 at 11 39 09
Controller manager producing logs with Adapter in json
Screenshot 2021-03-12 at 11 40 03
Controller manager producing logs with Adapter in simple

TODO

  • identify places for debug messages
  • We don't need to print everything on info level, some info messages would go to debug
  • We don't want to generate errors like this:
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 producing into
github.com/pkg/errors.

Copy link
Member

@ytsarev ytsarev left a 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 :)

main.go Outdated Show resolved Hide resolved
main.go Outdated Show resolved Hide resolved
controllers/providers/assistant/gslb.go Outdated Show resolved Hide resolved
@kuritka kuritka force-pushed the logger-reconciliation branch 2 times, most recently from c8ae3ee to a7ba5f0 Compare March 15, 2021 15:59
@kuritka kuritka requested a review from ytsarev March 15, 2021 16:01
controllers/providers/assistant/gslb.go Outdated Show resolved Hide resolved
controllers/log/logr.go Outdated Show resolved Hide resolved
controllers/log/logr.go Outdated Show resolved Hide resolved
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`.
Copy link
Contributor

@somaritane somaritane left a comment

Choose a reason for hiding this comment

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

lgtm!

Copy link
Member

@ytsarev ytsarev left a comment

Choose a reason for hiding this comment

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

Great stuff 👍

@kuritka kuritka merged commit 440df35 into master Mar 15, 2021
@kuritka kuritka deleted the logger-reconciliation branch March 15, 2021 21:09
@somaritane somaritane linked an issue Mar 24, 2021 that may be closed by this pull request
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.

Logger Enhancements
3 participants