Skip to content

Instrument cache location #7868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Instrument cache location #7868",
"packageName": "@azure/msal-browser",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Instrument cache location #7868",
"packageName": "@azure/msal-common",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ export abstract class BaseInteractionClient {
this.correlationId
);
this.performanceClient = performanceClient;
this.performanceClient.addFields(
{
cacheLocation: config.cache.cacheLocation,
},
this.correlationId
);
}

abstract acquireToken(
Expand Down
87 changes: 79 additions & 8 deletions lib/msal-browser/test/app/PublicClientApplication.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,9 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {

it("Calls NativeInteractionClient.handleRedirectPromise and emits telemetry event", (done) => {
const config = {
cache: {
cacheLocation: BrowserCacheLocation.LocalStorage,
},
auth: {
clientId: TEST_CONFIG.MSAL_CLIENT_ID,
},
Expand All @@ -940,11 +943,11 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
},
},
};
pca = new PublicClientApplication(config);
const testPca = new PublicClientApplication(config);
stubExtensionProvider(config);

pca.initialize().then(() => {
const callbackId = pca.addPerformanceCallback((events) => {
testPca.initialize().then(() => {
const callbackId = testPca.addPerformanceCallback((events) => {
expect(events.length).toEqual(1);
const event = events[0];
expect(event.name).toBe(
Expand All @@ -960,12 +963,13 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
).toEqual(1);
expect(event.success).toBeTruthy();
expect(event.accountType).toEqual("MSA");
pca.removePerformanceCallback(callbackId);
expect(event.cacheLocation).toEqual("localStorage");
testPca.removePerformanceCallback(callbackId);
done();
});
// Implementation of PCA was moved to controller.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
pca = (pca as any).controller;
const testController = (testPca as any).controller;

const testAccount: AccountInfo = {
homeAccountId: TEST_DATA_CLIENT_INFO.TEST_HOME_ACCOUNT_ID,
Expand Down Expand Up @@ -1012,20 +1016,20 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
windowTitleSubstring: "test window",
};
// @ts-ignore
pca.browserStorage.setTemporaryCache(
testController.browserStorage.setTemporaryCache(
TemporaryCacheKeys.NATIVE_REQUEST,
JSON.stringify(nativeRequest),
true
);
jest.spyOn(pca, "getAllAccounts").mockReturnValue([
jest.spyOn(testController, "getAllAccounts").mockReturnValue([
testAccount,
]);
jest.spyOn(
PlatformAuthInteractionClient.prototype,
"handleRedirectPromise"
).mockResolvedValue(testTokenResponse);

pca.handleRedirectPromise();
testController.handleRedirectPromise();
});
});

Expand Down Expand Up @@ -3636,6 +3640,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
expect(events[0].requestId).toBe(undefined);
expect(events[0].visibilityChangeCount).toBe(0);
expect(events[0].accountType).toBeUndefined();
expect(events[0].cacheLocation).toEqual("sessionStorage");
pca.removePerformanceCallback(callbackId);
done();
});
Expand Down Expand Up @@ -6023,6 +6028,7 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
expect(events[0].requestId).toBe(undefined);
expect(events[0].visibilityChangeCount).toBe(0);
expect(events[0].accountType).toBe("AAD");
expect(events[0].cacheLocation).toBe("sessionStorage");

pca.removePerformanceCallback(callbackId);
done();
Expand All @@ -6034,6 +6040,71 @@ describe("PublicClientApplication.ts Class Unit Tests", () => {
});
});

xit("instruments local storage cache location", (done) => {
const testPca = new PublicClientApplication({
auth: {
clientId: TEST_CONFIG.MSAL_CLIENT_ID,
},
telemetry: {
client: new BrowserPerformanceClient(testAppConfig),
application: {
appName: TEST_CONFIG.applicationName,
appVersion: TEST_CONFIG.applicationVersion,
},
},
system: {
allowPlatformBroker: false,
},
cache: {
cacheLocation: BrowserCacheLocation.LocalStorage,
},
});
testPca.initialize().then(() => {
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: "[email protected]",
idTokenClaims: {
tid: "3338040d-6c67-4c5b-b112-36a304b66dad",
},
};
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: TestTimeUtils.nowDateWithOffset(3600),
account: testAccount,
tokenType: AuthenticationScheme.BEARER,
};
const silentCacheSpy: jest.SpyInstance = jest
.spyOn(SilentCacheClient.prototype, "acquireToken")
.mockRejectedValue(new Error("Expired"));
const silentRefreshSpy: jest.SpyInstance = jest
.spyOn(SilentRefreshClient.prototype, "acquireToken")
.mockResolvedValue(testTokenResponse);

const callbackId = testPca.addPerformanceCallback((events) => {
expect(events[0].cacheLocation).toBe("localStorage");

testPca.removePerformanceCallback(callbackId);
done();
});
testPca.acquireTokenSilent({
scopes: ["openid"],
account: testAccount,
correlationId: RANDOM_TEST_GUID,
});
});
});

it("sets visibilityChange in perf event to true when visibility changes", (done) => {
const testAccount: AccountInfo = {
homeAccountId: TEST_DATA_CLIENT_INFO.TEST_HOME_ACCOUNT_ID,
Expand Down
1 change: 1 addition & 0 deletions lib/msal-common/apiReview/msal-common.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3463,6 +3463,7 @@ export type PerformanceEvent = {
usePreGeneratedPkce?: boolean;
msalInstanceCount?: number;
sameClientIdInstanceCount?: number;
cacheLocation?: string;
};

// Warning: (tsdoc-undefined-tag) The TSDoc tag "@export" is not defined in this configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,8 @@ export type PerformanceEvent = {
msalInstanceCount?: number;
// Number of MSAL JS instances using the same client id in the frame
sameClientIdInstanceCount?: number;
// Browser cache location
cacheLocation?: string;
};

export type PerformanceEventContext = {
Expand Down