Add method timings with conditional logging

- Add timing measurements to key methods
- Only log when duration exceeds 1 second
This commit is contained in:
pascal
2025-11-03 11:52:44 +01:00
parent a2313a5ba4
commit 9eb2faef7f
4 changed files with 25 additions and 0 deletions

View File

@@ -1304,6 +1304,7 @@ func (am *DefaultAccountManager) UpdateAccountOnboarding(ctx context.Context, ac
}
func (am *DefaultAccountManager) GetAccountIDFromUserAuth(ctx context.Context, userAuth nbcontext.UserAuth) (string, string, error) {
defer util.TimeTrack(ctx, "GetAccountIDFromUserAuth")()
if userAuth.UserId == "" {
return "", "", errors.New(emptyUserID)
}
@@ -1348,6 +1349,7 @@ func (am *DefaultAccountManager) GetAccountIDFromUserAuth(ctx context.Context, u
// and propagates changes to peers if group propagation is enabled.
// requires userAuth to have been ValidateAndParseToken and EnsureUserAccessByJWTGroups by the AuthManager
func (am *DefaultAccountManager) SyncUserJWTGroups(ctx context.Context, userAuth nbcontext.UserAuth) error {
defer util.TimeTrack(ctx, "SyncUserJWTGroups")()
if userAuth.IsChild || userAuth.IsPAT {
return nil
}

View File

@@ -575,6 +575,8 @@ func (s *SqlStore) GetUserByPATID(ctx context.Context, lockStrength LockingStren
}
func (s *SqlStore) GetUserByUserID(ctx context.Context, lockStrength LockingStrength, userID string) (*types.User, error) {
defer util.TimeTrack(ctx, "GetUserByUserID")()
ctx, cancel := getDebuggingCtx(ctx)
defer cancel()
@@ -1127,6 +1129,8 @@ func (s *SqlStore) GetAccountCreatedBy(ctx context.Context, lockStrength Locking
// SaveUserLastLogin stores the last login time for a user in DB.
func (s *SqlStore) SaveUserLastLogin(ctx context.Context, accountID, userID string, lastLogin time.Time) error {
defer util.TimeTrack(ctx, "SyncUserJWTGroups")()
ctx, cancel := getDebuggingCtx(ctx)
defer cancel()

View File

@@ -178,6 +178,8 @@ func (am *DefaultAccountManager) GetUserByID(ctx context.Context, id string) (*t
// GetUser looks up a user by provided nbContext.UserAuths.
// Expects account to have been created already.
func (am *DefaultAccountManager) GetUserFromUserAuth(ctx context.Context, userAuth nbContext.UserAuth) (*types.User, error) {
defer util.TimeTrack(ctx, "SyncUserJWTGroups")()
user, err := am.Store.GetUserByUserID(ctx, store.LockingStrengthNone, userAuth.UserId)
if err != nil {
return nil, err

View File

@@ -1,5 +1,12 @@
package util
import (
"context"
"time"
log "github.com/sirupsen/logrus"
)
// Difference returns the elements in `a` that aren't in `b`.
func Difference(a, b []string) []string {
mb := make(map[string]struct{}, len(b))
@@ -50,3 +57,13 @@ func contains[T comparableObject[T]](slice []T, element T) bool {
}
return false
}
func TimeTrack(ctx context.Context, name string) func() {
start := time.Now()
return func() {
elapsed := time.Since(start)
if elapsed > time.Second {
log.WithContext(ctx).Infof("Slow Call: [%s] took %s", name, elapsed)
}
}
}