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

Adding logs with uber/zap #185

Merged
merged 16 commits into from
Apr 8, 2024
6 changes: 5 additions & 1 deletion controllers/ibpca/ibpca_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/IBM-Blockchain/fabric-operator/pkg/util"
"github.com/go-test/deep"
"github.com/pkg/errors"
"go.uber.org/zap"
ctrl "sigs.k8s.io/controller-runtime"
yaml "sigs.k8s.io/yaml"

Expand Down Expand Up @@ -192,7 +193,10 @@ type ReconcileIBPCA struct {
func (r *ReconcileIBPCA) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
var err error

reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger := r.Config.Logger.With(
zap.String("Request.Namespace", request.Namespace),
zap.String("Request.Name", request.Name),
)

// If ca-restart-config configmap is the object being reconciled, reconcile the
// restart configmap.
Expand Down
4 changes: 4 additions & 0 deletions controllers/ibpca/ibpca_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sync"

current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
opconfig "github.com/IBM-Blockchain/fabric-operator/operatorconfig"
v1 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/ca/v1"
"github.com/IBM-Blockchain/fabric-operator/pkg/offering/common"
"github.com/IBM-Blockchain/fabric-operator/pkg/util"
Expand Down Expand Up @@ -116,7 +117,10 @@ var _ = Describe("ReconcileIBPCA", func() {
scheme: &runtime.Scheme{},
update: map[string][]Update{},
mutex: &sync.Mutex{},
Config: &opconfig.Config{},
}
zaplogger, _ := util.SetupLogging("DEBUG")
reconciler.Config.Logger = zaplogger
request = reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: "test-namespace",
Expand Down
6 changes: 5 additions & 1 deletion controllers/ibpconsole/ibpconsole_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/IBM-Blockchain/fabric-operator/pkg/operatorerrors"
"github.com/IBM-Blockchain/fabric-operator/pkg/util"
"github.com/pkg/errors"
"go.uber.org/zap"
"gopkg.in/yaml.v2"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -153,7 +154,10 @@ type ReconcileIBPConsole struct {
func (r *ReconcileIBPConsole) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
var err error

reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger := r.Config.Logger.With(
zap.String("Request.Namespace", request.Namespace),
zap.String("Request.Name", request.Name),
)
reqLogger.Info(fmt.Sprintf("Reconciling IBPConsole with update values of [ %+v ]", r.update.GetUpdateStackWithTrues()))

// Fetch the IBPConsole instance
Expand Down
10 changes: 6 additions & 4 deletions controllers/ibpconsole/ibpconsole_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ import (
"context"
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/pkg/errors"

current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
consolemocks "github.com/IBM-Blockchain/fabric-operator/controllers/ibpconsole/mocks"
"github.com/IBM-Blockchain/fabric-operator/controllers/mocks"
config "github.com/IBM-Blockchain/fabric-operator/operatorconfig"
"github.com/IBM-Blockchain/fabric-operator/pkg/offering/common"
"github.com/IBM-Blockchain/fabric-operator/pkg/operatorerrors"
"github.com/IBM-Blockchain/fabric-operator/pkg/util"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
k8serror "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -118,6 +118,8 @@ var _ = Describe("ReconcileIBPConsole", func() {
client: mockKubeClient,
scheme: &runtime.Scheme{},
}
zaplogger, _ := util.SetupLogging("DEBUG")
reconciler.Config.Logger = zaplogger
request = reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: "test-namespace",
Expand Down
7 changes: 5 additions & 2 deletions controllers/ibporderer/ibporderer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/IBM-Blockchain/fabric-operator/pkg/util"
"github.com/IBM-Blockchain/fabric-operator/version"
"github.com/pkg/errors"
"go.uber.org/zap"
yaml "sigs.k8s.io/yaml"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -183,8 +184,10 @@ type ReconcileIBPOrderer struct {
func (r *ReconcileIBPOrderer) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
var err error

reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)

reqLogger := r.Config.Logger.With(
zap.String("Request.Namespace", request.Namespace),
zap.String("Request.Name", request.Name),
)
// If orderer-restart-config configmap is the object being reconciled, reconcile the
// restart configmap.
if request.Name == "orderer-restart-config" {
Expand Down
5 changes: 5 additions & 0 deletions controllers/ibporderer/ibporderer_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ import (
current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
orderermocks "github.com/IBM-Blockchain/fabric-operator/controllers/ibporderer/mocks"
"github.com/IBM-Blockchain/fabric-operator/controllers/mocks"
opconfig "github.com/IBM-Blockchain/fabric-operator/operatorconfig"
v1 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/orderer/v1"
config "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/orderer/config/v1"
"github.com/IBM-Blockchain/fabric-operator/pkg/offering/common"
"github.com/IBM-Blockchain/fabric-operator/pkg/operatorerrors"
"github.com/IBM-Blockchain/fabric-operator/pkg/util"
corev1 "k8s.io/api/core/v1"
k8serror "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -122,7 +124,10 @@ var _ = Describe("ReconcileIBPOrderer", func() {
scheme: &runtime.Scheme{},
update: map[string][]Update{},
mutex: &sync.Mutex{},
Config: &opconfig.Config{},
}
zaplogger, _ := util.SetupLogging("DEBUG")
reconciler.Config.Logger = zaplogger
request = reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: "test-namespace",
Expand Down
7 changes: 5 additions & 2 deletions controllers/ibppeer/ibppeer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/IBM-Blockchain/fabric-operator/pkg/util"
"github.com/IBM-Blockchain/fabric-operator/version"
"github.com/pkg/errors"
"go.uber.org/zap"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
yaml "sigs.k8s.io/yaml"

Expand Down Expand Up @@ -203,8 +204,10 @@ type ReconcileIBPPeer struct {
func (r *ReconcileIBPPeer) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
var err error

reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)

reqLogger := r.Config.Logger.With(
zap.String("Request.Namespace", request.Namespace),
zap.String("Request.Name", request.Name),
)
// If peer-restart-config configmap is the object being reconciled, reconcile the
// restart configmap.
if request.Name == "peer-restart-config" {
Expand Down
5 changes: 5 additions & 0 deletions controllers/ibppeer/ibppeer_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"
"sync"

opconfig "github.com/IBM-Blockchain/fabric-operator/operatorconfig"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/utils/pointer"
Expand All @@ -33,6 +34,7 @@ import (
"github.com/IBM-Blockchain/fabric-operator/controllers/mocks"
"github.com/IBM-Blockchain/fabric-operator/pkg/offering/common"
"github.com/IBM-Blockchain/fabric-operator/pkg/operatorerrors"
"github.com/IBM-Blockchain/fabric-operator/pkg/util"
corev1 "k8s.io/api/core/v1"
k8serror "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -125,7 +127,10 @@ var _ = Describe("ReconcileIBPPeer", func() {
scheme: &runtime.Scheme{},
update: map[string][]Update{},
mutex: &sync.Mutex{},
Config: &opconfig.Config{},
}
zaplogger, _ := util.SetupLogging("DEBUG")
reconciler.Config.Logger = zaplogger
request = reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: "test-namespace",
Expand Down
14 changes: 5 additions & 9 deletions integration/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package integration
import (
"context"
"fmt"
"os"
"path/filepath"

config "github.com/IBM-Blockchain/fabric-operator/operatorconfig"
Expand All @@ -31,21 +30,18 @@ import (
cainit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/ca"
ordererinit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/orderer"
peerinit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/peer"
uzap "go.uber.org/zap"
"github.com/IBM-Blockchain/fabric-operator/pkg/util"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

// GetOperatorConfig returns the operator configuration with the default templating files population
// and with default versions set for components.
func GetOperatorConfig(configs, caFiles, peerFiles, ordererFiles, consoleFiles string) *config.Config {
ulevel := uzap.NewAtomicLevelAt(2)
if os.Getenv("LOG_LEVEL") == "debug" {
ulevel = uzap.NewAtomicLevelAt(-1)
zaplogger, err := util.SetupLogging("DEBUG")
if err != nil {
fmt.Print("error initiating the logger", err)
Comment on lines +42 to +43
Copy link
Contributor

Choose a reason for hiding this comment

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

we might want to fallback to other logger instead of setting nil in 118 line

}
level := zap.Level(&ulevel)
logger := zap.New(zap.Opts(level))

cfg := &config.Config{
CAInitConfig: &cainit.Config{
Expand Down Expand Up @@ -119,7 +115,7 @@ func GetOperatorConfig(configs, caFiles, peerFiles, ordererFiles, consoleFiles s
NetworkPolicyIngressFile: filepath.Join(consoleFiles, "networkpolicy-ingress.yaml"),
NetworkPolicyDenyAllFile: filepath.Join(consoleFiles, "networkpolicy-denyall.yaml"),
},
Logger: &logger,
Logger: zaplogger,
Operator: config.Operator{
Restart: config.Restart{
Timeout: common.MustParseDuration("5m"),
Expand Down
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package main

import (
"fmt"
"path/filepath"
"time"

Expand All @@ -27,6 +28,7 @@ import (
cainit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/ca"
ordererinit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/orderer"
peerinit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/peer"
"github.com/IBM-Blockchain/fabric-operator/pkg/util"

logf "sigs.k8s.io/controller-runtime/pkg/log"

Expand Down Expand Up @@ -71,7 +73,11 @@ func main() {
setDefaultConsoleDefinitions(operatorCfg)

operatorCfg.Operator.SetDefaults()

zaplogger, err := util.SetupLogging("DEBUG")
if err != nil {
fmt.Print("error initiating the logger", err)
}
operatorCfg.Logger = zaplogger
if err := command.Operator(operatorCfg); err != nil {
log.Error(err, "failed to start operator")
time.Sleep(15 * time.Second)
Expand Down
4 changes: 2 additions & 2 deletions operatorconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
ordererinit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/orderer"
peerinit "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/peer"
"github.com/IBM-Blockchain/fabric-operator/pkg/offering"
"github.com/go-logr/logr"
"go.uber.org/zap"
)

type Config struct {
Expand All @@ -33,7 +33,7 @@ type Config struct {
ConsoleInitConfig *ConsoleConfig
Offering offering.Type
Operator Operator
Logger *logr.Logger
Logger *zap.Logger
}

type ConsoleConfig struct {
Expand Down
19 changes: 17 additions & 2 deletions pkg/command/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import (
"runtime"
"time"

"go.uber.org/zap/zapcore"
k8sruntime "k8s.io/apimachinery/pkg/runtime"

"github.com/go-logr/zapr"
routev1 "github.com/openshift/api/route/v1"
"github.com/operator-framework/operator-lib/leader"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand All @@ -49,6 +51,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"sigs.k8s.io/controller-runtime/pkg/client"

uberzap "go.uber.org/zap"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
)
Expand Down Expand Up @@ -102,8 +105,20 @@ func OperatorWithSignal(operatorCfg *oconfig.Config, signalHandler context.Conte
// be propagated through the whole operator, generating
// uniform and structured logs.
if operatorCfg.Logger != nil {
logf.SetLogger(*operatorCfg.Logger)
ctrl.SetLogger(*operatorCfg.Logger)

config := uberzap.NewProductionConfig()
config.EncoderConfig.TimeKey = "timestamp"
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
logger, err := config.Build()
if err != nil {
return err
}

// Wrap the zap.Logger with go-logr/zapr to satisfy the logr.Logger interface
log := zapr.NewLogger(logger)

logf.SetLogger(log)
ctrl.SetLogger(log)
} else {
// Use the unstructured log formatter when running locally.
logf.SetLogger(zap.New(zap.UseDevMode(local)))
Expand Down
23 changes: 23 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import (
"github.com/IBM-Blockchain/fabric-operator/pkg/k8s/clientset"
routev1 "github.com/openshift/api/route/v1"
"github.com/pkg/errors"
uberzap "go.uber.org/zap"
"go.uber.org/zap/zapcore"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
Expand Down Expand Up @@ -942,3 +944,24 @@ func GetServerVersion() (*version.Info, error) {
}
return version, nil
}

func SetupLogging(loglevel string) (*uberzap.Logger, error) {
// set up logging
var level zapcore.Level
err := level.Set(loglevel)
if err != nil {
return nil, err
}
zapConfig := uberzap.NewProductionConfig()
zapConfig.Level = uberzap.NewAtomicLevelAt(level)
zapConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
logger, err := zapConfig.Build()
defer logger.Sync()
if err != nil {
return nil, err
}
// redirect uses of standard logger
uberzap.RedirectStdLog(logger)

return logger, nil
}
Loading