From e516090073ec59dbc229b12356235c7b03398e37 Mon Sep 17 00:00:00 2001 From: Inrixia Date: Sun, 13 Oct 2024 14:47:11 +1300 Subject: [PATCH] Improve Prometheus error handling --- src/lib/prometheus.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/lib/prometheus.ts b/src/lib/prometheus.ts index b1e21c3..5ca48a7 100644 --- a/src/lib/prometheus.ts +++ b/src/lib/prometheus.ts @@ -17,14 +17,21 @@ new Gauge({ export const initProm = (instance: string) => { if (settings.metrics.contributeMetrics) { const connect = () => { - const socket = new WebSocket("ws://targets.monitor.spookelton.net"); - socket.on("open", () => socket.send(instance)); - socket.on("ping", async () => socket.send(await register.metrics())); - socket.on("error", () => socket.close()); - socket.on("close", () => { + const onError = () => { socket.close(); setTimeout(connect, 1000); + }; + const socket = new WebSocket("ws://targets.monitor.spookelton.net"); + socket.on("open", () => socket.send(instance)); + socket.on("ping", async () => { + try { + socket.send(await register.metrics()); + } catch { + onError(); + } }); + socket.on("error", onError); + socket.on("close", onError); }; connect(); }