From 7b23b3f11f146fb9d8fca94b89a443e44f3dd734 Mon Sep 17 00:00:00 2001 From: JuanSe Cardenas Rodriguez Date: Fri, 20 Sep 2024 17:17:21 -0500 Subject: [PATCH] Add performance metrics for one way API calls. --- .../teams-js/src/internal/communication.ts | 1 + .../src/internal/hostToAppTelemetry.ts | 38 ++++++++++++++----- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/packages/teams-js/src/internal/communication.ts b/packages/teams-js/src/internal/communication.ts index c31e27137e..2ac7de5c64 100644 --- a/packages/teams-js/src/internal/communication.ts +++ b/packages/teams-js/src/internal/communication.ts @@ -751,6 +751,7 @@ function handleParentMessage(evt: DOMMessageEvent): void { } else if ('func' in evt.data && typeof evt.data.func === 'string') { // Delegate the request to the proper handler const message = evt.data as MessageRequest; + HostToAppMessageDelayTelemetry.handleOneWayPerformanceMetrics(message, logger); logger('Received an action message %s from parent', message.func); callHandler(message.func, message.args); } else { diff --git a/packages/teams-js/src/internal/hostToAppTelemetry.ts b/packages/teams-js/src/internal/hostToAppTelemetry.ts index f04e408b3e..978bce453b 100644 --- a/packages/teams-js/src/internal/hostToAppTelemetry.ts +++ b/packages/teams-js/src/internal/hostToAppTelemetry.ts @@ -2,7 +2,7 @@ import { Debugger } from 'debug'; import { handleHostToAppPerformanceMetrics } from './handlers'; import { CallbackInformation } from './interfaces'; -import { MessageResponse } from './messageObjects'; +import { MessageRequest, MessageResponse } from './messageObjects'; import { getCurrentTimestamp } from './utils'; import { UUID as MessageUUID } from './uuidObject'; @@ -40,6 +40,26 @@ export default class HostToAppMessageDelayTelemetry { 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 where event is raised in the host. + * @param message The request from the host. + * @param logger The logger in case an error occurs. + */ + public static handleOneWayPerformanceMetrics(message: MessageRequest, logger: Debugger): void { + const timestamp = message.monotonicTimestamp; + if (!timestamp) { + logger('Unable to send performance metrics for event %s', message.func); + return; + } + handleHostToAppPerformanceMetrics({ + actionName: message.func, + messageDelay: getCurrentTimestamp() - timestamp, + messageWasCreatedAt: timestamp, + }); + } /** * @internal @@ -52,15 +72,15 @@ export default class HostToAppMessageDelayTelemetry { */ 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 { + if (!callbackInformation || !message.timestamp) { logger('Unable to send performance metrics for callback %i with arguments %o', callbackID, message.args); + return; } + handleHostToAppPerformanceMetrics({ + actionName: callbackInformation.name, + messageDelay: getCurrentTimestamp() - message.timestamp, + messageWasCreatedAt: callbackInformation.calledAt, + }); + HostToAppMessageDelayTelemetry.deleteMessageInformation(callbackID); } }