feat: enhance health checks in docker-compose and improve WebRTC stats collection

This commit is contained in:
2025-02-09 02:10:53 +01:00
parent be0e0f8153
commit 7b78f54510
6 changed files with 138 additions and 268 deletions

View File

@@ -31,15 +31,55 @@ class WebrtcInternalExporter {
);
}
/**
* @param {RTCPeerConnection} pc
*/
add(pc) {
const id = this.randomId();
pc.iceCandidates = [];
pc.iceCandidateErrors = [];
this.peerConnections.set(id, pc);
pc.addEventListener("connectionstatechange", () => {
if (pc.connectionState === "closed") {
this.peerConnections.delete(id);
}
});
//this.collectAndPostStats(id);
/**
* @param {RTCPeerConnectionIceErrorEvent} event
*/
pc.addEventListener("icecandidateerror", (event) => {
this.peerConnections.get(id).iceCandidateErrors.push({
timestamp: Date.now(),
address: event.errorAddress,
errorCode: event.errorCode,
errorText: event.errorText,
port: event.errorPort,
url: event.url,
});
});
/**
* @param {RTCPeerConnectionIceEvent} event
*/
pc.addEventListener("icecandidate", (event) => {
this.peerConnections.get(id).iceCandidates.push({
timestamp: Date.now(),
candidate: event.candidate?.candidate,
component: event.candidate?.component,
foundation: event.candidate?.foundation,
port: event.candidate?.port,
priority: event.candidate?.priority,
protocol: event.candidate?.protocol,
relatedAddress: event.candidate?.relatedAddress,
relatedPort: event.candidate?.relatedPort,
sdpMLineIndex: event.candidate?.sdpMLineIndex,
sdpMid: event.candidate?.sdpMid,
tcpType: event.candidate?.tcpType,
type: event.candidate?.type,
usernameFragment: event.candidate?.usernameFragment,
});
});
}
async collectAndPostSingleStat(id) {
@@ -49,9 +89,9 @@ class WebrtcInternalExporter {
window.postMessage(
{
event: "webrtc-internal-exporter:peer-connection-stats",
...stats,
stats
},
[stats],
stats
);
}
@@ -59,26 +99,30 @@ class WebrtcInternalExporter {
const stats = [];
for (const [id, pc] of this.peerConnections) {
if (this.url && this.enabled && pc.connectionState === "connected") {
if (this.url && this.enabled) {
const pcStats = await this.collectStats(id, pc);
stats.push(pcStats);
}
}
window.postMessage(
{
event: "webrtc-internal-exporter:peer-connections-stats",
stats,
},
[stats],
);
window.postMessage(
{
event: "webrtc-internal-exporter:peer-connections-stats",
data: JSON.parse(JSON.stringify(stats)),
},
);
log(`Stats collected:`, stats);
log(`Stats collected:`, JSON.parse(JSON.stringify(stats)));
setTimeout(this.collectAllStats.bind(this), this.updateInterval);
return stats;
setTimeout(this.collectAllStats.bind(this), this.updateInterval);
return stats;
}
/**
* @param {string} id
* @param {RTCPeerConnection} pc
* @param {Function} binding
*/
async collectStats(id, pc, binding) {
var completeStats = {};
@@ -87,7 +131,7 @@ class WebrtcInternalExporter {
if (!pc) return;
}
if (this.url && this.enabled && pc.connectionState === "connected") {
if (this.url && this.enabled) {
try {
const stats = await pc.getStats();
const values = [...stats.values()].filter(
@@ -98,7 +142,12 @@ class WebrtcInternalExporter {
completeStats = {
url: window.location.href,
id,
state: pc.connectionState,
connectionState: pc.connectionState,
iceConnectionState: pc.iceConnectionState,
iceGatheringState: pc.iceGatheringState,
signalingState: pc.signalingState,
iceCandidateErrors: pc.iceCandidateErrors,
iceCandidates: pc.iceCandidates,
values,
};
} catch (error) {