Skip to content

Commit

Permalink
- Instrument scenario id for tracking custom user prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
konstantin-msft committed Apr 19, 2024
1 parent ad8a43f commit 500e3b8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/msal-browser/src/controllers/StandardController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,8 @@ export class StandardController implements IController {
correlationId
);

atPopupMeasurement.add({ scenarioId: request.scenarioId });

try {
this.logger.verbose("acquireTokenPopup called", correlationId);
BrowserUtils.preflightCheck(this.initialized);
Expand Down Expand Up @@ -811,6 +813,8 @@ export class StandardController implements IController {
this.ssoSilentMeasurement?.increment({
visibilityChangeCount: 0,
});
this.ssoSilentMeasurement?.add({ scenarioId: request.scenarioId });

document.addEventListener(
"visibilitychange",
this.trackPageVisibilityWithMeasurement
Expand Down Expand Up @@ -912,6 +916,7 @@ export class StandardController implements IController {
PerformanceEvents.AcquireTokenByCode,
correlationId
);
atbcMeasurement.add({ scenarioId: request.scenarioId });

try {
if (request.code && request.nativeAccountId) {
Expand Down Expand Up @@ -1856,6 +1861,7 @@ export class StandardController implements IController {
);
atsMeasurement.add({
cacheLookupPolicy: request.cacheLookupPolicy,
scenarioId: request.scenarioId,
});

BrowserUtils.preflightCheck(this.initialized);
Expand Down
47 changes: 47 additions & 0 deletions lib/msal-browser/test/app/PublicClientApplication.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2424,6 +2424,47 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
window.opener = oldWindowOpener;
});
});

it("emits successful performance telemetry event", (done) => {
const testAccount: AccountInfo = {
homeAccountId: TEST_DATA_CLIENT_INFO.TEST_HOME_ACCOUNT_ID,
localAccountId: TEST_DATA_CLIENT_INFO.TEST_UID,
environment: "login.windows.net",
tenantId: "3338040d-6c67-4c5b-b112-36a304b66dad",
username: "AbeLi@microsoft.com",
};
const testTokenResponse: AuthenticationResult = {
authority: TEST_CONFIG.validAuthority,
uniqueId: testAccount.localAccountId,
tenantId: testAccount.tenantId,
scopes: TEST_CONFIG.DEFAULT_SCOPES,
idToken: "test-idToken",
idTokenClaims: {},
accessToken: "test-accessToken",
fromCache: false,
correlationId: RANDOM_TEST_GUID,
expiresOn: new Date(Date.now() + 3600000),
account: testAccount,
tokenType: AuthenticationScheme.BEARER,
};
const popupClientSpy = sinon
.stub(PopupClient.prototype, "acquireToken")
.resolves(testTokenResponse);

const callbackId = pca.addPerformanceCallback((events) => {
expect(events[0].correlationId).toBe(RANDOM_TEST_GUID);
expect(events[0].success).toBe(true);
expect(events[0].scenarioId).toBe("test-scenario-id");
pca.removePerformanceCallback(callbackId);
done();
});

pca.acquireTokenPopup({
scopes: ["openid"],
scenarioId: "test-scenario-id",
correlationId: RANDOM_TEST_GUID,
});
});
});

describe("ssoSilent", () => {
Expand Down Expand Up @@ -2811,12 +2852,14 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
expect(events[0].success).toBe(false);
expect(events[0].errorCode).toBe("abc");
expect(events[0].subErrorCode).toBe("defg");
expect(events[0].scenarioId).toBe("test-scenario-id");
pca.removePerformanceCallback(callbackId);
done();
});
pca.ssoSilent({
scopes: ["openid"],
correlationId: RANDOM_TEST_GUID,
scenarioId: "test-scenario-id",
}).catch(() => {});
});
});
Expand Down Expand Up @@ -3175,12 +3218,14 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
expect(events[0].idTokenSize).toBe(12);
expect(events[0].requestId).toBe(undefined);
expect(events[0].visibilityChangeCount).toBe(0);
expect(events[0].scenarioId).toBe("test-scenario-id");
pca.removePerformanceCallback(callbackId);
done();
});
pca.acquireTokenByCode({
code: "auth-code",
correlationId: testTokenResponse.correlationId,
scenarioId: "test-scenario-id",
});
});

Expand Down Expand Up @@ -4854,6 +4899,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
scopes: ["User.Read"],
account: testAccount,
correlationId: RANDOM_TEST_GUID,
scenarioId: "test-scenario-id",
};

const atsSpy = sinon
Expand All @@ -4876,6 +4922,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
expect(events[0].idTokenSize).toBe(4);
expect(events[0].isNativeBroker).toBe(true);
expect(events[0].requestId).toBe(undefined);
expect(events[0].scenarioId).toBe("test-scenario-id");

pca.removePerformanceCallback(callbackId);
done();
Expand Down
2 changes: 2 additions & 0 deletions lib/msal-common/src/request/BaseAuthRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { ShrOptions } from "../crypto/SignedHttpRequest";
* - requestedClaimsHash - SHA 256 hash string of the requested claims string, used as part of an access token cache key so tokens can be filtered by requested claims
* - tokenQueryParameters - String to string map of custom query parameters added to the /token call
* - storeInCache - Object containing boolean values indicating whether to store tokens in the cache or not (default is true)
* - scenarioId - Scenario id to track custom user prompts
*/
export type BaseAuthRequest = {
authority: string;
Expand All @@ -46,4 +47,5 @@ export type BaseAuthRequest = {
maxAge?: number;
tokenQueryParameters?: StringDict;
storeInCache?: StoreInCache;
scenarioId?: string;
};
3 changes: 3 additions & 0 deletions lib/msal-common/src/telemetry/performance/PerformanceEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,9 @@ export type PerformanceEvent = {
cacheRtCount?: number;
cacheIdCount?: number;
cacheAtCount?: number;

// Scenario id to track custom user prompts
scenarioId?: string;
};

export type PerformanceEventContext = {
Expand Down

0 comments on commit 500e3b8

Please sign in to comment.