From 85177732be30a5a1e6a44199faad1e28a0608fe4 Mon Sep 17 00:00:00 2001 From: chlins Date: Thu, 10 Aug 2023 11:45:40 +0800 Subject: [PATCH] log: change log level to reduce the noise logs 1. Change some logs level to reduce the noise. 2. Wrap the go-redis.Nil error as ErrNotFound to avoid confusing Signed-off-by: chlins --- src/cmd/exporter/main.go | 45 ++++++++++++++++++++++++--------- src/core/session/session.go | 7 +++-- src/lib/cache/redis/redis.go | 4 +++ src/pkg/notifier/notifier.go | 2 +- src/server/registry/manifest.go | 2 +- 5 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/cmd/exporter/main.go b/src/cmd/exporter/main.go index 0c5dfa9407e..400d5417876 100644 --- a/src/cmd/exporter/main.go +++ b/src/cmd/exporter/main.go @@ -36,16 +36,7 @@ func main() { viper.SetEnvPrefix("harbor") viper.AutomaticEnv() viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) - connMaxLifetime, err := time.ParseDuration(viper.GetString("database.conn_max_lifetime")) - if err != nil { - log.Errorf("Failed to parse database.conn_max_lifetime: %v", err) - connMaxLifetime = 5 * time.Minute - } - connMaxIdleTime, err := time.ParseDuration(viper.GetString("database.conn_max_idle_time")) - if err != nil { - log.Errorf("Failed to parse database.conn_max_idle_time: %v", err) - connMaxIdleTime = 0 - } + dbCfg := &models.Database{ Type: "postgresql", PostGreSQL: &models.PostGreSQL{ @@ -57,8 +48,8 @@ func main() { SSLMode: viper.GetString("database.sslmode"), MaxIdleConns: viper.GetInt("database.max_idle_conns"), MaxOpenConns: viper.GetInt("database.max_open_conns"), - ConnMaxLifetime: connMaxLifetime, - ConnMaxIdleTime: connMaxIdleTime, + ConnMaxLifetime: getConnMaxLifetime(viper.GetString("database.conn_max_lifetime")), + ConnMaxIdleTime: getConnMaxIdleTime(viper.GetString("database.conn_max_idle_time")), }, } if err := dao.InitDatabase(dbCfg); err != nil { @@ -109,3 +100,33 @@ func main() { os.Exit(1) } } + +func getConnMaxLifetime(duration string) time.Duration { + // set conn max life time, 5m by default + connMaxLifetime := 5 * time.Minute + if duration != "" { + maxLifetime, err := time.ParseDuration(duration) + if err == nil { + connMaxLifetime = maxLifetime + } else { + log.Warningf("Failed to parse database.conn_max_lifetime, use default value: %s, err: %v", connMaxLifetime, err) + } + } + + return connMaxLifetime +} + +func getConnMaxIdleTime(duration string) time.Duration { + // set conn max idle time, 0 by default + connMaxIdleTime := time.Duration(0) + if duration != "" { + maxIdleTime, err := time.ParseDuration(duration) + if err == nil { + connMaxIdleTime = maxIdleTime + } else { + log.Warningf("Failed to parse database.conn_max_idle_time, use default value: %s, err: %v", connMaxIdleTime, err) + } + } + + return connMaxIdleTime +} diff --git a/src/core/session/session.go b/src/core/session/session.go index 80d4b389c64..d76f8c01f78 100644 --- a/src/core/session/session.go +++ b/src/core/session/session.go @@ -16,13 +16,12 @@ package session import ( "context" + "errors" "net/http" - "strings" "sync" "time" "github.com/beego/beego/v2/server/web/session" - goredis "github.com/go-redis/redis/v8" "github.com/goharbor/harbor/src/lib/cache" "github.com/goharbor/harbor/src/lib/cache/redis" @@ -131,7 +130,7 @@ func (rp *Provider) SessionRead(ctx context.Context, sid string) (session.Store, ctx = context.TODO() } err := rp.c.Fetch(ctx, sid, &kv) - if err != nil && !strings.Contains(err.Error(), goredis.Nil.Error()) { + if err != nil && !errors.Is(err, cache.ErrNotFound) { return nil, err } @@ -166,7 +165,7 @@ func (rp *Provider) SessionRegenerate(ctx context.Context, oldsid, sid string) ( } else { kv := make(map[interface{}]interface{}) err := rp.c.Fetch(ctx, sid, &kv) - if err != nil && !strings.Contains(err.Error(), goredis.Nil.Error()) { + if err != nil && !errors.Is(err, cache.ErrNotFound) { return nil, err } diff --git a/src/lib/cache/redis/redis.go b/src/lib/cache/redis/redis.go index a72b05e9c87..80beb3b997c 100644 --- a/src/lib/cache/redis/redis.go +++ b/src/lib/cache/redis/redis.go @@ -58,6 +58,10 @@ func (c *Cache) Fetch(ctx context.Context, key string, value interface{}) error // convert internal or Timeout error to be ErrNotFound // so that the caller can continue working without breaking // return cache.ErrNotFound + if err == redis.Nil { + return cache.ErrNotFound + } + return fmt.Errorf("%w:%v", cache.ErrNotFound, err) } diff --git a/src/pkg/notifier/notifier.go b/src/pkg/notifier/notifier.go index 47293b9390b..b8f2a2c36c9 100644 --- a/src/pkg/notifier/notifier.go +++ b/src/pkg/notifier/notifier.go @@ -217,7 +217,7 @@ func (nw *NotificationWatcher) Notify(ctx context.Context, notification Notifica // Currently, we just log the error log.Errorf("Error occurred when triggering handler %s of topic %s: %s\n", reflect.TypeOf(hd).String(), notification.Topic, err.Error()) } else { - log.Infof("Handle notification with Handler '%s' on topic '%s': %+v\n", hd.Name(), notification.Topic, notification.Value) + log.Debugf("Handle notification with Handler '%s' on topic '%s': %+v\n", hd.Name(), notification.Topic, notification.Value) } }() }(h, handlerChan) diff --git a/src/server/registry/manifest.go b/src/server/registry/manifest.go index 39a26fab9ea..4693c3a7e65 100644 --- a/src/server/registry/manifest.go +++ b/src/server/registry/manifest.go @@ -93,7 +93,7 @@ func getManifest(w http.ResponseWriter, req *http.Request) { _, _ = buffer.Write(manifest) } } else { - log.Warningf("failed to get manifest from cache, error: %v", err) + log.Debugf("failed to get manifest from cache, will fallback to registry, error: %v", err) // only write cache when request is GET because HEAD request resp // body is empty. if req.Method == http.MethodGet {