From cc3c100c62911ff9236b3a176ee0cea02f5fe5e9 Mon Sep 17 00:00:00 2001 From: Tit Petric Date: Tue, 6 Aug 2019 20:27:35 +0000 Subject: [PATCH] replace profiler with logger, update logger option to bool --- .env.example | 4 ++-- internal/db/connector.go | 16 +++++++++------- internal/db/logger.go | 32 ++++++++++++++++++++++++++++++++ internal/db/profiler.go | 36 ------------------------------------ pkg/cli/options/db.go | 4 ++-- 5 files changed, 45 insertions(+), 47 deletions(-) create mode 100644 internal/db/logger.go delete mode 100644 internal/db/profiler.go diff --git a/.env.example b/.env.example index 93dd9fc32..3f5995e81 100644 --- a/.env.example +++ b/.env.example @@ -41,8 +41,8 @@ MONITOR_INTERVAL=5min # Database to use DB_DSN=corteza:corteza@tcp(localhost:3306)/corteza?collation=utf8mb4_general_ci -# DB query profiler (none, stdout, logger) -#DB_PROFILER=none +# Log database queries? +#DB_LOGGER=false # Logging level we want to use (values: debug, info, warn, error, dpanic, panic, fatal) #LOG_LEVEL=info diff --git a/internal/db/connector.go b/internal/db/connector.go index a66f47c68..d63582a2b 100644 --- a/internal/db/connector.go +++ b/internal/db/connector.go @@ -8,6 +8,7 @@ import ( "github.com/getsentry/sentry-go" "github.com/pkg/errors" "github.com/titpetric/factory" + "github.com/titpetric/factory/logger" "go.uber.org/zap" "github.com/cortezaproject/corteza-server/pkg/cli/options" @@ -93,14 +94,15 @@ func TryToConnect(ctx context.Context, log *zap.Logger, name string, opt options return nil, errors.Errorf("db connection for %q cancelled", name) } - switch opt.Profiler { - case "stdout": - db.Profiler = &factory.Database.ProfilerStdout - case "logger": - // Skip 3 levels in call stack to get to the actual function used - db.Profiler = ZapProfiler(log. - WithOptions(zap.AddCallerSkip(3)), + if opt.Logger { + db.SetLogger( + NewZapLogger( + // Skip 3 levels in call stack to get to the actual function used + log.WithOptions(zap.AddCallerSkip(3)), + ), ) + } else { + db.SetLogger(logger.Silent{}) } if err != nil { diff --git a/internal/db/logger.go b/internal/db/logger.go new file mode 100644 index 000000000..a863fd9a6 --- /dev/null +++ b/internal/db/logger.go @@ -0,0 +1,32 @@ +package db + +import ( + "context" + + "github.com/titpetric/factory/logger" + "go.uber.org/zap" +) + +type ( + zapLogger struct { + logger *zap.Logger + } +) + +func NewZapLogger(logger *zap.Logger) *zapLogger { + return &zapLogger{ + logger: logger, + } +} + +func (z *zapLogger) Log(ctx context.Context, msg string, fields ...logger.Field) { + // @todo when factory.DatabaseProfilerContext gets access to context from + // db functions, try to extract RequestID with middleware.GetReqID() + + zapFields := []zap.Field{} + for _, v := range fields { + zapFields = append(zapFields, zap.Any(v.Name(), v.Value())) + } + + z.logger.Debug(msg, zapFields...) +} diff --git a/internal/db/profiler.go b/internal/db/profiler.go deleted file mode 100644 index ec48b0815..000000000 --- a/internal/db/profiler.go +++ /dev/null @@ -1,36 +0,0 @@ -package db - -import ( - "time" - - "github.com/titpetric/factory" - "go.uber.org/zap" -) - -// zapProfiler logs query statistics to zap.logger -type ( - zapProfiler struct { - logger *zap.Logger - } -) - -func ZapProfiler(logger *zap.Logger) *zapProfiler { - return &zapProfiler{ - logger: logger, - } -} - -// Post prints the query statistics to stdout -func (p zapProfiler) Post(c *factory.DatabaseProfilerContext) { - // @todo when factory.DatabaseProfilerContext gets access to context from - // db functions, try to extract RequestID with middleware.GetReqID() - - p.logger.Debug( - c.Query, - zap.Any("args", c.Args), - zap.Float64("duration", time.Since(c.Time).Seconds())) -} - -// Flush stdout (no-op for this profiler) -func (zapProfiler) Flush() { -} diff --git a/pkg/cli/options/db.go b/pkg/cli/options/db.go index 7fb3a805a..0e310dcf1 100644 --- a/pkg/cli/options/db.go +++ b/pkg/cli/options/db.go @@ -7,7 +7,7 @@ import ( type ( DBOpt struct { DSN string `env:"DB_DSN"` - Profiler string `env:"DB_PROFILER"` + Logger bool `env:"DB_LOGGER"` MaxTries int `env:"DB_MAX_TRIES"` Delay time.Duration `env:"DB_CONN_ERR_DELAY"` Timeout time.Duration `env:"DB_CONN_TIMEOUT"` @@ -17,7 +17,7 @@ type ( func DB(pfix string) (o *DBOpt) { o = &DBOpt{ DSN: "corteza:corteza@tcp(db:3306)/corteza?collation=utf8mb4_general_ci", - Profiler: "none", + Logger: false, MaxTries: 100, Delay: 5 * time.Second, Timeout: 1 * time.Minute,