Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/diagnostics.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { parseSfuMetrics } from "./diagnostics";
import { parseSfuMetrics, sfuMetricsUrl } from "./diagnostics";

describe("parseSfuMetrics", () => {
it("keeps gryt_sfu series, sums labelled ones and drops the rest", () => {
Expand All @@ -19,3 +19,13 @@ describe("parseSfuMetrics", () => {
});
});
});

describe("sfuMetricsUrl", () => {
it("prefers SFU_METRICS_URL, and otherwise guesses port 9091 on the SFU's host", () => {
assert.equal(sfuMetricsUrl({ SFU_METRICS_URL: "http://x:1/m", SFU_WS_HOST: "ws://sfu:9092" }), "http://x:1/m");
assert.equal(sfuMetricsUrl({ SFU_WS_HOST: "ws://sfu:9092" }), "http://sfu:9091/metrics");
assert.equal(sfuMetricsUrl({ SFU_WS_HOST: "wss://sfu.example.com" }), "http://sfu.example.com:9091/metrics");
assert.equal(sfuMetricsUrl({ SFU_WS_HOST: "ws://[::1]:9092" }), "http://[::1]:9091/metrics");
assert.equal(sfuMetricsUrl({}), undefined);
});
});
15 changes: 13 additions & 2 deletions src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface DiagnosticsSnapshot {
uptimeSeconds: number;
online: number;
sfu: ReturnType<SFUClient["getConnectionStatus"]> | null;
/** Null when SFU_METRICS_URL is unset or the SFU did not answer. */
/** Null when the SFU's metrics did not answer. */
sfuMetrics: Record<string, number> | null;
voice: {
channelId: string;
Expand Down Expand Up @@ -34,6 +34,17 @@ export function parseSfuMetrics(text: string): Record<string, number> {
return out;
}

/** SFU_METRICS_URL, or the SFU's default metrics port on the host SFU_WS_HOST names. */
export function sfuMetricsUrl(env: NodeJS.ProcessEnv = process.env): string | undefined {
if (env.SFU_METRICS_URL) return env.SFU_METRICS_URL;
try {
const host = new URL(env.SFU_WS_HOST ?? "").hostname;
return host ? `http://${host}:9091/metrics` : undefined;
} catch {
return undefined;
}
}

async function readSfuMetrics(url: string | undefined): Promise<Record<string, number> | null> {
if (!url) return null;
try {
Expand Down Expand Up @@ -72,7 +83,7 @@ export async function collectDiagnostics(
uptimeSeconds: Math.round(process.uptime()),
online,
sfu: sfuClient?.getConnectionStatus() ?? null,
sfuMetrics: await readSfuMetrics(process.env.SFU_METRICS_URL),
sfuMetrics: await readSfuMetrics(sfuMetricsUrl()),
voice: [...rooms].map(([channelId, members]) => ({ channelId, members })),
};
}
Loading