2023-03-15 14:35:10 +00:00
|
|
|
/*
|
|
|
|
|
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, { Context, Span } from "@opentelemetry/api";
|
2023-03-17 17:01:59 +00:00
|
|
|
import {
|
|
|
|
|
GroupCall,
|
|
|
|
|
MatrixClient,
|
|
|
|
|
MatrixEvent,
|
|
|
|
|
RoomMember,
|
|
|
|
|
} from "matrix-js-sdk";
|
2023-03-15 16:00:39 +00:00
|
|
|
import { VoipEvent } from "matrix-js-sdk/src/webrtc/call";
|
2023-03-15 14:35:10 +00:00
|
|
|
|
|
|
|
|
import { tracer } from "./otel";
|
|
|
|
|
|
2023-03-15 16:00:39 +00:00
|
|
|
/**
|
|
|
|
|
* Recursively sets the contents of a todevice event object as attributes on a span
|
|
|
|
|
*/
|
2023-03-16 18:08:28 +00:00
|
|
|
function setNestedAttributesFromEvent(span: Span, event: VoipEvent) {
|
2023-03-15 16:00:39 +00:00
|
|
|
setSpanEventAttributesRecursive(
|
|
|
|
|
span,
|
|
|
|
|
event as unknown as Record<string, unknown>, // XXX Types
|
2023-03-16 14:41:55 +00:00
|
|
|
"matrix.event.",
|
2023-03-15 16:00:39 +00:00
|
|
|
0
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setSpanEventAttributesRecursive(
|
|
|
|
|
span: Span,
|
|
|
|
|
obj: Record<string, unknown>,
|
|
|
|
|
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)) {
|
|
|
|
|
span.setAttribute(prefix + k, v as string | number);
|
|
|
|
|
} else if (typeof v === "object") {
|
|
|
|
|
setSpanEventAttributesRecursive(
|
|
|
|
|
span,
|
|
|
|
|
v as Record<string, unknown>,
|
|
|
|
|
prefix + k + ".",
|
|
|
|
|
depth + 1
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 14:35:10 +00:00
|
|
|
/**
|
|
|
|
|
* Represent the span of time which we intend to be joined to a group call
|
|
|
|
|
*/
|
|
|
|
|
export class OTelGroupCallMembership {
|
|
|
|
|
private context: Context;
|
|
|
|
|
private callMembershipSpan: Span;
|
2023-03-17 17:01:59 +00:00
|
|
|
private myUserId: string;
|
|
|
|
|
private myMember: RoomMember;
|
2023-03-15 14:35:10 +00:00
|
|
|
|
2023-03-17 17:01:59 +00:00
|
|
|
constructor(private groupCall: GroupCall, client: MatrixClient) {
|
|
|
|
|
this.myUserId = client.getUserId();
|
|
|
|
|
this.myMember = groupCall.room.getMember(client.getUserId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public onJoinCall() {
|
2023-03-16 14:41:55 +00:00
|
|
|
// 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)
|
2023-03-15 14:35:10 +00:00
|
|
|
|
2023-03-17 17:01:59 +00:00
|
|
|
// Create the main span that tracks the time we intend to be in the call
|
|
|
|
|
this.callMembershipSpan = tracer.startSpan("otel_groupCallMembershipSpan");
|
|
|
|
|
|
2023-03-16 14:41:55 +00:00
|
|
|
this.context = opentelemetry.trace
|
|
|
|
|
.setSpan(opentelemetry.context.active(), this.callMembershipSpan)
|
2023-03-17 17:01:59 +00:00
|
|
|
.setValue(Symbol("confId"), this.groupCall.groupCallId)
|
|
|
|
|
.setValue(Symbol("matrix.userId"), this.myUserId)
|
|
|
|
|
.setValue(Symbol("matrix.displayName"), this.myMember.name);
|
2023-03-15 14:35:10 +00:00
|
|
|
|
|
|
|
|
// Here we start a very short span. This is a hack to trigger the posthog exporter.
|
|
|
|
|
// Only ended spans are processed by the exporter.
|
|
|
|
|
// We want the exporter to know that a call has started
|
|
|
|
|
const joinCallSpan = tracer.startSpan(
|
|
|
|
|
"otel_joinCallSpan",
|
|
|
|
|
undefined,
|
|
|
|
|
this.context
|
|
|
|
|
);
|
|
|
|
|
joinCallSpan.end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public onLeaveCall() {
|
|
|
|
|
// A very short span to represent us leaving the call
|
|
|
|
|
const startCallSpan = tracer.startSpan(
|
|
|
|
|
"otel_leaveCallSpan",
|
|
|
|
|
undefined,
|
|
|
|
|
this.context
|
|
|
|
|
);
|
|
|
|
|
startCallSpan.end();
|
|
|
|
|
|
|
|
|
|
// and end the main span to indicate we've left
|
2023-03-16 14:41:55 +00:00
|
|
|
if (this.callMembershipSpan) this.callMembershipSpan.end();
|
2023-03-15 14:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
2023-03-16 18:08:28 +00:00
|
|
|
public onUpdateRoomState(event: MatrixEvent) {
|
|
|
|
|
if (
|
|
|
|
|
!event ||
|
|
|
|
|
(!event.getType().startsWith("m.call") &&
|
|
|
|
|
!event.getType().startsWith("org.matrix.msc3401.call"))
|
|
|
|
|
)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const span = tracer.startSpan(
|
|
|
|
|
`otel_onRoomStateEvent_${event.getType()}`,
|
|
|
|
|
undefined,
|
|
|
|
|
this.context
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
setNestedAttributesFromEvent(span, event.getContent());
|
|
|
|
|
span.end();
|
|
|
|
|
}
|
2023-03-15 14:35:10 +00:00
|
|
|
|
2023-03-15 16:00:39 +00:00
|
|
|
public onSendEvent(event: VoipEvent) {
|
|
|
|
|
const eventType = event.eventType as string;
|
2023-03-15 14:35:10 +00:00
|
|
|
if (!eventType.startsWith("m.call")) return;
|
|
|
|
|
|
2023-03-15 16:00:39 +00:00
|
|
|
if (event.type === "toDevice") {
|
|
|
|
|
const span = tracer.startSpan(
|
|
|
|
|
`otel_sendToDeviceEvent_${event.eventType}`,
|
|
|
|
|
undefined,
|
|
|
|
|
this.context
|
|
|
|
|
);
|
2023-03-15 14:35:10 +00:00
|
|
|
|
2023-03-16 18:08:28 +00:00
|
|
|
setNestedAttributesFromEvent(span, event);
|
|
|
|
|
span.end();
|
|
|
|
|
} else if (event.type === "sendEvent") {
|
|
|
|
|
const span = tracer.startSpan(
|
|
|
|
|
`otel_sendToRoomEvent_${event.eventType}`,
|
|
|
|
|
undefined,
|
|
|
|
|
this.context
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
setNestedAttributesFromEvent(span, event);
|
2023-03-15 16:00:39 +00:00
|
|
|
span.end();
|
2023-03-15 14:35:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|