Skip to content

Commit

Permalink
Add performance metrics for one way API calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
juanscr committed Sep 20, 2024
1 parent 114df12 commit 7b23b3f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/teams-js/src/internal/communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 29 additions & 9 deletions packages/teams-js/src/internal/hostToAppTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}

0 comments on commit 7b23b3f

Please sign in to comment.