From 21e33362e5a92c22dcf659d83ee4d4578d99407d Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Wed, 8 Sep 2021 11:26:54 -0600 Subject: [PATCH 1/3] Add config option to turn on in-room event sending timing metrics This is intended to be hooked up to an external system. Due to the extra events and metadata concerns, this is only available if turned on from the config. See `sendTimePerformanceMetrics.ts` for event schemas. --- src/ContentMessages.tsx | 11 +++++ .../views/rooms/SendMessageComposer.tsx | 10 ++++ src/sendTimePerformanceMetrics.ts | 48 +++++++++++++++++++ src/settings/Settings.tsx | 4 ++ 4 files changed, 73 insertions(+) create mode 100644 src/sendTimePerformanceMetrics.ts diff --git a/src/ContentMessages.tsx b/src/ContentMessages.tsx index 14a0c1ed51..40f8e307a5 100644 --- a/src/ContentMessages.tsx +++ b/src/ContentMessages.tsx @@ -39,6 +39,8 @@ import { import { IUpload } from "./models/IUpload"; import { IAbortablePromise, IImageInfo } from "matrix-js-sdk/src/@types/partials"; import { BlurhashEncoder } from "./BlurhashEncoder"; +import SettingsStore from "./settings/SettingsStore"; +import { decorateStartSendingTime, sendRoundTripMetric } from "./sendTimePerformanceMetrics"; const MAX_WIDTH = 800; const MAX_HEIGHT = 600; @@ -539,6 +541,10 @@ export default class ContentMessages { msgtype: "", // set later }; + if (SettingsStore.getValue("Performance.addSendMessageTimingMetadata")) { + decorateStartSendingTime(content); + } + // if we have a mime type for the file, add it to the message metadata if (file.type) { content.info.mimetype = file.type; @@ -614,6 +620,11 @@ export default class ContentMessages { }).then(function() { if (upload.canceled) throw new UploadCanceledError(); const prom = matrixClient.sendMessage(roomId, content); + if (SettingsStore.getValue("Performance.addSendMessageTimingMetadata")) { + prom.then(resp => { + sendRoundTripMetric(matrixClient, roomId, resp.event_id); + }); + } CountlyAnalytics.instance.trackSendMessage(startTime, prom, roomId, false, false, content); return prom; }, function(err) { diff --git a/src/components/views/rooms/SendMessageComposer.tsx b/src/components/views/rooms/SendMessageComposer.tsx index aca397b6b2..bb5d537895 100644 --- a/src/components/views/rooms/SendMessageComposer.tsx +++ b/src/components/views/rooms/SendMessageComposer.tsx @@ -54,6 +54,7 @@ import { Room } from 'matrix-js-sdk/src/models/room'; import ErrorDialog from "../dialogs/ErrorDialog"; import QuestionDialog from "../dialogs/QuestionDialog"; import { ActionPayload } from "../../../dispatcher/payloads"; +import { decorateStartSendingTime, sendRoundTripMetric } from "../../../sendTimePerformanceMetrics"; function addReplyToMessageContent( content: IContent, @@ -418,6 +419,10 @@ export default class SendMessageComposer extends React.Component { // don't bother sending an empty message if (!content.body.trim()) return; + if (SettingsStore.getValue("Performance.addSendMessageTimingMetadata")) { + decorateStartSendingTime(content); + } + const prom = this.context.sendMessage(roomId, content); if (replyToEvent) { // Clear reply_to_event as we put the message into the queue @@ -433,6 +438,11 @@ export default class SendMessageComposer extends React.Component { dis.dispatch({ action: `effects.${effect.command}` }); } }); + if (SettingsStore.getValue("Performance.addSendMessageTimingMetadata")) { + prom.then(resp => { + sendRoundTripMetric(this.context, roomId, resp.event_id); + }); + } CountlyAnalytics.instance.trackSendMessage(startTime, prom, roomId, false, !!replyToEvent, content); } diff --git a/src/sendTimePerformanceMetrics.ts b/src/sendTimePerformanceMetrics.ts new file mode 100644 index 0000000000..ef461db939 --- /dev/null +++ b/src/sendTimePerformanceMetrics.ts @@ -0,0 +1,48 @@ +/* +Copyright 2021 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import { MatrixClient } from "matrix-js-sdk"; + +/** + * Decorates the given event content object with the "send start time". The + * object will be modified in-place. + * @param {object} content The event content. + */ +export function decorateStartSendingTime(content: object) { + content['io.element.performance_metrics'] = { + sendStartTs: Date.now(), + }; +} + +/** + * Called when an event decorated with `decorateStartSendingTime()` has been sent + * by the server (the client now knows the event ID). + * @param {MatrixClient} client The client to send as. + * @param {string} inRoomId The room ID where the original event was sent. + * @param {string} forEventId The event ID for the decorated event. + */ +export function sendRoundTripMetric(client: MatrixClient, inRoomId: string, forEventId: string) { + // noinspection JSIgnoredPromiseFromCall + client.sendEvent(inRoomId, 'io.element.performance_metric', { + // XXX: We stick all of this into `m.relates_to` so it doesn't end up encrypted. + "m.relates_to": { + rel_type: "io.element.metric", + event_id: forEventId, + responseTs: Date.now(), + kind: 'send_time', + } as any, // override types because we're actually allowed to add extra metadata to relates_to + }); +} diff --git a/src/settings/Settings.tsx b/src/settings/Settings.tsx index 40f57a0a1c..6dbefd4b8e 100644 --- a/src/settings/Settings.tsx +++ b/src/settings/Settings.tsx @@ -759,6 +759,10 @@ export const SETTINGS: {[setting: string]: ISetting} = { default: true, controller: new ReducedMotionController(), }, + "Performance.addSendMessageTimingMetadata": { + supportedLevels: [SettingLevel.CONFIG], + default: false, + }, "Widgets.pinned": { // deprecated supportedLevels: LEVELS_ROOM_OR_ACCOUNT, default: {}, From 70e28e7e137ced3700c4fd8473f00683edf8e92e Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Wed, 8 Sep 2021 11:31:37 -0600 Subject: [PATCH 2/3] Move fields into consistent location for js-sdk to target --- src/sendTimePerformanceMetrics.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/sendTimePerformanceMetrics.ts b/src/sendTimePerformanceMetrics.ts index ef461db939..a8846d3cbf 100644 --- a/src/sendTimePerformanceMetrics.ts +++ b/src/sendTimePerformanceMetrics.ts @@ -37,12 +37,10 @@ export function decorateStartSendingTime(content: object) { export function sendRoundTripMetric(client: MatrixClient, inRoomId: string, forEventId: string) { // noinspection JSIgnoredPromiseFromCall client.sendEvent(inRoomId, 'io.element.performance_metric', { - // XXX: We stick all of this into `m.relates_to` so it doesn't end up encrypted. - "m.relates_to": { - rel_type: "io.element.metric", - event_id: forEventId, + "io.element.performance_metrics": { + forEventId: forEventId, responseTs: Date.now(), kind: 'send_time', - } as any, // override types because we're actually allowed to add extra metadata to relates_to + }, }); } From b4f42e1103262b0f55a5e06e7dd0d254d986e638 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Wed, 8 Sep 2021 11:35:25 -0600 Subject: [PATCH 3/3] Appease the linter --- src/sendTimePerformanceMetrics.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sendTimePerformanceMetrics.ts b/src/sendTimePerformanceMetrics.ts index a8846d3cbf..ee5caa05a9 100644 --- a/src/sendTimePerformanceMetrics.ts +++ b/src/sendTimePerformanceMetrics.ts @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { MatrixClient } from "matrix-js-sdk"; +import { MatrixClient } from "matrix-js-sdk/src"; /** * Decorates the given event content object with the "send start time". The