From 1b29543b64fa2a783f60beed26b889eeb65fce07 Mon Sep 17 00:00:00 2001 From: Ieva Date: Wed, 7 Dec 2022 14:58:57 +0000 Subject: [PATCH] Auth: Logger for user remote cache (#59961) add logger --- pkg/services/user/userimpl/user.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/pkg/services/user/userimpl/user.go b/pkg/services/user/userimpl/user.go index dbc0ca2315a..111aec0694b 100644 --- a/pkg/services/user/userimpl/user.go +++ b/pkg/services/user/userimpl/user.go @@ -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 }