diff --git a/packages/teams-js/src/internal/communication.ts b/packages/teams-js/src/internal/communication.ts index 12b65a1092..c31e27137e 100644 --- a/packages/teams-js/src/internal/communication.ts +++ b/packages/teams-js/src/internal/communication.ts @@ -2,13 +2,14 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable strict-null-checks/all */ -import { ApiName, ApiVersionNumber, getApiVersionTag, HostToAppMessageDelayTelemetry } from '../internal/telemetry'; +import { ApiName, ApiVersionNumber, getApiVersionTag } from '../internal/telemetry'; import { FrameContexts } from '../public/constants'; import { SdkError } from '../public/interfaces'; import { latestRuntimeApiVersion } from '../public/runtime'; import { version } from '../public/version'; import { GlobalVars } from './globalVars'; import { callHandler } from './handlers'; +import HostToAppMessageDelayTelemetry from './hostToAppTelemetry'; import { DOMMessageEvent, ExtendedWindow } from './interfaces'; import { deserializeMessageRequest, @@ -710,7 +711,7 @@ function handleParentMessage(evt: DOMMessageEvent): void { if (callbackId) { const callback = CommunicationPrivate.callbacks.get(callbackId); logger('Received a response from parent for message %i', callbackId); - HostToAppMessageDelayTelemetry.telemetryHostToAppPerformanceMetrics(callbackId, message, logger); + HostToAppMessageDelayTelemetry.handlePerformanceMetrics(callbackId, message, logger); if (callback) { logger('Invoking the registered callback for message %i with arguments %o', callbackId, message.args); // eslint-disable-next-line @typescript-eslint/ban-ts-comment diff --git a/packages/teams-js/src/internal/hostToAppTelemetry.ts b/packages/teams-js/src/internal/hostToAppTelemetry.ts new file mode 100644 index 0000000000..f04e408b3e --- /dev/null +++ b/packages/teams-js/src/internal/hostToAppTelemetry.ts @@ -0,0 +1,66 @@ +import { Debugger } from 'debug'; + +import { handleHostToAppPerformanceMetrics } from './handlers'; +import { CallbackInformation } from './interfaces'; +import { MessageResponse } from './messageObjects'; +import { getCurrentTimestamp } from './utils'; +import { UUID as MessageUUID } from './uuidObject'; + +/** + * @internal + * Limited to Microsoft-internal use + */ +export default class HostToAppMessageDelayTelemetry { + private static callbackInformation: Map = new Map(); + + /** + * @internal + * Limited to Microsoft-internal use + * + * Store information about a particular message. + * @param messageUUID The message id for the request. + * @param callbackInformation The information of the callback. + */ + public static storeCallbackInformation(messageUUID: MessageUUID, callbackInformation: CallbackInformation): void { + HostToAppMessageDelayTelemetry.callbackInformation.set(messageUUID, callbackInformation); + } + + /** + * @internal + * Limited to Microsoft-internal use + */ + public static clearMessages(): void { + HostToAppMessageDelayTelemetry.callbackInformation.clear(); + } + + /** + * @internal + * Limited to Microsoft-internal use + */ + public static deleteMessageInformation(callbackId: MessageUUID): void { + HostToAppMessageDelayTelemetry.callbackInformation.delete(callbackId); + } + + /** + * @internal + * Limited to Microsoft-internal use + * + * Executes telemetry actions related to host to app performance metrics. + * @param callbackId The message id for the request. + * @param message The response from the host. + * @param logger The logger in case an error occurs. + */ + public static handlePerformanceMetrics(callbackID: MessageUUID, message: MessageResponse, logger: Debugger): void { + const callbackInformation = HostToAppMessageDelayTelemetry.callbackInformation.get(callbackID); + if (callbackInformation && message.timestamp) { + handleHostToAppPerformanceMetrics({ + actionName: callbackInformation.name, + messageDelay: getCurrentTimestamp() - message.timestamp, + messageWasCreatedAt: callbackInformation.calledAt, + }); + HostToAppMessageDelayTelemetry.deleteMessageInformation(callbackID); + } else { + logger('Unable to send performance metrics for callback %i with arguments %o', callbackID, message.args); + } + } +} diff --git a/packages/teams-js/src/internal/telemetry.ts b/packages/teams-js/src/internal/telemetry.ts index 0f840269c7..87a49abde7 100644 --- a/packages/teams-js/src/internal/telemetry.ts +++ b/packages/teams-js/src/internal/telemetry.ts @@ -1,11 +1,5 @@ import { debug as registerLogger, Debugger } from 'debug'; -import { handleHostToAppPerformanceMetrics } from './handlers'; -import { CallbackInformation } from './interfaces'; -import { MessageResponse } from './messageObjects'; -import { getCurrentTimestamp } from './utils'; -import { UUID as MessageUUID } from './uuidObject'; - const topLevelLogger = registerLogger('teamsJs'); /** @@ -56,69 +50,6 @@ export const enum ApiVersionNumber { V_3 = 'v3', } -/** - * @internal - * Limited to Microsoft-internal use - */ -export class HostToAppMessageDelayTelemetry { - private static callbackInformation: Map = new Map(); - - /** - * @internal - * Limited to Microsoft-internal use - * - * Store information about a particular message. - * @param messageUUID The message id for the request. - * @param callbackInformation The information of the callback. - */ - public static storeCallbackInformation(messageUUID: MessageUUID, callbackInformation: CallbackInformation): void { - HostToAppMessageDelayTelemetry.callbackInformation.set(messageUUID, callbackInformation); - } - - /** - * @internal - * Limited to Microsoft-internal use - */ - public static clearMessages(): void { - HostToAppMessageDelayTelemetry.callbackInformation.clear(); - } - - /** - * @internal - * Limited to Microsoft-internal use - */ - public static deleteMessageInformation(callbackId: MessageUUID): void { - HostToAppMessageDelayTelemetry.callbackInformation.delete(callbackId); - } - - /** - * @internal - * Limited to Microsoft-internal use - * - * Executes telemetry actions related to host to app performance metrics. - * @param callbackId The message id for the request. - * @param message The response from the host. - * @param logger A logger for logging any possible error. - */ - public static telemetryHostToAppPerformanceMetrics( - callbackID: MessageUUID, - message: MessageResponse, - logger: debug.Debugger, - ): void { - const callbackInformation = HostToAppMessageDelayTelemetry.callbackInformation.get(callbackID); - if (callbackInformation && message.timestamp) { - handleHostToAppPerformanceMetrics({ - actionName: callbackInformation.name, - messageDelay: getCurrentTimestamp() - message.timestamp, - messageWasCreatedAt: callbackInformation.calledAt, - }); - HostToAppMessageDelayTelemetry.deleteMessageInformation(callbackID); - } else { - logger('Unable to send performance metrics for callback %i with arguments %o', callbackID, message.args); - } - } -} - export const enum ApiName { App_GetContext = 'app.getContext', App_Initialize = 'app.initialize', diff --git a/packages/teams-js/test/internal/communication.spec.ts b/packages/teams-js/test/internal/communication.spec.ts index 5fc4107ca3..c45343511f 100644 --- a/packages/teams-js/test/internal/communication.spec.ts +++ b/packages/teams-js/test/internal/communication.spec.ts @@ -11,7 +11,6 @@ import { Utils } from '../utils'; jest.mock('../../src/internal/handlers', () => ({ callHandler: jest.fn(), - handleHostToAppPerformanceMetrics: jest.fn(), })); const testApiVersion = getApiVersionTag(ApiVersionNumber.V_1, 'mockedApiName' as ApiName);