Skip to content

Commit

Permalink
Organize telemetry in the correct module.
Browse files Browse the repository at this point in the history
All the telemetry related code is added to the telemetry module to
avoid cluttering the communication layer with nuances about
telemetry.
  • Loading branch information
juanscr committed Sep 11, 2024
1 parent 45826f9 commit 0ad25a2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 19 deletions.
25 changes: 6 additions & 19 deletions packages/teams-js/src/internal/communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable strict-null-checks/all */

import { ApiName, ApiVersionNumber, getApiVersionTag } from '../internal/telemetry';
import { ApiName, ApiVersionNumber, getApiVersionTag, HostToAppMessageDelayTelemetry } 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, handleHostToAppPerformanceMetrics } from './handlers';
import { CallbackInformation, DOMMessageEvent, ExtendedWindow } from './interfaces';
import { callHandler } from './handlers';
import { DOMMessageEvent, ExtendedWindow } from './interfaces';
import {
deserializeMessageRequest,
deserializeMessageResponse,
Expand Down Expand Up @@ -59,7 +59,6 @@ class CommunicationPrivate {
public static topMessageQueue: MessageRequest[] = [];
public static nextMessageId = 0;
public static callbacks: Map<MessageUUID, Function> = new Map();
public static callbackInformation: Map<MessageUUID, CallbackInformation> = new Map();
public static promiseCallbacks: Map<MessageUUID, (value?: unknown) => void> = new Map();
public static portCallbacks: Map<MessageUUID, (port?: MessagePort, args?: unknown[]) => void> = new Map();
public static messageListener: Function;
Expand Down Expand Up @@ -159,7 +158,7 @@ export function uninitializeCommunication(): void {
CommunicationPrivate.promiseCallbacks.clear();
CommunicationPrivate.portCallbacks.clear();
CommunicationPrivate.legacyMessageIdsToUuidMap = {};
CommunicationPrivate.callbackInformation.clear();
HostToAppMessageDelayTelemetry.clearMessages();
}

/**
Expand Down Expand Up @@ -424,7 +423,7 @@ function sendMessageToParentHelper(
const logger = sendMessageToParentHelperLogger;
const targetWindow = Communication.parentWindow;
const request = createMessageRequest(apiVersionTag, actionName, args);
CommunicationPrivate.callbackInformation.set(request.uuid, {
HostToAppMessageDelayTelemetry.storeCallbackInformation(request.uuid, {
name: actionName,
calledAt: request.timestamp,
});
Expand Down Expand Up @@ -711,19 +710,7 @@ function handleParentMessage(evt: DOMMessageEvent): void {
if (callbackId) {
const callback = CommunicationPrivate.callbacks.get(callbackId);
logger('Received a response from parent for message %i', callbackId);

// Send performance metrics information of message delay
const callbackInformation = CommunicationPrivate.callbackInformation.get(callbackId);
if (callbackInformation && message.timestamp) {
handleHostToAppPerformanceMetrics({
actionName: callbackInformation.name,
messageDelay: getCurrentTimestamp() - message.timestamp,
messageWasCreatedAt: callbackInformation.calledAt,
});
} else {
logger('Unable to send performance metrics for callback %i with arguments %o', callbackId, message.args);
}

HostToAppMessageDelayTelemetry.telemetryHostToAppPerformanceMetrics(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
Expand Down
60 changes: 60 additions & 0 deletions packages/teams-js/src/internal/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
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');

/**
Expand Down Expand Up @@ -50,6 +56,60 @@ export const enum ApiVersionNumber {
V_3 = 'v3',
}

/**
* @internal
* Limited to Microsoft-internal use
*/
export class HostToAppMessageDelayTelemetry {
private static callbackInformation: Map<MessageUUID, CallbackInformation> = 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
*
* 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,
});
} 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',
Expand Down

0 comments on commit 0ad25a2

Please sign in to comment.