/* Copyright 2023 New Vector Ltd 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 opentelemetry, { Span, Attributes } from "@opentelemetry/api"; import { GroupCall, MatrixClient, MatrixEvent, RoomMember, } from "matrix-js-sdk"; import { VoipEvent } from "matrix-js-sdk/src/webrtc/call"; import { tracer } from "./otel"; /** * Flattens out an object into a single layer with components * of the key separated by dots */ function flattenVoipEvent(event: VoipEvent): Attributes { const flatObject = {}; flattenVoipEventRecursive( event as unknown as Record, // XXX Types flatObject, "matrix.event.", 0 ); return flatObject; } function flattenVoipEventRecursive( obj: Record, flatObject: Record, prefix: string, depth: number ) { if (depth > 10) throw new Error( "Depth limit exceeded: aborting VoipEvent recursion. Prefix is " + prefix ); for (const [k, v] of Object.entries(obj)) { if (["string", "number"].includes(typeof v)) { flatObject[prefix + k] = v; } else if (typeof v === "object") { flattenVoipEventRecursive( v as Record, flatObject, prefix + k + ".", depth + 1 ); } } } /** * Represent the span of time which we intend to be joined to a group call */ export class OTelGroupCallMembership { private callMembershipSpan: Span; private myUserId: string; private myMember: RoomMember; constructor(private groupCall: GroupCall, client: MatrixClient) { this.myUserId = client.getUserId(); this.myMember = groupCall.room.getMember(client.getUserId()); } public onJoinCall() { // Create a new call based on the callIdContext. This context also has a span assigned to it. // Other spans can use this context to extract the parent span. // (When passing this context to startSpan the started span will use the span set in the context (in this case the callSpan) as the parent) // Create the main span that tracks the time we intend to be in the call this.callMembershipSpan = tracer.startSpan("otel_groupCallMembershipSpan"); this.callMembershipSpan.setAttribute( "matrix.confId", this.groupCall.groupCallId ); this.callMembershipSpan.setAttribute("matrix.userId", this.myUserId); this.callMembershipSpan.setAttribute( "matrix.displayName", this.myMember.name ); opentelemetry.trace.setSpan( opentelemetry.context.active(), this.callMembershipSpan ); this.callMembershipSpan.addEvent("matrix.joinCall"); } public onLeaveCall() { this.callMembershipSpan.addEvent("matrix.leaveCall"); // and end the main span to indicate we've left if (this.callMembershipSpan) this.callMembershipSpan.end(); } public onUpdateRoomState(event: MatrixEvent) { if ( !event || (!event.getType().startsWith("m.call") && !event.getType().startsWith("org.matrix.msc3401.call")) ) { return; } this.callMembershipSpan.addEvent( `otel_onRoomStateEvent_${event.getType()}`, flattenVoipEvent(event.getContent()) ); } public onSendEvent(event: VoipEvent) { const eventType = event.eventType as string; if (!eventType.startsWith("m.call")) return; if (event.type === "toDevice") { this.callMembershipSpan.addEvent( `matrix.sendToDeviceEvent_${event.eventType}`, flattenVoipEvent(event) ); } else if (event.type === "sendEvent") { this.callMembershipSpan.addEvent( `matrix.sendToRoomEvent_${event.eventType}`, flattenVoipEvent(event) ); } } }