Skip to content

Commit

Permalink
Change ServerTelemetryEntity from Class to Type (#6651)
Browse files Browse the repository at this point in the history
Continuation of the work to convert cache entities from classes to types
and moving the related static methods to CacheHelpers namespace
  • Loading branch information
tnorling authored Nov 2, 2023
1 parent 25472ea commit 42a5e13
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Convert ServerTelemetryEntity to Type instead of Class #6651",
"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": "Convert ServerTelemetryEntity to Type instead of Class #6651",
"packageName": "@azure/msal-common",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Convert ServerTelemetryEntity to Type instead of Class #6651",
"packageName": "@azure/msal-node",
"email": "[email protected]",
"dependentChangeType": "patch"
}
13 changes: 5 additions & 8 deletions lib/msal-browser/src/cache/BrowserCacheManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -880,12 +880,12 @@ export class BrowserCacheManager extends CacheManager {
);
return null;
}
const parsedMetadata = this.validateAndParseJson(value);
const parsedEntity = this.validateAndParseJson(value);
if (
!parsedMetadata ||
!ServerTelemetryEntity.isServerTelemetryEntity(
!parsedEntity ||
!CacheHelpers.isServerTelemetryEntity(
serverTelemetryKey,
parsedMetadata
parsedEntity
)
) {
this.logger.trace(
Expand All @@ -895,10 +895,7 @@ export class BrowserCacheManager extends CacheManager {
}

this.logger.trace("BrowserCacheManager.getServerTelemetry: cache hit");
return CacheManager.toObject(
new ServerTelemetryEntity(),
parsedMetadata
);
return parsedEntity as ServerTelemetryEntity;
}

/**
Expand Down
24 changes: 10 additions & 14 deletions lib/msal-browser/test/cache/BrowserCacheManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1384,23 +1384,21 @@ describe("BrowserCacheManager tests", () => {

it("getServerTelemetry returns ServerTelemetryEntity", () => {
const testKey = "server-telemetry-clientId";
const testVal = new ServerTelemetryEntity();
const testVal = {
failedRequests: ["61|test-correlationId"],
errors: ["test_error"],
cacheHits: 2,
};

browserLocalStorage.setServerTelemetry(testKey, testVal);
browserSessionStorage.setServerTelemetry(testKey, testVal);

expect(
browserSessionStorage.getServerTelemetry(testKey)
).toEqual(testVal);
expect(
browserSessionStorage.getServerTelemetry(testKey)
).toBeInstanceOf(ServerTelemetryEntity);
expect(
browserLocalStorage.getServerTelemetry(testKey)
).toEqual(testVal);
expect(
browserLocalStorage.getServerTelemetry(testKey)
).toBeInstanceOf(ServerTelemetryEntity);
});
});

Expand Down Expand Up @@ -2294,23 +2292,21 @@ describe("BrowserCacheManager tests", () => {

it("getServerTelemetry returns ServerTelemetryEntity", () => {
const testKey = "server-telemetry-clientId";
const testVal = new ServerTelemetryEntity();
const testVal = {
failedRequests: ["61|test-correlationId"],
errors: ["test_error"],
cacheHits: 2,
};

browserLocalStorage.setServerTelemetry(testKey, testVal);
browserSessionStorage.setServerTelemetry(testKey, testVal);

expect(
browserSessionStorage.getServerTelemetry(testKey)
).toEqual(testVal);
expect(
browserSessionStorage.getServerTelemetry(testKey)
).toBeInstanceOf(ServerTelemetryEntity);
expect(
browserLocalStorage.getServerTelemetry(testKey)
).toEqual(testVal);
expect(
browserLocalStorage.getServerTelemetry(testKey)
).toBeInstanceOf(ServerTelemetryEntity);
});
});

Expand Down
32 changes: 2 additions & 30 deletions lib/msal-common/src/cache/entities/ServerTelemetryEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,8 @@
* Licensed under the MIT License.
*/

import { SERVER_TELEM_CONSTANTS } from "../../utils/Constants";

export class ServerTelemetryEntity {
export type ServerTelemetryEntity = {
failedRequests: Array<string | number>;
errors: string[];
cacheHits: number;

constructor() {
this.failedRequests = [];
this.errors = [];
this.cacheHits = 0;
}

/**
* validates if a given cache entry is "Telemetry", parses <key,value>
* @param key
* @param entity
*/
static isServerTelemetryEntity(key: string, entity?: object): boolean {
const validateKey: boolean =
key.indexOf(SERVER_TELEM_CONSTANTS.CACHE_KEY) === 0;
let validateEntity: boolean = true;

if (entity) {
validateEntity =
entity.hasOwnProperty("failedRequests") &&
entity.hasOwnProperty("errors") &&
entity.hasOwnProperty("cacheHits");
}

return validateKey && validateEntity;
}
}
};
21 changes: 21 additions & 0 deletions lib/msal-common/src/cache/utils/CacheHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import {
AuthenticationScheme,
CredentialType,
SERVER_TELEM_CONSTANTS,
Separators,
} from "../../utils/Constants";
import { TimeUtils } from "../../utils/TimeUtils";
Expand Down Expand Up @@ -304,3 +305,23 @@ function generateScheme(credentialEntity: CredentialEntity): string {
? credentialEntity.tokenType.toLowerCase()
: "";
}

/**
* validates if a given cache entry is "Telemetry", parses <key,value>
* @param key
* @param entity
*/
export function isServerTelemetryEntity(key: string, entity?: object): boolean {
const validateKey: boolean =
key.indexOf(SERVER_TELEM_CONSTANTS.CACHE_KEY) === 0;
let validateEntity: boolean = true;

if (entity) {
validateEntity =
entity.hasOwnProperty("failedRequests") &&
entity.hasOwnProperty("errors") &&
entity.hasOwnProperty("cacheHits");
}

return validateKey && validateEntity;
}
18 changes: 12 additions & 6 deletions lib/msal-common/src/telemetry/server/ServerTelemetryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ export class ServerTelemetryManager {
* Get the server telemetry entity from cache or initialize a new one
*/
getLastRequests(): ServerTelemetryEntity {
const initialValue: ServerTelemetryEntity = new ServerTelemetryEntity();
const initialValue: ServerTelemetryEntity = {
failedRequests: [],
errors: [],
cacheHits: 0,
};
const lastRequests = this.cacheManager.getServerTelemetry(
this.telemetryCacheKey
) as ServerTelemetryEntity;
Expand All @@ -181,11 +185,13 @@ export class ServerTelemetryManager {
this.cacheManager.removeItem(this.telemetryCacheKey);
} else {
// Partial data was flushed to server, construct a new telemetry cache item with errors that were not flushed
const serverTelemEntity = new ServerTelemetryEntity();
serverTelemEntity.failedRequests =
lastRequests.failedRequests.slice(numErrorsFlushed * 2); // failedRequests contains 2 items for each error
serverTelemEntity.errors =
lastRequests.errors.slice(numErrorsFlushed);
const serverTelemEntity: ServerTelemetryEntity = {
failedRequests: lastRequests.failedRequests.slice(
numErrorsFlushed * 2
), // failedRequests contains 2 items for each error
errors: lastRequests.errors.slice(numErrorsFlushed),
cacheHits: 0,
};

this.cacheManager.setServerTelemetry(
this.telemetryCacheKey,
Expand Down
24 changes: 12 additions & 12 deletions lib/msal-common/test/cache/entities/ServerTelemetryEntity.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ServerTelemetryEntity } from "../../../src/cache/entities/ServerTelemetryEntity";
import {
SERVER_TELEM_CONSTANTS,
Separators,
} from "../../../src/utils/Constants";
import { TEST_CONFIG } from "../../test_kit/StringConstants";
import * as CacheHelpers from "../../../src/cache/utils/CacheHelpers";

describe("ServerTelemetryEntity.ts Unit Tests", () => {
const ServerTelemetryKey =
Expand All @@ -19,7 +19,7 @@ describe("ServerTelemetryEntity.ts Unit Tests", () => {
};

expect(
ServerTelemetryEntity.isServerTelemetryEntity(
CacheHelpers.isServerTelemetryEntity(
ServerTelemetryKey,
serverTelemetryObject
)
Expand All @@ -34,7 +34,7 @@ describe("ServerTelemetryEntity.ts Unit Tests", () => {
};

expect(
ServerTelemetryEntity.isServerTelemetryEntity(
CacheHelpers.isServerTelemetryEntity(
ServerTelemetryKey,
missingCacheHits
)
Expand All @@ -46,7 +46,7 @@ describe("ServerTelemetryEntity.ts Unit Tests", () => {
cacheHits: 0,
};
expect(
ServerTelemetryEntity.isServerTelemetryEntity(
CacheHelpers.isServerTelemetryEntity(
ServerTelemetryKey,
missingErrors
)
Expand All @@ -58,7 +58,7 @@ describe("ServerTelemetryEntity.ts Unit Tests", () => {
cacheHits: 0,
};
expect(
ServerTelemetryEntity.isServerTelemetryEntity(
CacheHelpers.isServerTelemetryEntity(
ServerTelemetryKey,
missingFailedRequests
)
Expand All @@ -68,26 +68,26 @@ describe("ServerTelemetryEntity.ts Unit Tests", () => {
testParam: "test",
};
expect(
ServerTelemetryEntity.isServerTelemetryEntity(
CacheHelpers.isServerTelemetryEntity(
ServerTelemetryKey,
noCommonValues
)
).toBe(false);
});

it("Verify an object is a ServerTelemetry Entity only when cache key is passed", () => {
expect(
ServerTelemetryEntity.isServerTelemetryEntity(ServerTelemetryKey)
).toBe(true);
expect(CacheHelpers.isServerTelemetryEntity(ServerTelemetryKey)).toBe(
true
);
});

it("Verify an object is not a ServerTelemetry Entity only when cache key is passed", () => {
const ServerTelemetryKey =
Separators.CACHE_KEY_SEPARATOR +
SERVER_TELEM_CONSTANTS.CACHE_KEY +
TEST_CONFIG.MSAL_CLIENT_ID;
expect(
ServerTelemetryEntity.isServerTelemetryEntity(ServerTelemetryKey)
).toBe(false);
expect(CacheHelpers.isServerTelemetryEntity(ServerTelemetryKey)).toBe(
false
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,11 @@ describe("ServerTelemetryManager.ts", () => {
});

it("incrementCacheHits", () => {
const initialCacheValue = new ServerTelemetryEntity();
const initialCacheValue: ServerTelemetryEntity = {
failedRequests: [],
errors: [],
cacheHits: 0,
};
initialCacheValue.cacheHits = 1;
testCacheManager.setServerTelemetry(cacheKey, initialCacheValue);
const telemetryManager = new ServerTelemetryManager(
Expand Down
2 changes: 1 addition & 1 deletion lib/msal-node/src/cache/NodeStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ export class NodeStorage extends CacheManager {
) as ServerTelemetryEntity;
if (
serverTelemetryEntity &&
ServerTelemetryEntity.isServerTelemetryEntity(
CacheHelpers.isServerTelemetryEntity(
serverTelemetrykey,
serverTelemetryEntity
)
Expand Down

0 comments on commit 42a5e13

Please sign in to comment.