github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/libkbfs/keybase_service_measured.go (about)

     1  // Copyright 2016 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package libkbfs
     6  
     7  import (
     8  	"time"
     9  
    10  	"github.com/keybase/client/go/kbfs/idutil"
    11  	"github.com/keybase/client/go/kbfs/kbfscrypto"
    12  	"github.com/keybase/client/go/kbfs/kbfsmd"
    13  	"github.com/keybase/client/go/kbfs/tlf"
    14  	kbname "github.com/keybase/client/go/kbun"
    15  	"github.com/keybase/client/go/protocol/keybase1"
    16  	"github.com/keybase/go-framed-msgpack-rpc/rpc"
    17  	metrics "github.com/rcrowley/go-metrics"
    18  	"golang.org/x/net/context"
    19  )
    20  
    21  // KeybaseServiceMeasured delegates to another KeybaseService instance
    22  // but also keeps track of stats.
    23  type KeybaseServiceMeasured struct {
    24  	delegate                         KeybaseService
    25  	resolveTimer                     metrics.Timer
    26  	identifyTimer                    metrics.Timer
    27  	normalizeSocialAssertionTimer    metrics.Timer
    28  	resolveIdentifyImplicitTeamTimer metrics.Timer
    29  	resolveImplicitTeamByIDTimer     metrics.Timer
    30  	loadUserPlusKeysTimer            metrics.Timer
    31  	loadTeamPlusKeysTimer            metrics.Timer
    32  	createTeamTLFTimer               metrics.Timer
    33  	getTeamSettingsTimer             metrics.Timer
    34  	getCurrentMerkleRootTimer        metrics.Timer
    35  	verifyMerkleRootTimer            metrics.Timer
    36  	currentSessionTimer              metrics.Timer
    37  	favoriteAddTimer                 metrics.Timer
    38  	favoriteDeleteTimer              metrics.Timer
    39  	favoriteListTimer                metrics.Timer
    40  	encryptFavoritesTimer            metrics.Timer
    41  	decryptFavoritesTimer            metrics.Timer
    42  	notifyTimer                      metrics.Timer
    43  	notifyPathUpdatedTimer           metrics.Timer
    44  	putGitMetadataTimer              metrics.Timer
    45  	onPathChangeTimer                metrics.Timer
    46  	onChangeTimer                    metrics.Timer
    47  }
    48  
    49  var _ KeybaseService = KeybaseServiceMeasured{}
    50  
    51  // NewKeybaseServiceMeasured creates and returns a new KeybaseServiceMeasured
    52  // instance with the given delegate and registry.
    53  func NewKeybaseServiceMeasured(delegate KeybaseService, r metrics.Registry) KeybaseServiceMeasured {
    54  	resolveTimer := metrics.GetOrRegisterTimer("KeybaseService.Resolve", r)
    55  	identifyTimer := metrics.GetOrRegisterTimer("KeybaseService.Identify", r)
    56  	normalizeSocialAssertionTimer := metrics.GetOrRegisterTimer("KeybaseService.NormalizeSocialAssertion", r)
    57  	resolveIdentifyImplicitTeamTimer := metrics.GetOrRegisterTimer(
    58  		"KeybaseService.ResolveIdentifyImplicitTeam", r)
    59  	resolveImplicitTeamByIDTimer := metrics.GetOrRegisterTimer(
    60  		"KeybaseService.ResolveImplicitTeamByID", r)
    61  	loadUserPlusKeysTimer := metrics.GetOrRegisterTimer("KeybaseService.LoadUserPlusKeys", r)
    62  	loadTeamPlusKeysTimer := metrics.GetOrRegisterTimer("KeybaseService.LoadTeamPlusKeys", r)
    63  	createTeamTLFTimer := metrics.GetOrRegisterTimer("KeybaseService.CreateTeamTLF", r)
    64  	getTeamSettingsTimer := metrics.GetOrRegisterTimer("KeybaseService.GetTeamSettings", r)
    65  	getCurrentMerkleRootTimer := metrics.GetOrRegisterTimer("KeybaseService.GetCurrentMerkleRoot", r)
    66  	verifyMerkleRootTimer := metrics.GetOrRegisterTimer("KeybaseService.VerifyMerkleRoot", r)
    67  	currentSessionTimer := metrics.GetOrRegisterTimer("KeybaseService.CurrentSession", r)
    68  	favoriteAddTimer := metrics.GetOrRegisterTimer("KeybaseService.FavoriteAdd", r)
    69  	favoriteDeleteTimer := metrics.GetOrRegisterTimer("KeybaseService.FavoriteDelete", r)
    70  	favoriteListTimer := metrics.GetOrRegisterTimer("KeybaseService.FavoriteList", r)
    71  	encryptFavoritesTimer := metrics.GetOrRegisterTimer("KeybaseService."+
    72  		"EncryptFavorites", r)
    73  	decryptFavoritesTimer := metrics.GetOrRegisterTimer("KeybaseService."+
    74  		"DecryptFavorites", r)
    75  	notifyTimer := metrics.GetOrRegisterTimer("KeybaseService.Notify", r)
    76  	notifyPathUpdatedTimer := metrics.GetOrRegisterTimer("KeybaseService.NotifyPathUpdated", r)
    77  	putGitMetadataTimer := metrics.GetOrRegisterTimer(
    78  		"KeybaseService.PutGitMetadata", r)
    79  	onPathChangeTimer := metrics.GetOrRegisterTimer(
    80  		"KeybaseService.OnPathChangeTimer", r)
    81  	onChangeTimer := metrics.GetOrRegisterTimer(
    82  		"KeybaseService.OnChangeTimer", r)
    83  	return KeybaseServiceMeasured{
    84  		delegate:                         delegate,
    85  		resolveTimer:                     resolveTimer,
    86  		identifyTimer:                    identifyTimer,
    87  		normalizeSocialAssertionTimer:    normalizeSocialAssertionTimer,
    88  		resolveIdentifyImplicitTeamTimer: resolveIdentifyImplicitTeamTimer,
    89  		resolveImplicitTeamByIDTimer:     resolveImplicitTeamByIDTimer,
    90  		loadUserPlusKeysTimer:            loadUserPlusKeysTimer,
    91  		loadTeamPlusKeysTimer:            loadTeamPlusKeysTimer,
    92  		createTeamTLFTimer:               createTeamTLFTimer,
    93  		getTeamSettingsTimer:             getTeamSettingsTimer,
    94  		getCurrentMerkleRootTimer:        getCurrentMerkleRootTimer,
    95  		verifyMerkleRootTimer:            verifyMerkleRootTimer,
    96  		currentSessionTimer:              currentSessionTimer,
    97  		favoriteAddTimer:                 favoriteAddTimer,
    98  		favoriteDeleteTimer:              favoriteDeleteTimer,
    99  		favoriteListTimer:                favoriteListTimer,
   100  		encryptFavoritesTimer:            encryptFavoritesTimer,
   101  		decryptFavoritesTimer:            decryptFavoritesTimer,
   102  		notifyTimer:                      notifyTimer,
   103  		notifyPathUpdatedTimer:           notifyPathUpdatedTimer,
   104  		putGitMetadataTimer:              putGitMetadataTimer,
   105  		onPathChangeTimer:                onPathChangeTimer,
   106  		onChangeTimer:                    onChangeTimer,
   107  	}
   108  }
   109  
   110  // Resolve implements the KeybaseService interface for KeybaseServiceMeasured.
   111  func (k KeybaseServiceMeasured) Resolve(
   112  	ctx context.Context, assertion string,
   113  	offline keybase1.OfflineAvailability) (
   114  	name kbname.NormalizedUsername, uid keybase1.UserOrTeamID, err error) {
   115  	k.resolveTimer.Time(func() {
   116  		name, uid, err = k.delegate.Resolve(ctx, assertion, offline)
   117  	})
   118  	return name, uid, err
   119  }
   120  
   121  // Identify implements the KeybaseService interface for KeybaseServiceMeasured.
   122  func (k KeybaseServiceMeasured) Identify(
   123  	ctx context.Context, assertion, reason string,
   124  	offline keybase1.OfflineAvailability) (
   125  	name kbname.NormalizedUsername, id keybase1.UserOrTeamID, err error) {
   126  	k.identifyTimer.Time(func() {
   127  		name, id, err = k.delegate.Identify(ctx, assertion, reason, offline)
   128  	})
   129  	return name, id, err
   130  }
   131  
   132  // NormalizeSocialAssertion implements the KeybaseService interface for
   133  // KeybaseServiceMeasured.
   134  func (k KeybaseServiceMeasured) NormalizeSocialAssertion(
   135  	ctx context.Context, assertion string) (res keybase1.SocialAssertion, err error) {
   136  	k.normalizeSocialAssertionTimer.Time(func() {
   137  		res, err = k.delegate.NormalizeSocialAssertion(
   138  			ctx, assertion)
   139  	})
   140  	return res, err
   141  }
   142  
   143  // ResolveIdentifyImplicitTeam implements the KeybaseService interface
   144  // for KeybaseServiceMeasured.
   145  func (k KeybaseServiceMeasured) ResolveIdentifyImplicitTeam(
   146  	ctx context.Context, assertions, suffix string, tlfType tlf.Type,
   147  	doIdentifies bool, reason string, offline keybase1.OfflineAvailability) (
   148  	info idutil.ImplicitTeamInfo, err error) {
   149  	k.resolveIdentifyImplicitTeamTimer.Time(func() {
   150  		info, err = k.delegate.ResolveIdentifyImplicitTeam(
   151  			ctx, assertions, suffix, tlfType, doIdentifies, reason, offline)
   152  	})
   153  	return info, err
   154  }
   155  
   156  // ResolveImplicitTeamByID implements the KeybaseService interface for
   157  // KeybaseServiceMeasured.
   158  func (k KeybaseServiceMeasured) ResolveImplicitTeamByID(
   159  	ctx context.Context, teamID keybase1.TeamID) (name string, err error) {
   160  	k.resolveImplicitTeamByIDTimer.Time(func() {
   161  		name, err = k.delegate.ResolveImplicitTeamByID(ctx, teamID)
   162  	})
   163  	return name, err
   164  }
   165  
   166  // LoadUserPlusKeys implements the KeybaseService interface for KeybaseServiceMeasured.
   167  func (k KeybaseServiceMeasured) LoadUserPlusKeys(
   168  	ctx context.Context, uid keybase1.UID, pollForKID keybase1.KID,
   169  	offline keybase1.OfflineAvailability) (userInfo idutil.UserInfo, err error) {
   170  	k.loadUserPlusKeysTimer.Time(func() {
   171  		userInfo, err = k.delegate.LoadUserPlusKeys(
   172  			ctx, uid, pollForKID, offline)
   173  	})
   174  	return userInfo, err
   175  }
   176  
   177  // LoadTeamPlusKeys implements the KeybaseService interface for KeybaseServiceMeasured.
   178  func (k KeybaseServiceMeasured) LoadTeamPlusKeys(ctx context.Context,
   179  	tid keybase1.TeamID, tlfType tlf.Type, desiredKeyGen kbfsmd.KeyGen,
   180  	desiredUser keybase1.UserVersion, desiredKey kbfscrypto.VerifyingKey,
   181  	desiredRole keybase1.TeamRole, offline keybase1.OfflineAvailability) (
   182  	teamInfo idutil.TeamInfo, err error) {
   183  	k.loadTeamPlusKeysTimer.Time(func() {
   184  		teamInfo, err = k.delegate.LoadTeamPlusKeys(
   185  			ctx, tid, tlfType, desiredKeyGen, desiredUser, desiredKey,
   186  			desiredRole, offline)
   187  	})
   188  	return teamInfo, err
   189  }
   190  
   191  // CreateTeamTLF implements the KeybaseService interface for
   192  // KeybaseServiceMeasured.
   193  func (k KeybaseServiceMeasured) CreateTeamTLF(
   194  	ctx context.Context, teamID keybase1.TeamID, tlfID tlf.ID) (err error) {
   195  	k.createTeamTLFTimer.Time(func() {
   196  		err = k.delegate.CreateTeamTLF(ctx, teamID, tlfID)
   197  	})
   198  	return err
   199  }
   200  
   201  // GetTeamSettings implements the KeybaseService interface for
   202  // KeybaseServiceMeasured.
   203  func (k KeybaseServiceMeasured) GetTeamSettings(
   204  	ctx context.Context, teamID keybase1.TeamID,
   205  	offline keybase1.OfflineAvailability) (
   206  	settings keybase1.KBFSTeamSettings, err error) {
   207  	k.getTeamSettingsTimer.Time(func() {
   208  		settings, err = k.delegate.GetTeamSettings(ctx, teamID, offline)
   209  	})
   210  	return settings, err
   211  }
   212  
   213  // GetCurrentMerkleRoot implements the KeybaseService interface for
   214  // KeybaseServiceMeasured.
   215  func (k KeybaseServiceMeasured) GetCurrentMerkleRoot(ctx context.Context) (
   216  	root keybase1.MerkleRootV2, updateTime time.Time, err error) {
   217  	k.getCurrentMerkleRootTimer.Time(func() {
   218  		root, updateTime, err = k.delegate.GetCurrentMerkleRoot(ctx)
   219  	})
   220  	return root, updateTime, err
   221  }
   222  
   223  // VerifyMerkleRoot implements the KBPKI interface for
   224  // KeybaseServiceMeasured.
   225  func (k KeybaseServiceMeasured) VerifyMerkleRoot(
   226  	ctx context.Context, root keybase1.MerkleRootV2,
   227  	kbfsRoot keybase1.KBFSRoot) (err error) {
   228  	k.verifyMerkleRootTimer.Time(func() {
   229  		err = k.delegate.VerifyMerkleRoot(ctx, root, kbfsRoot)
   230  	})
   231  	return err
   232  }
   233  
   234  // CurrentSession implements the KeybaseService interface for
   235  // KeybaseServiceMeasured.
   236  func (k KeybaseServiceMeasured) CurrentSession(ctx context.Context, sessionID int) (
   237  	sessionInfo idutil.SessionInfo, err error) {
   238  	k.currentSessionTimer.Time(func() {
   239  		sessionInfo, err = k.delegate.CurrentSession(ctx, sessionID)
   240  	})
   241  	return sessionInfo, err
   242  }
   243  
   244  // FavoriteAdd implements the KeybaseService interface for
   245  // KeybaseServiceMeasured.
   246  func (k KeybaseServiceMeasured) FavoriteAdd(ctx context.Context, folder keybase1.FolderHandle) (err error) {
   247  	k.favoriteAddTimer.Time(func() {
   248  		err = k.delegate.FavoriteAdd(ctx, folder)
   249  	})
   250  	return err
   251  }
   252  
   253  // FavoriteDelete implements the KeybaseService interface for
   254  // KeybaseServiceMeasured.
   255  func (k KeybaseServiceMeasured) FavoriteDelete(ctx context.Context, folder keybase1.FolderHandle) (err error) {
   256  	k.favoriteDeleteTimer.Time(func() {
   257  		err = k.delegate.FavoriteDelete(ctx, folder)
   258  	})
   259  	return err
   260  }
   261  
   262  // FavoriteList implements the KeybaseService interface for
   263  // KeybaseServiceMeasured.
   264  func (k KeybaseServiceMeasured) FavoriteList(ctx context.Context, sessionID int) (
   265  	favorites keybase1.FavoritesResult, err error) {
   266  	k.favoriteListTimer.Time(func() {
   267  		favorites, err = k.delegate.FavoriteList(ctx, sessionID)
   268  	})
   269  	return favorites, err
   270  }
   271  
   272  // EncryptFavorites implements the KeybaseService interface for
   273  // KeybaseServiceMeasured.
   274  func (k KeybaseServiceMeasured) EncryptFavorites(ctx context.Context,
   275  	dataIn []byte) (dataOut []byte, err error) {
   276  	k.favoriteListTimer.Time(func() {
   277  		dataOut, err = k.delegate.EncryptFavorites(ctx, dataIn)
   278  	})
   279  	return dataOut, err
   280  }
   281  
   282  // DecryptFavorites implements the KeybaseService interface for
   283  // KeybaseServiceMeasured.
   284  func (k KeybaseServiceMeasured) DecryptFavorites(ctx context.Context,
   285  	dataIn []byte) (dataOut []byte, err error) {
   286  	k.favoriteListTimer.Time(func() {
   287  		dataOut, err = k.delegate.DecryptFavorites(ctx, dataIn)
   288  	})
   289  	return dataOut, err
   290  }
   291  
   292  // NotifyOnlineStatusChanged implements the KeybaseService interface for
   293  // KeybaseServiceMeasured.
   294  func (k KeybaseServiceMeasured) NotifyOnlineStatusChanged(
   295  	ctx context.Context, online bool) (err error) {
   296  	k.notifyTimer.Time(func() {
   297  		err = k.delegate.NotifyOnlineStatusChanged(ctx, online)
   298  	})
   299  	return err
   300  }
   301  
   302  // Notify implements the KeybaseService interface for KeybaseServiceMeasured.
   303  func (k KeybaseServiceMeasured) Notify(ctx context.Context, notification *keybase1.FSNotification) (err error) {
   304  	k.notifyTimer.Time(func() {
   305  		err = k.delegate.Notify(ctx, notification)
   306  	})
   307  	return err
   308  }
   309  
   310  // NotifyPathUpdated implements the KeybaseService interface for
   311  // KeybaseServiceMeasured.
   312  func (k KeybaseServiceMeasured) NotifyPathUpdated(
   313  	ctx context.Context, path string) (err error) {
   314  	k.notifyPathUpdatedTimer.Time(func() {
   315  		err = k.delegate.NotifyPathUpdated(ctx, path)
   316  	})
   317  	return err
   318  }
   319  
   320  // NotifySyncStatus implements the KeybaseService interface for
   321  // KeybaseServiceMeasured.
   322  func (k KeybaseServiceMeasured) NotifySyncStatus(ctx context.Context,
   323  	status *keybase1.FSPathSyncStatus) (err error) {
   324  	k.notifyTimer.Time(func() {
   325  		err = k.delegate.NotifySyncStatus(ctx, status)
   326  	})
   327  	return err
   328  }
   329  
   330  // NotifyOverallSyncStatus implements the KeybaseService interface for
   331  // KeybaseServiceMeasured.
   332  func (k KeybaseServiceMeasured) NotifyOverallSyncStatus(
   333  	ctx context.Context, status keybase1.FolderSyncStatus) (err error) {
   334  	k.notifyTimer.Time(func() {
   335  		err = k.delegate.NotifyOverallSyncStatus(ctx, status)
   336  	})
   337  	return err
   338  }
   339  
   340  // NotifyFavoritesChanged implements the KeybaseService interface for
   341  // KeybaseServiceMeasured.
   342  func (k KeybaseServiceMeasured) NotifyFavoritesChanged(
   343  	ctx context.Context) (err error) {
   344  	k.notifyTimer.Time(func() {
   345  		err = k.delegate.NotifyFavoritesChanged(ctx)
   346  	})
   347  	return err
   348  }
   349  
   350  // FlushUserFromLocalCache implements the KeybaseService interface for
   351  // KeybaseServiceMeasured.
   352  func (k KeybaseServiceMeasured) FlushUserFromLocalCache(
   353  	ctx context.Context, uid keybase1.UID) {
   354  	k.delegate.FlushUserFromLocalCache(ctx, uid)
   355  }
   356  
   357  // ClearCaches implements the KeybaseService interface for
   358  // KeybaseServiceMeasured.
   359  func (k KeybaseServiceMeasured) ClearCaches(ctx context.Context) {
   360  	k.delegate.ClearCaches(ctx)
   361  }
   362  
   363  // EstablishMountDir implements the KeybaseDaemon interface for KeybaseDaemonLocal.
   364  func (k KeybaseServiceMeasured) EstablishMountDir(ctx context.Context) (string, error) {
   365  	return k.delegate.EstablishMountDir(ctx)
   366  }
   367  
   368  // PutGitMetadata implements the KeybaseDaemon interface for
   369  // KeybaseServiceMeasured.
   370  func (k KeybaseServiceMeasured) PutGitMetadata(
   371  	ctx context.Context, folder keybase1.FolderHandle, repoID keybase1.RepoID,
   372  	metadata keybase1.GitLocalMetadata) (err error) {
   373  	k.putGitMetadataTimer.Time(func() {
   374  		err = k.delegate.PutGitMetadata(ctx, folder, repoID, metadata)
   375  	})
   376  	return err
   377  }
   378  
   379  // OnPathChange implements the SubscriptionNotifier interface.
   380  func (k KeybaseServiceMeasured) OnPathChange(clientID SubscriptionManagerClientID,
   381  	subscriptionIDs []SubscriptionID, path string, topics []keybase1.PathSubscriptionTopic) {
   382  	k.onPathChangeTimer.Time(func() {
   383  		k.delegate.OnPathChange(clientID, subscriptionIDs, path, topics)
   384  	})
   385  }
   386  
   387  // OnNonPathChange implements the SubscriptionNotifier interface.
   388  func (k KeybaseServiceMeasured) OnNonPathChange(
   389  	clientID SubscriptionManagerClientID,
   390  	subscriptionIDs []SubscriptionID, topic keybase1.SubscriptionTopic) {
   391  	k.onChangeTimer.Time(func() {
   392  		k.delegate.OnNonPathChange(clientID, subscriptionIDs, topic)
   393  	})
   394  }
   395  
   396  // GetKVStoreClient implements the KeybaseService interface.
   397  func (k KeybaseServiceMeasured) GetKVStoreClient() keybase1.KvstoreInterface {
   398  	return k.delegate.GetKVStoreClient()
   399  }
   400  
   401  // GetKeybaseDaemonRawClient implements the KeybaseService interface.
   402  func (k KeybaseServiceMeasured) GetKeybaseDaemonRawClient() rpc.GenericClient {
   403  	return k.delegate.GetKeybaseDaemonRawClient()
   404  }
   405  
   406  // Shutdown implements the KeybaseService interface for
   407  // KeybaseServiceMeasured.
   408  func (k KeybaseServiceMeasured) Shutdown() {
   409  	k.delegate.Shutdown()
   410  }