diff --git a/src/otel/OTelGroupCallMembership.ts b/src/otel/OTelGroupCallMembership.ts index 9f7573e..5ca7689 100644 --- a/src/otel/OTelGroupCallMembership.ts +++ b/src/otel/OTelGroupCallMembership.ts @@ -45,48 +45,6 @@ import { ElementCallOpenTelemetry } from "./otel"; import { ObjectFlattener } from "./ObjectFlattener"; import { OTelCall } from "./OTelCall"; -/** - * 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", "boolean"].includes(typeof v) || v === null) { - 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 */ @@ -179,7 +137,7 @@ export class OTelGroupCallMembership { this.callMembershipSpan?.addEvent( `matrix.roomStateEvent_${event.getType()}`, - flattenVoipEvent(event.getContent()) + ObjectFlattener.flattenVoipEvent(event.getContent()) ); } @@ -247,12 +205,12 @@ export class OTelGroupCallMembership { if (event.type === "toDevice") { callTrackingInfo.span.addEvent( `matrix.sendToDeviceEvent_${event.eventType}`, - flattenVoipEvent(event) + ObjectFlattener.flattenVoipEvent(event) ); } else if (event.type === "sendEvent") { callTrackingInfo.span.addEvent( `matrix.sendToRoomEvent_${event.eventType}`, - flattenVoipEvent(event) + ObjectFlattener.flattenVoipEvent(event) ); } } @@ -284,7 +242,7 @@ export class OTelGroupCallMembership { call.span.addEvent("matrix.receive_voip_event", { "sender.userId": event.getSender(), - ...flattenVoipEvent(event.getContent()), + ...ObjectFlattener.flattenVoipEvent(event.getContent()), }); } diff --git a/src/otel/ObjectFlattener.ts b/src/otel/ObjectFlattener.ts index dcda078..321c45e 100644 --- a/src/otel/ObjectFlattener.ts +++ b/src/otel/ObjectFlattener.ts @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ import { Attributes } from "@opentelemetry/api"; +import { VoipEvent } from "matrix-js-sdk/src/webrtc/call"; import { GroupCallStatsReport } from "matrix-js-sdk/src/webrtc/groupCall"; import { ByteSentStatsReport, @@ -61,6 +62,21 @@ export class ObjectFlattener { return flatObject; } + /* Flattens out an object into a single layer with components + * of the key separated by dots + */ + public static flattenVoipEvent(event: VoipEvent): Attributes { + const flatObject = {}; + ObjectFlattener.flattenObjectRecursive( + event as unknown as Record, // XXX Types + flatObject, + "matrix.event.", + 0 + ); + + return flatObject; + } + public static flattenObjectRecursive( obj: Object, flatObject: Attributes, diff --git a/src/otel/otel.ts b/src/otel/otel.ts index 452bd83..e30e84a 100644 --- a/src/otel/otel.ts +++ b/src/otel/otel.ts @@ -42,7 +42,7 @@ export class ElementCallOpenTelemetry { const config = Config.get(); // we always enable opentelemetry in general. We only enable the OTLP // collector if a URL is defined (and in future if another setting is defined) - // The posthog exporteer is always enabled, posthog reporting is enabled or disabled + // The posthog exporter is always enabled, posthog reporting is enabled or disabled // within the posthog code. const shouldEnableOtlp = Boolean(config.opentelemetry?.collector_url); @@ -65,7 +65,7 @@ export class ElementCallOpenTelemetry { collectorUrl: string | undefined, rageshakeUrl: string | undefined ) { - // This is how we can make Jaeger show a reaonsable service in the dropdown on the left. + // This is how we can make Jaeger show a reasonable service in the dropdown on the left. const providerConfig = { resource: new Resource({ [SemanticResourceAttributes.SERVICE_NAME]: SERVICE_NAME,