Skip to content

Commit

Permalink
log: change log level to reduce the noise logs
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
chlins committed Aug 10, 2023
1 parent 661c762 commit d6dadd8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
45 changes: 33 additions & 12 deletions src/cmd/exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
7 changes: 3 additions & 4 deletions src/core/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions src/lib/cache/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion src/pkg/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/server/registry/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d6dadd8

Please sign in to comment.