Auth: Logger for user remote cache (#59961)

add logger
This commit is contained in:
Ieva
2022-12-07 09:58:57 -05:00
committed by GitHub
parent 95d2adf251
commit 1b29543b64
+16 -2
View File
@@ -5,6 +5,7 @@ import (
"errors"
"time"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/remotecache"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/accesscontrol"
@@ -37,6 +38,7 @@ type Service struct {
accessControlStore accesscontrol.Service
remoteCache *remotecache.RemoteCache
features *featuremgmt.FeatureManager
logger log.Logger
// TODO remove sqlstore
sqlStore *sqlstore.SQLStore
@@ -79,6 +81,7 @@ func ProvideService(
sqlStore: ss,
remoteCache: remoteCache,
features: features,
logger: log.New("user.service"),
}
}
@@ -323,11 +326,19 @@ func (s *Service) SetUsingOrg(ctx context.Context, cmd *user.SetUsingOrgCommand)
func (s *Service) GetSignedInUserWithCacheCtx(ctx context.Context, query *user.GetSignedInUserQuery) (*user.SignedInUser, error) {
// Fetching from remote cache first otherwise fallback to in memory cache
cacheKey := sqlstore.NewSignedInUserCacheKey(query.OrgID, query.UserID)
if s.features.IsEnabled(featuremgmt.FlagUserRemoteCache) {
if query.OrgID > 0 && query.UserID > 0 && s.features.IsEnabled(featuremgmt.FlagUserRemoteCache) {
res, errCache := s.remoteCache.Get(ctx, cacheKey)
if errCache == nil {
if signedInUser, ok := res.(user.SignedInUser); ok {
return &signedInUser, nil
} else {
if errors.Is(errCache, remotecache.ErrCacheItemNotFound) {
s.logger.Debug("user not found in cache",
"cacheKey", cacheKey)
} else {
s.logger.Warn("failed to get user from cache",
"cacheKey", cacheKey, "error", errCache)
}
}
}
}
@@ -345,7 +356,10 @@ func (s *Service) GetSignedInUserWithCacheCtx(ctx context.Context, query *user.G
// Remember user in remote cache
if s.features.IsEnabled(featuremgmt.FlagUserRemoteCache) {
_ = s.remoteCache.Set(ctx, cacheKey, *(q.Result), time.Second*5)
if errCache := s.remoteCache.Set(ctx, cacheKey, *(q.Result), time.Second*5); errCache != nil {
s.logger.Warn("could not cache user in remote cache",
"cacheKey", cacheKey, "error", errCache)
}
}
return q.Result, nil
}