Skip to content

Commit ecd9435

Browse files
committed
add callback for oneauth
1 parent 1989713 commit ecd9435

File tree

5 files changed

+123
-23
lines changed

5 files changed

+123
-23
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
vNext
22
----------
3+
- [MINOR] Add callback for OneAuth for measuring Broker Discovery Client Perf (#2796)
34
- [MINOR] Add new span name for DELEGATION_CERT_INSTALL's telemetry (#2790)
45
- [MINOR] Refactor getAccountByLocalAccountId (#2781)
56
- [MINOR] Add OTel Benchmarker (#2786)

common/src/main/java/com/microsoft/identity/common/internal/activebrokerdiscovery/BrokerDiscoveryClient.kt

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
// THE SOFTWARE.
2323
package com.microsoft.identity.common.internal.activebrokerdiscovery
2424

25+
import IBrokerDiscoveryClientTelemetryCallback
2526
import android.content.Context
2627
import android.os.Bundle
2728
import com.microsoft.identity.common.exception.BrokerCommunicationException
@@ -66,15 +67,6 @@ class BrokerDiscoveryClient(private val brokerCandidates: Set<BrokerData>,
6667
private val isPackageInstalled: (BrokerData) -> Boolean,
6768
private val isValidBroker: (BrokerData) -> Boolean) : IBrokerDiscoveryClient {
6869

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-
7870
companion object {
7971
val TAG = BrokerDiscoveryClient::class.simpleName
8072

@@ -108,8 +100,6 @@ class BrokerDiscoveryClient(private val brokerCandidates: Set<BrokerData>,
108100

109101
const val ERROR_BUNDLE_KEY = "ERROR_BUNDLE_KEY"
110102

111-
public val sTelemetryCallback: IBrokerDiscoveryClientTelemetryCallback? = null
112-
113103
/**
114104
* Per-process Thread-safe, coroutine-safe Mutex of this class.
115105
* This is to prevent the IPC mechanism from being unnecessarily triggered due to race condition.
@@ -286,24 +276,35 @@ class BrokerDiscoveryClient(private val brokerCandidates: Set<BrokerData>,
286276

287277
override fun getActiveBroker(shouldSkipCache: Boolean): BrokerData? {
288278
return runBlocking {
289-
return@runBlocking getActiveBrokerAsync(shouldSkipCache)
279+
return@runBlocking getActiveBrokerAsync(shouldSkipCache, null)
280+
}
281+
}
282+
283+
override fun getActiveBroker(
284+
shouldSkipCache: Boolean,
285+
telemetryCallback: IBrokerDiscoveryClientTelemetryCallback
286+
): BrokerData? {
287+
return runBlocking {
288+
return@runBlocking getActiveBrokerAsync(shouldSkipCache, telemetryCallback)
290289
}
291290
}
292291

293-
private suspend fun getActiveBrokerAsync(shouldSkipCache:Boolean): BrokerData?{
292+
private suspend fun getActiveBrokerAsync(shouldSkipCache:Boolean,
293+
telemetryCallback: IBrokerDiscoveryClientTelemetryCallback?): BrokerData?{
294294
val methodTag = "$TAG:getActiveBrokerAsync"
295295
classLevelLock.withLock {
296296
if (!shouldSkipCache) {
297297
if (cache.shouldUseAccountManager()) {
298-
sTelemetryCallback?.onShouldUseAccountManager()
298+
telemetryCallback?.onUseAccountManager()
299299
return getActiveBrokerFromAccountManager()
300300
}
301+
val timeStartReadingFromCache = System.nanoTime()
301302
cache.getCachedActiveBroker()?.let {
302-
sTelemetryCallback?.onReadFromCache()
303+
telemetryCallback?.onReadFromCache(System.nanoTime() - timeStartReadingFromCache)
303304

304-
val startTime1 = System.nanoTime()
305+
val timeStartIsPackageInstalled = System.nanoTime()
305306
val isPackageInstalled= isPackageInstalled(it)
306-
sTelemetryCallback?.onFinishCheckingIfPackageIsInstalled(System.nanoTime() - startTime1)
307+
telemetryCallback?.onFinishCheckingIfPackageIsInstalled(System.nanoTime() - timeStartIsPackageInstalled)
307308
if (!isPackageInstalled) {
308309
Logger.info(
309310
methodTag,
@@ -313,9 +314,9 @@ class BrokerDiscoveryClient(private val brokerCandidates: Set<BrokerData>,
313314
return@let
314315
}
315316

316-
val startTime2 = System.nanoTime()
317+
val timeStartIsValidBroker = System.nanoTime()
317318
val isValidBroker= isValidBroker(it)
318-
sTelemetryCallback?.onFinishCheckingIfValidBroker(System.nanoTime() - startTime2)
319+
telemetryCallback?.onFinishCheckingIfValidBroker(System.nanoTime() - timeStartIsValidBroker)
319320
if (!isValidBroker) {
320321
Logger.info(
321322
methodTag,
@@ -325,10 +326,10 @@ class BrokerDiscoveryClient(private val brokerCandidates: Set<BrokerData>,
325326
return@let
326327
}
327328

328-
val startTime3 = System.nanoTime()
329+
val timeStartIsSupportedByTargetedBroker = System.nanoTime()
329330
val isSupportedByTargetedBroker =
330331
ipcStrategy.isSupportedByTargetedBroker(it.packageName)
331-
sTelemetryCallback?.onFinishCheckingIfSupportedByTargetedBroker(System.nanoTime() - startTime3)
332+
telemetryCallback?.onFinishCheckingIfSupportedByTargetedBroker(System.nanoTime() - timeStartIsSupportedByTargetedBroker)
332333
if(!isSupportedByTargetedBroker){
333334
Logger.info(
334335
methodTag,
@@ -343,14 +344,14 @@ class BrokerDiscoveryClient(private val brokerCandidates: Set<BrokerData>,
343344
}
344345
}
345346

346-
val startTime4 = System.nanoTime()
347+
val timeStartQueryFromBroker = System.nanoTime()
347348
val brokerData = queryFromBroker(
348349
brokerCandidates = brokerCandidates,
349350
ipcStrategy = ipcStrategy,
350351
isPackageInstalled = isPackageInstalled,
351352
isValidBroker = isValidBroker
352353
)
353-
sTelemetryCallback?.onFinishCheckingIfPackageIsInstalled(System.nanoTime() - startTime4)
354+
telemetryCallback?.onFinishQueryingResultFromBroker(System.nanoTime() - timeStartQueryFromBroker)
354355

355356
if (brokerData != null) {
356357
cache.setCachedActiveBroker(brokerData)
@@ -369,6 +370,7 @@ class BrokerDiscoveryClient(private val brokerCandidates: Set<BrokerData>,
369370
)
370371
)
371372

373+
telemetryCallback?.onUseAccountManager()
372374
val accountManagerResult = getActiveBrokerFromAccountManager()
373375
Logger.info(
374376
methodTag, "Tried getting active broker from account manager, " +

common/src/main/java/com/microsoft/identity/common/internal/activebrokerdiscovery/IBrokerDiscoveryClient.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
// THE SOFTWARE.
2323
package com.microsoft.identity.common.internal.activebrokerdiscovery
2424

25+
import IBrokerDiscoveryClientTelemetryCallback
2526
import com.microsoft.identity.common.internal.broker.BrokerData
2627
import com.microsoft.identity.common.java.exception.ClientException
2728

@@ -37,6 +38,20 @@ interface IBrokerDiscoveryClient {
3738
* **/
3839
fun getActiveBroker(shouldSkipCache: Boolean = false) : BrokerData?
3940

41+
42+
/**
43+
* Performs a discovery to figure out which broker app the SDK (MSAL/OneAuth)
44+
* has to send its request to.
45+
*
46+
* @param shouldSkipCache If true, this will skip cached value (if any)
47+
* and always query the broker for the result.
48+
* @param telemetryCallback callback with telemetry data.
49+
* @return BrokerData package name and signature hash of the targeted app.
50+
* **/
51+
fun getActiveBroker(shouldSkipCache: Boolean = false,
52+
telemetryCallback: IBrokerDiscoveryClientTelemetryCallback) : BrokerData?
53+
54+
4055
/**
4156
* Force a broker app to figure out which broker app the SDK (MSAL/OneAuth)
4257
* has to send its request to.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// All rights reserved.
3+
//
4+
// This code is licensed under the MIT License.
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files(the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions :
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
// THE SOFTWARE.
23+
24+
interface IBrokerDiscoveryClientTelemetryCallback {
25+
/**
26+
* If triggered, the broker discovery client is using Account Manager
27+
* (e.g. one of the installed broker is too old and doesn't support this new mechanism).
28+
**/
29+
fun onUseAccountManager()
30+
31+
/**
32+
* Triggered when BrokerDiscoveryClient finishes reading from its cache.
33+
*
34+
* @return timeSpentInNanoSeconds Time spent reading the cache (in nanoseconds).
35+
**/
36+
fun onReadFromCache(timeSpentInNanoSeconds: Long)
37+
38+
/**
39+
* Triggered when BrokerDiscoveryClient finishes validating if the cached broker package is installed.
40+
*
41+
* @return timeSpentInNanoSeconds Time spent to validate if the package is installed (in nanoseconds).
42+
**/
43+
fun onFinishCheckingIfPackageIsInstalled(timeSpentInNanoSeconds: Long)
44+
45+
/**
46+
* Triggered when BrokerDiscoveryClient finishes validating if the cached broker package supports the given IPC strategy.
47+
*
48+
* @return timeSpentInNanoSeconds Time spent to validate if the package supports the IPC strategy (in nanoseconds).
49+
**/
50+
fun onFinishCheckingIfSupportedByTargetedBroker(timeSpentInNanoSeconds: Long)
51+
52+
/**
53+
* Triggered when BrokerDiscoveryClient finishes validating if the cached broker package is a valid broker.
54+
*
55+
* @return timeSpentInNanoSeconds Time spent to validate if the package is a valid broker (in nanoseconds).
56+
**/
57+
fun onFinishCheckingIfValidBroker(timeSpentInNanoSeconds: Long)
58+
59+
/**
60+
* Triggered when BrokerDiscoveryClient finishes querying active broker from one of the broker apps.
61+
*
62+
* There are 2 possible cases when this is triggered.
63+
* 1. Both Broker and this MSAL/OneAuth app are freshly installed.
64+
* This will trigger the broker discovery in broker, and will take longer time.
65+
*
66+
* 2. Broker has already done broker discovery before (e.g. by other MSAL/OneAuth app). Only this MSAL/OneAuth app is freshly installed.
67+
* When the request reaches the broker, the broker would just return the cached value, which will be faster.
68+
*
69+
* @return timeSpentInNanoSeconds Time spent to query the active broker (in nanoseconds).
70+
**/
71+
fun onFinishQueryingResultFromBroker(timeSpentInNanoSeconds: Long)
72+
}

common/src/main/java/com/microsoft/identity/common/internal/activebrokerdiscovery/LegacyBrokerDiscoveryClient.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
// THE SOFTWARE.
2323
package com.microsoft.identity.common.internal.activebrokerdiscovery
2424

25+
import IBrokerDiscoveryClientTelemetryCallback
2526
import android.content.Context
2627
import com.microsoft.identity.common.internal.broker.BrokerData
2728

@@ -35,6 +36,15 @@ class LegacyBrokerDiscoveryClient(val context: Context): IBrokerDiscoveryClient
3536
.getActiveBrokerFromAccountManager()
3637
}
3738

39+
override fun getActiveBroker(
40+
shouldSkipCache: Boolean,
41+
telemetryCallback: IBrokerDiscoveryClientTelemetryCallback
42+
): BrokerData? {
43+
// LegacyBrokerDiscoveryClient does not support telemetry callback.
44+
return AccountManagerBrokerDiscoveryUtil(context)
45+
.getActiveBrokerFromAccountManager()
46+
}
47+
3848
override fun forceBrokerRediscovery(brokerCandidate: BrokerData): BrokerData {
3949
throw UnsupportedOperationException("LegacyBrokerDiscoveryClient does not support forceBrokerRediscovery.")
4050
}

0 commit comments

Comments
 (0)