From c524b4e88ba7c7dec0157677dcb4bffa0d5f52b5 Mon Sep 17 00:00:00 2001 From: Roman Glushko Date: Sat, 23 Dec 2023 23:45:48 +0200 Subject: [PATCH] #14: Configured hertz logger to obey the general logging config --- Makefile | 2 -- go.mod | 1 + go.sum | 2 ++ pkg/api/http/server.go | 6 ++++-- pkg/gateway.go | 6 +++--- pkg/telemetry/logging.go | 42 +++++++++++++++++++++++++++++++++++++- pkg/telemetry/telemetry.go | 10 ++++----- 7 files changed, 55 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 5db2a78a..af0a64ac 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,6 @@ help: install-checkers: ## Install static checkers @echo "๐Ÿšš Downloading binaries.." @GOBIN=$(CHECKER_BIN) go install mvdan.cc/gofumpt@latest - @GOBIN=$(CHECKER_BIN) go install go.uber.org/nilaway/cmd/nilaway@latest @GOBIN=$(CHECKER_BIN) go install golang.org/x/vuln/cmd/govulncheck@latest @GOBIN=$(CHECKER_BIN) go install github.com/securego/gosec/v2/cmd/gosec@latest @@ -33,7 +32,6 @@ static-checks: install-checkers ## Static Analysis @echo "๐Ÿงน GoCI Lint.." @golangci-lint run ./... @echo "๐Ÿงน Nilaway.." - @$(CHECKER_BIN)/nilaway ./... vuln: install-checkers ## Check for vulnerabilities @echo "๐Ÿ” Checking for vulnerabilities" diff --git a/go.mod b/go.mod index 395794df..0d59fa3d 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21.5 require ( github.com/cloudwego/hertz v0.7.3 github.com/go-playground/validator/v10 v10.16.0 + github.com/hertz-contrib/logger/zap v1.1.0 github.com/spf13/cobra v1.8.0 go.uber.org/goleak v1.3.0 go.uber.org/multierr v1.11.0 diff --git a/go.sum b/go.sum index 6b1a2835..e8bc9817 100644 --- a/go.sum +++ b/go.sum @@ -43,6 +43,8 @@ github.com/henrylee2cn/ameda v1.4.10 h1:JdvI2Ekq7tapdPsuhrc4CaFiqw6QXFvZIULWJgQy github.com/henrylee2cn/ameda v1.4.10/go.mod h1:liZulR8DgHxdK+MEwvZIylGnmcjzQ6N6f2PlWe7nEO4= github.com/henrylee2cn/goutil v0.0.0-20210127050712-89660552f6f8 h1:yE9ULgp02BhYIrO6sdV/FPe0xQM6fNHkVQW2IAymfM0= github.com/henrylee2cn/goutil v0.0.0-20210127050712-89660552f6f8/go.mod h1:Nhe/DM3671a5udlv2AdV2ni/MZzgfv2qrPL5nIi3EGQ= +github.com/hertz-contrib/logger/zap v1.1.0 h1:4efINiIDJrXEtAFeEdDJvc3Hye0VFxp+0X4BwaZgxNs= +github.com/hertz-contrib/logger/zap v1.1.0/go.mod h1:D/rJJgsYn+SGaHVfVqWS3vHTbbc7ODAlJO+6smWgTeE= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= diff --git a/pkg/api/http/server.go b/pkg/api/http/server.go index cb76a32d..6db53efb 100644 --- a/pkg/api/http/server.go +++ b/pkg/api/http/server.go @@ -21,10 +21,12 @@ type Server struct { } func NewServer(config *ServerConfig, tel *telemetry.Telemetry, router *pools.Router) (*Server, error) { + srv := config.ToServer() + return &Server{ telemetry: tel, router: router, - server: config.ToServer(), + server: srv, }, nil } @@ -39,7 +41,7 @@ func (srv *Server) Run() error { func (srv *Server) Shutdown(_ context.Context) error { exitWaitTime := srv.server.GetOptions().ExitWaitTimeout - srv.telemetry.Logger().Info( + srv.telemetry.Logger.Info( fmt.Sprintf("Begin graceful shutdown, wait at most %d seconds...", exitWaitTime/time.Second), ) diff --git a/pkg/gateway.go b/pkg/gateway.go index 94c6f03c..f8d801a3 100644 --- a/pkg/gateway.go +++ b/pkg/gateway.go @@ -73,13 +73,13 @@ LOOP: select { // TODO: Watch for config updates case sig := <-gw.signalC: - gw.telemetry.Logger().Info("Received signal from OS", zap.String("signal", sig.String())) + gw.telemetry.Logger.Info("Received signal from OS", zap.String("signal", sig.String())) break LOOP case <-gw.shutdownC: - gw.telemetry.Logger().Info("received shutdown request") + gw.telemetry.Logger.Info("received shutdown request") break LOOP case <-ctx.Done(): - gw.telemetry.Logger().Info("context done, terminating process") + gw.telemetry.Logger.Info("context done, terminating process") // Call shutdown with background context as the passed in context has been canceled return gw.shutdown(context.Background()) } diff --git a/pkg/telemetry/logging.go b/pkg/telemetry/logging.go index df4b169b..cea041b5 100644 --- a/pkg/telemetry/logging.go +++ b/pkg/telemetry/logging.go @@ -1,6 +1,8 @@ package telemetry import ( + "github.com/cloudwego/hertz/pkg/common/hlog" + hertzzap "github.com/hertz-contrib/logger/zap" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) @@ -57,8 +59,46 @@ func (c *LogConfig) ToZapConfig() *zap.Config { return &zapConfig } +func NewHertzLogger(zapConfig *zap.Config) (*hertzzap.Logger, error) { + // Both hertzzap and zap have a set of private methods that prevents from leveraging + // their native encoder & sink building functionality + // We had to copy & paste some of those to get it working + + var encoder zapcore.Encoder + + if zapConfig.Encoding == "console" { + encoder = zapcore.NewConsoleEncoder(zapConfig.EncoderConfig) + } else { + encoder = zapcore.NewJSONEncoder(zapConfig.EncoderConfig) + } + + sink, _, err := zap.Open(zapConfig.OutputPaths...) + if err != nil { + return nil, err + } + + return hertzzap.NewLogger( + hertzzap.WithCoreEnc(encoder), + hertzzap.WithCoreWs(sink), + hertzzap.WithCoreLevel(zapConfig.Level), + hertzzap.WithZapOptions(zap.AddCallerSkip(3)), + ), nil +} + func NewLogger(cfg *LogConfig) (*zap.Logger, error) { zapConfig := cfg.ToZapConfig() - return zapConfig.Build() + logger, err := zapConfig.Build() + if err != nil { + return nil, err + } + + hertzLogger, err := NewHertzLogger(zapConfig) + if err != nil { + return nil, err + } + + hlog.SetLogger(hertzLogger) + + return logger, nil } diff --git a/pkg/telemetry/telemetry.go b/pkg/telemetry/telemetry.go index 2e61706f..2e6b0bb9 100644 --- a/pkg/telemetry/telemetry.go +++ b/pkg/telemetry/telemetry.go @@ -8,7 +8,8 @@ type Config struct { } type Telemetry struct { - logger *zap.Logger + Config *Config + Logger *zap.Logger // TODO: add OTEL meter, tracer } @@ -19,10 +20,7 @@ func NewTelemetry(cfg *Config) (*Telemetry, error) { } return &Telemetry{ - logger: logger, + Config: cfg, + Logger: logger, }, nil } - -func (t *Telemetry) Logger() *zap.Logger { - return t.logger -}