@@ -66,6 +66,15 @@ class BrokerDiscoveryClient(private val brokerCandidates: Set<BrokerData>,
6666 private val isPackageInstalled : (BrokerData ) -> Boolean ,
6767 private val isValidBroker : (BrokerData ) -> Boolean ) : IBrokerDiscoveryClient {
6868
69+ interface IBrokerDiscoveryClientTelemetryCallback {
70+ fun onReadFromCache ()
71+ fun onShouldUseAccountManager ()
72+ fun onFinishCheckingIfPackageIsInstalled (timeSpent : Long )
73+ fun onFinishCheckingIfSupportedByTargetedBroker (timeSpent : Long )
74+ fun onFinishQueryingResultFromBroker (timeSpent : Long )
75+ fun onFinishCheckingIfValidBroker (timeSpent : Long )
76+ }
77+
6978 companion object {
7079 val TAG = BrokerDiscoveryClient ::class .simpleName
7180
@@ -99,6 +108,8 @@ class BrokerDiscoveryClient(private val brokerCandidates: Set<BrokerData>,
99108
100109 const val ERROR_BUNDLE_KEY = " ERROR_BUNDLE_KEY"
101110
111+ public val sTelemetryCallback: IBrokerDiscoveryClientTelemetryCallback ? = null
112+
102113 /* *
103114 * Per-process Thread-safe, coroutine-safe Mutex of this class.
104115 * This is to prevent the IPC mechanism from being unnecessarily triggered due to race condition.
@@ -284,10 +295,16 @@ class BrokerDiscoveryClient(private val brokerCandidates: Set<BrokerData>,
284295 classLevelLock.withLock {
285296 if (! shouldSkipCache) {
286297 if (cache.shouldUseAccountManager()) {
298+ sTelemetryCallback?.onShouldUseAccountManager()
287299 return getActiveBrokerFromAccountManager()
288300 }
289301 cache.getCachedActiveBroker()?.let {
290- if (! isPackageInstalled(it)) {
302+ sTelemetryCallback?.onReadFromCache()
303+
304+ val startTime1 = System .nanoTime()
305+ val isPackageInstalled= isPackageInstalled(it)
306+ sTelemetryCallback?.onFinishCheckingIfPackageIsInstalled(System .nanoTime() - startTime1)
307+ if (! isPackageInstalled) {
291308 Logger .info(
292309 methodTag,
293310 " There is a cached broker: $it , but the app is no longer installed."
@@ -296,7 +313,10 @@ class BrokerDiscoveryClient(private val brokerCandidates: Set<BrokerData>,
296313 return @let
297314 }
298315
299- if (! isValidBroker(it)) {
316+ val startTime2 = System .nanoTime()
317+ val isValidBroker= isValidBroker(it)
318+ sTelemetryCallback?.onFinishCheckingIfValidBroker(System .nanoTime() - startTime2)
319+ if (! isValidBroker) {
300320 Logger .info(
301321 methodTag,
302322 " Clearing cache as the installed app does not have a matching signature hash."
@@ -305,7 +325,11 @@ class BrokerDiscoveryClient(private val brokerCandidates: Set<BrokerData>,
305325 return @let
306326 }
307327
308- if (! ipcStrategy.isSupportedByTargetedBroker(it.packageName)){
328+ val startTime3 = System .nanoTime()
329+ val isSupportedByTargetedBroker =
330+ ipcStrategy.isSupportedByTargetedBroker(it.packageName)
331+ sTelemetryCallback?.onFinishCheckingIfSupportedByTargetedBroker(System .nanoTime() - startTime3)
332+ if (! isSupportedByTargetedBroker){
309333 Logger .info(
310334 methodTag,
311335 " Clearing cache as the installed app does not provide any IPC mechanism to communicate to. (e.g. the broker code isn't shipped with this apk)"
@@ -319,12 +343,14 @@ class BrokerDiscoveryClient(private val brokerCandidates: Set<BrokerData>,
319343 }
320344 }
321345
346+ val startTime4 = System .nanoTime()
322347 val brokerData = queryFromBroker(
323348 brokerCandidates = brokerCandidates,
324349 ipcStrategy = ipcStrategy,
325350 isPackageInstalled = isPackageInstalled,
326351 isValidBroker = isValidBroker
327352 )
353+ sTelemetryCallback?.onFinishCheckingIfPackageIsInstalled(System .nanoTime() - startTime4)
328354
329355 if (brokerData != null ) {
330356 cache.setCachedActiveBroker(brokerData)
0 commit comments