2023-04-05 10:25:26 +02:00
|
|
|
/*
|
2024-09-06 10:22:13 +02:00
|
|
|
Copyright 2023, 2024 New Vector Ltd.
|
2023-04-05 10:25:26 +02:00
|
|
|
|
2025-02-18 17:59:58 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-06 10:22:13 +02:00
|
|
|
Please see LICENSE in the repository root for full details.
|
2023-04-05 10:25:26 +02:00
|
|
|
*/
|
2024-12-11 09:27:55 +00:00
|
|
|
import { type Attributes } from "@opentelemetry/api";
|
2025-03-13 16:58:14 +01:00
|
|
|
import { type VoipEvent } from "matrix-js-sdk/lib/webrtc/call";
|
|
|
|
|
import { type GroupCallStatsReport } from "matrix-js-sdk/lib/webrtc/groupCall";
|
2023-04-05 10:25:26 +02:00
|
|
|
import {
|
2024-12-11 09:27:55 +00:00
|
|
|
type ByteSentStatsReport,
|
|
|
|
|
type ConnectionStatsReport,
|
|
|
|
|
type SummaryStatsReport,
|
2025-03-13 16:58:14 +01:00
|
|
|
} from "matrix-js-sdk/lib/webrtc/stats/statsReport";
|
2023-04-05 10:25:26 +02:00
|
|
|
|
|
|
|
|
export class ObjectFlattener {
|
2023-06-06 08:28:53 +02:00
|
|
|
public static flattenReportObject(
|
|
|
|
|
prefix: string,
|
2023-10-11 10:42:04 -04:00
|
|
|
report: ConnectionStatsReport | ByteSentStatsReport,
|
2023-04-05 10:25:26 +02:00
|
|
|
): Attributes {
|
|
|
|
|
const flatObject = {};
|
2023-06-06 08:28:53 +02:00
|
|
|
ObjectFlattener.flattenObjectRecursive(report, flatObject, `${prefix}.`, 0);
|
2023-04-05 10:25:26 +02:00
|
|
|
return flatObject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static flattenByteSentStatsReportObject(
|
2023-10-11 10:42:04 -04:00
|
|
|
statsReport: GroupCallStatsReport<ByteSentStatsReport>,
|
2023-04-05 10:25:26 +02:00
|
|
|
): Attributes {
|
|
|
|
|
const flatObject = {};
|
|
|
|
|
ObjectFlattener.flattenObjectRecursive(
|
|
|
|
|
statsReport.report,
|
|
|
|
|
flatObject,
|
|
|
|
|
"matrix.stats.bytesSent.",
|
2023-10-11 10:42:04 -04:00
|
|
|
0,
|
2023-04-05 10:25:26 +02:00
|
|
|
);
|
|
|
|
|
return flatObject;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-22 18:05:13 -04:00
|
|
|
public static flattenSummaryStatsReportObject(
|
2023-10-11 10:42:04 -04:00
|
|
|
statsReport: GroupCallStatsReport<SummaryStatsReport>,
|
2023-09-22 18:05:13 -04:00
|
|
|
): Attributes {
|
2023-04-05 10:25:26 +02:00
|
|
|
const flatObject = {};
|
|
|
|
|
ObjectFlattener.flattenObjectRecursive(
|
|
|
|
|
statsReport.report,
|
|
|
|
|
flatObject,
|
|
|
|
|
"matrix.stats.summary.",
|
2023-10-11 10:42:04 -04:00
|
|
|
0,
|
2023-04-05 10:25:26 +02:00
|
|
|
);
|
|
|
|
|
return flatObject;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-27 14:00:27 +02:00
|
|
|
/* 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<string, unknown>, // XXX Types
|
|
|
|
|
flatObject,
|
|
|
|
|
"matrix.event.",
|
2023-10-11 10:42:04 -04:00
|
|
|
0,
|
2023-04-27 14:00:27 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return flatObject;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 10:25:26 +02:00
|
|
|
public static flattenObjectRecursive(
|
2024-09-03 17:14:27 +02:00
|
|
|
obj: object,
|
2023-04-05 10:25:26 +02:00
|
|
|
flatObject: Attributes,
|
|
|
|
|
prefix: string,
|
2023-10-11 10:42:04 -04:00
|
|
|
depth: number,
|
2023-04-05 10:25:26 +02:00
|
|
|
): void {
|
|
|
|
|
if (depth > 10)
|
|
|
|
|
throw new Error(
|
|
|
|
|
"Depth limit exceeded: aborting VoipEvent recursion. Prefix is " +
|
2023-10-11 10:42:04 -04:00
|
|
|
prefix,
|
2023-04-05 10:25:26 +02:00
|
|
|
);
|
|
|
|
|
let entries;
|
|
|
|
|
if (obj instanceof Map) {
|
|
|
|
|
entries = obj.entries();
|
|
|
|
|
} else {
|
|
|
|
|
entries = Object.entries(obj);
|
|
|
|
|
}
|
|
|
|
|
for (const [k, v] of entries) {
|
|
|
|
|
if (["string", "number", "boolean"].includes(typeof v) || v === null) {
|
|
|
|
|
let value;
|
|
|
|
|
value = v === null ? "null" : v;
|
|
|
|
|
value = typeof v === "number" && Number.isNaN(v) ? "NaN" : value;
|
|
|
|
|
flatObject[prefix + k] = value;
|
|
|
|
|
} else if (typeof v === "object") {
|
|
|
|
|
ObjectFlattener.flattenObjectRecursive(
|
|
|
|
|
v,
|
|
|
|
|
flatObject,
|
|
|
|
|
prefix + k + ".",
|
2023-10-11 10:42:04 -04:00
|
|
|
depth + 1,
|
2023-04-05 10:25:26 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|