Skip to content

Commit 866f2da

Browse files
sameeragCopilot
andauthored
Add JS platform telemetry params (#7991)
- `isPlatformAuthorizeRequest:boolean` Is set on every request that is sent to STS with nativeBroker=1 - `isPlatformBrokerRequest:boolean` Is set on every request that is sent to the platform broker directly, and always set only if `nativeAccountId` is in the cache/request - `isNativeBroker:boolean` Is set on every successful response from the Broker - `BrokerErrorName` for intermittent fatal broker errors --------- Co-authored-by: Copilot <[email protected]>
1 parent 81f41cb commit 866f2da

File tree

11 files changed

+776
-86
lines changed

11 files changed

+776
-86
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "Add Platform Telemetry (PR #7991)",
4+
"packageName": "@azure/msal-browser",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "Add Platform Telemetry (PR #7991)",
4+
"packageName": "@azure/msal-common",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

lib/msal-browser/src/controllers/StandardController.ts

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,9 @@ export class StandardController implements IController {
495495
this.logger.trace(
496496
"handleRedirectPromise - acquiring token from native platform"
497497
);
498+
rootMeasurement.add({
499+
isPlatformBrokerRequest: true,
500+
});
498501
const nativeClient = new PlatformAuthInteractionClient(
499502
this.config,
500503
this.browserStorage,
@@ -727,14 +730,19 @@ export class StandardController implements IController {
727730
this.nativeInternalStorage,
728731
correlationId
729732
);
733+
730734
result = nativeClient
731735
.acquireTokenRedirect(request, atrMeasurement)
732736
.catch((e: AuthError) => {
737+
atrMeasurement.add({
738+
brokerErrorName: e.name,
739+
brokerErrorCode: e.errorCode,
740+
});
733741
if (
734742
e instanceof NativeAuthError &&
735743
isFatalNativeAuthError(e)
736744
) {
737-
this.platformAuthProvider = undefined; // If extension gets uninstalled during session prevent future requests from continuing to attempt
745+
this.platformAuthProvider = undefined; // If extension gets uninstalled during session prevent future requests from continuing to attempt platform broker calls
738746
const redirectClient =
739747
this.createRedirectClient(correlationId);
740748
return redirectClient.acquireToken(request);
@@ -845,6 +853,9 @@ export class StandardController implements IController {
845853
const pkce = this.getPreGeneratedPkceCodes(correlationId);
846854

847855
if (this.canUsePlatformBroker(request)) {
856+
atPopupMeasurement.add({
857+
isPlatformBrokerRequest: true,
858+
});
848859
result = this.acquireTokenNative(
849860
{
850861
...request,
@@ -855,17 +866,20 @@ export class StandardController implements IController {
855866
.then((response) => {
856867
atPopupMeasurement.end({
857868
success: true,
858-
isNativeBroker: true,
859869
accountType: getAccountType(response.account),
860870
});
861871
return response;
862872
})
863873
.catch((e: AuthError) => {
874+
atPopupMeasurement.add({
875+
brokerErrorName: e.name,
876+
brokerErrorCode: e.errorCode,
877+
});
864878
if (
865879
e instanceof NativeAuthError &&
866880
isFatalNativeAuthError(e)
867881
) {
868-
this.platformAuthProvider = undefined; // If extension gets uninstalled during session prevent future requests from continuing to attempt
882+
this.platformAuthProvider = undefined; // If extension gets uninstalled during session prevent future requests from continuing to continuing to attempt platform broker calls
869883
const popupClient =
870884
this.createPopupClient(correlationId);
871885
return popupClient.acquireToken(request, pkce);
@@ -1018,10 +1032,17 @@ export class StandardController implements IController {
10181032
let result: Promise<AuthenticationResult>;
10191033

10201034
if (this.canUsePlatformBroker(validRequest)) {
1035+
this.ssoSilentMeasurement?.add({
1036+
isPlatformBrokerRequest: true,
1037+
});
10211038
result = this.acquireTokenNative(
10221039
validRequest,
10231040
ApiId.ssoSilent
10241041
).catch((e: AuthError) => {
1042+
this.ssoSilentMeasurement?.add({
1043+
brokerErrorName: e.name,
1044+
brokerErrorCode: e.errorCode,
1045+
});
10251046
// If native token acquisition fails for availability reasons fallback to standard flow
10261047
if (e instanceof NativeAuthError && isFatalNativeAuthError(e)) {
10271048
this.platformAuthProvider = undefined; // If extension gets uninstalled during session prevent future requests from continuing to attempt
@@ -1048,7 +1069,6 @@ export class StandardController implements IController {
10481069
);
10491070
this.ssoSilentMeasurement?.end({
10501071
success: true,
1051-
isNativeBroker: response.fromNativeBroker,
10521072
accessTokenSize: response.accessToken.length,
10531073
idTokenSize: response.idToken.length,
10541074
accountType: getAccountType(response.account),
@@ -1132,7 +1152,6 @@ export class StandardController implements IController {
11321152
this.hybridAuthCodeResponses.delete(hybridAuthCode);
11331153
atbcMeasurement.end({
11341154
success: true,
1135-
isNativeBroker: result.fromNativeBroker,
11361155
accessTokenSize: result.accessToken.length,
11371156
idTokenSize: result.idToken.length,
11381157
accountType: getAccountType(result.account),
@@ -1168,6 +1187,9 @@ export class StandardController implements IController {
11681187
if (
11691188
this.canUsePlatformBroker(request, request.nativeAccountId)
11701189
) {
1190+
atbcMeasurement.add({
1191+
isPlatformBrokerRequest: true,
1192+
});
11711193
const result = await this.acquireTokenNative(
11721194
{
11731195
...request,
@@ -1183,6 +1205,10 @@ export class StandardController implements IController {
11831205
) {
11841206
this.platformAuthProvider = undefined; // If extension gets uninstalled during session prevent future requests from continuing to attempt
11851207
}
1208+
atbcMeasurement.add({
1209+
brokerErrorName: e.name,
1210+
brokerErrorCode: e.errorCode,
1211+
});
11861212
throw e;
11871213
});
11881214
atbcMeasurement.end({
@@ -1250,7 +1276,6 @@ export class StandardController implements IController {
12501276
this.acquireTokenByCodeAsyncMeasurement?.end({
12511277
success: true,
12521278
fromCache: response.fromCache,
1253-
isNativeBroker: response.fromNativeBroker,
12541279
});
12551280
return response;
12561281
})
@@ -1615,7 +1640,6 @@ export class StandardController implements IController {
16151640
BrowserAuthErrorCodes.nativeConnectionNotEstablished
16161641
);
16171642
}
1618-
16191643
const nativeClient = new PlatformAuthInteractionClient(
16201644
this.config,
16211645
this.browserStorage,
@@ -2059,7 +2083,6 @@ export class StandardController implements IController {
20592083
atsMeasurement.end({
20602084
success: true,
20612085
fromCache: result.fromCache,
2062-
isNativeBroker: result.fromNativeBroker,
20632086
accessTokenSize: result.accessToken.length,
20642087
idTokenSize: result.idToken.length,
20652088
});
@@ -2298,7 +2321,6 @@ export class StandardController implements IController {
22982321
this.performanceClient.addFields(
22992322
{
23002323
fromCache: response.fromCache,
2301-
isNativeBroker: response.fromNativeBroker,
23022324
},
23032325
request.correlationId
23042326
);
@@ -2346,12 +2368,23 @@ export class StandardController implements IController {
23462368
this.logger.verbose(
23472369
"acquireTokenSilent - attempting to acquire token from native platform"
23482370
);
2371+
this.performanceClient.addFields(
2372+
{ isPlatformBrokerRequest: true },
2373+
silentRequest.correlationId
2374+
);
23492375
return this.acquireTokenNative(
23502376
silentRequest,
23512377
ApiId.acquireTokenSilent_silentFlow,
23522378
silentRequest.account.nativeAccountId,
23532379
cacheLookupPolicy
23542380
).catch(async (e: AuthError) => {
2381+
this.performanceClient.addFields(
2382+
{
2383+
brokerErrorName: e.name,
2384+
brokerErrorCode: e.errorCode,
2385+
},
2386+
silentRequest.correlationId
2387+
);
23552388
// If native token acquisition fails for availability reasons fallback to web flow
23562389
if (e instanceof NativeAuthError && isFatalNativeAuthError(e)) {
23572390
this.logger.verbose(

lib/msal-browser/src/error/NativeAuthError.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ export function isFatalNativeAuthError(error: NativeAuthError): boolean {
5252
if (
5353
error.ext &&
5454
error.ext.status &&
55-
(error.ext.status === NativeStatusCodes.PERSISTENT_ERROR ||
56-
error.ext.status === NativeStatusCodes.DISABLED)
55+
error.ext.status === NativeStatusCodes.DISABLED
5756
) {
5857
return true;
5958
}

lib/msal-browser/src/interaction_client/PlatformAuthInteractionClient.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,14 @@ export class PlatformAuthInteractionClient extends BaseInteractionClient {
155155
): Promise<AuthenticationResult> {
156156
this.performanceClient.addQueueMeasurement(
157157
PerformanceEvents.NativeInteractionClientAcquireToken,
158-
request.correlationId
158+
this.correlationId
159159
);
160160
this.logger.trace("NativeInteractionClient - acquireToken called.");
161161

162162
// start the perf measurement
163163
const nativeATMeasurement = this.performanceClient.startMeasurement(
164164
PerformanceEvents.NativeInteractionClientAcquireToken,
165-
request.correlationId
165+
this.correlationId
166166
);
167167
const reqTimestamp = TimeUtils.nowSeconds();
168168

@@ -191,6 +191,10 @@ export class PlatformAuthInteractionClient extends BaseInteractionClient {
191191
this.logger.info(
192192
"MSAL internal Cache does not contain tokens, return error as per cache policy"
193193
);
194+
nativeATMeasurement.end({
195+
success: false,
196+
brokerErrorCode: "cache_request_failed",
197+
});
194198
throw e;
195199
}
196200
// continue with a native call for any and all errors
@@ -221,14 +225,16 @@ export class PlatformAuthInteractionClient extends BaseInteractionClient {
221225
success: false,
222226
errorCode: error.errorCode,
223227
subErrorCode: error.subError,
224-
isNativeBroker: true,
225228
});
226229
throw error;
227230
});
228231
} catch (e) {
229232
if (e instanceof NativeAuthError) {
230233
serverTelemetryManager.setNativeBrokerErrorCode(e.errorCode);
231234
}
235+
nativeATMeasurement.end({
236+
success: false,
237+
});
232238
throw e;
233239
}
234240
}
@@ -423,6 +429,12 @@ export class PlatformAuthInteractionClient extends BaseInteractionClient {
423429
const serverTelemetryManager =
424430
this.initializeServerTelemetryManager(this.apiId);
425431
serverTelemetryManager.clearNativeBrokerErrorCode();
432+
if (performanceClient && this.correlationId) {
433+
this.performanceClient.addFields(
434+
{ isNativeBroker: true },
435+
this.correlationId
436+
);
437+
}
426438
return authResult;
427439
} catch (e) {
428440
throw e;
@@ -948,7 +960,7 @@ export class PlatformAuthInteractionClient extends BaseInteractionClient {
948960
PerformanceEvents.PopTokenGenerateCnf,
949961
this.logger,
950962
this.performanceClient,
951-
request.correlationId
963+
this.correlationId
952964
)(shrParameters, this.logger);
953965
reqCnfData = generatedReqCnfData.reqCnfString;
954966
validatedRequest.keyId = generatedReqCnfData.kid;
@@ -1073,7 +1085,7 @@ export class PlatformAuthInteractionClient extends BaseInteractionClient {
10731085
embeddedClientId: child_client_id,
10741086
embeddedRedirectUri: child_redirect_uri,
10751087
},
1076-
request.correlationId
1088+
this.correlationId
10771089
);
10781090
}
10791091
}

lib/msal-browser/src/protocol/Authorize.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ async function getStandardParameters(
8484
// signal ests that this is a WAM call
8585
RequestParameterBuilder.addNativeBroker(parameters);
8686

87+
// instrument JS-platform bridge specific fields
88+
performanceClient.addFields(
89+
{
90+
isPlatformAuthorizeRequest: true,
91+
},
92+
request.correlationId
93+
);
94+
8795
// pass the req_cnf for POP
8896
if (request.authenticationScheme === AuthenticationScheme.POP) {
8997
const cryptoOps = new CryptoOps(logger, performanceClient);

0 commit comments

Comments
 (0)