From f6ef390c76b282474a9f96984777576a60102e0d Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Fri, 7 Jan 2022 11:57:24 +0800 Subject: [PATCH 1/5] Fix: Remove Prom. metrics on delete monitor --- server/model/monitor.js | 5 +++++ server/prometheus.js | 10 ++++++++++ server/server.js | 1 + 3 files changed, 16 insertions(+) diff --git a/server/model/monitor.js b/server/model/monitor.js index c4441d63..5a04b383 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -468,6 +468,11 @@ class Monitor extends BeanModel { this.isStop = true; } + onDelete() { + let prometheus = new Prometheus(this); + prometheus.remove(); + } + /** * Helper Method: * returns URL object for further usage diff --git a/server/prometheus.js b/server/prometheus.js index 870581d2..ebcc8fa4 100644 --- a/server/prometheus.js +++ b/server/prometheus.js @@ -84,6 +84,16 @@ class Prometheus { } } + remove() { + try { + monitor_cert_days_remaining.remove(this.monitorLabelValues); + monitor_cert_is_valid.remove(this.monitorLabelValues); + monitor_response_time.remove(this.monitorLabelValues); + monitor_status.remove(this.monitorLabelValues); + } catch (e) { + console.error(e); + } + } } module.exports = { diff --git a/server/server.js b/server/server.js index 868bbd5e..db985c43 100644 --- a/server/server.js +++ b/server/server.js @@ -733,6 +733,7 @@ exports.entryPage = "dashboard"; if (monitorID in monitorList) { monitorList[monitorID].stop(); + monitorList[monitorID].onDelete(); delete monitorList[monitorID]; } From edd2534a1ba2fbaca5b1c00db533807646d25c7a Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Fri, 7 Jan 2022 12:26:26 +0800 Subject: [PATCH 2/5] Fix: Clear metrics also on stop and edit --- server/model/monitor.js | 9 +++++++-- server/server.js | 5 +++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 5a04b383..48bd5a4c 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -466,11 +466,16 @@ class Monitor extends BeanModel { stop() { clearTimeout(this.heartbeatInterval); this.isStop = true; + + this.prometheus().remove(); } onDelete() { - let prometheus = new Prometheus(this); - prometheus.remove(); + this.prometheus().remove(); + } + + prometheus() { + return new Prometheus(this); } /** diff --git a/server/server.js b/server/server.js index db985c43..fbf0a06e 100644 --- a/server/server.js +++ b/server/server.js @@ -567,6 +567,11 @@ exports.entryPage = "dashboard"; throw new Error("Permission denied."); } + // Reset Prometheus labels + if (monitorList[monitor.id] && monitorList[monitor.id].prometheus) { + monitorList[monitor.id].prometheus().remove(); + } + bean.name = monitor.name; bean.type = monitor.type; bean.url = monitor.url; From 1e92487f30639343b9c0bf0cf138100d5adc4b9e Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Fri, 7 Jan 2022 12:28:08 +0800 Subject: [PATCH 3/5] Chore: Remove onDelete as unused --- server/model/monitor.js | 4 ---- server/server.js | 1 - 2 files changed, 5 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 48bd5a4c..f9993917 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -470,10 +470,6 @@ class Monitor extends BeanModel { this.prometheus().remove(); } - onDelete() { - this.prometheus().remove(); - } - prometheus() { return new Prometheus(this); } diff --git a/server/server.js b/server/server.js index fbf0a06e..3e888c92 100644 --- a/server/server.js +++ b/server/server.js @@ -738,7 +738,6 @@ exports.entryPage = "dashboard"; if (monitorID in monitorList) { monitorList[monitorID].stop(); - monitorList[monitorID].onDelete(); delete monitorList[monitorID]; } From 2e0e35a1ee982ae21452035648c7f707f404498c Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Fri, 7 Jan 2022 12:34:01 +0800 Subject: [PATCH 4/5] Fix: Fix typo --- server/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/server.js b/server/server.js index 3e888c92..7db702a9 100644 --- a/server/server.js +++ b/server/server.js @@ -568,7 +568,7 @@ exports.entryPage = "dashboard"; } // Reset Prometheus labels - if (monitorList[monitor.id] && monitorList[monitor.id].prometheus) { + if (monitorList[monitor.id] && monitorList[monitor.id].prometheus()) { monitorList[monitor.id].prometheus().remove(); } From 1bbd744d0285bcb5bda457165221a3da97bc79d3 Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Fri, 7 Jan 2022 14:29:42 +0800 Subject: [PATCH 5/5] Chore: Improve syntax --- server/server.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/server/server.js b/server/server.js index 7db702a9..821fccc7 100644 --- a/server/server.js +++ b/server/server.js @@ -568,9 +568,7 @@ exports.entryPage = "dashboard"; } // Reset Prometheus labels - if (monitorList[monitor.id] && monitorList[monitor.id].prometheus()) { - monitorList[monitor.id].prometheus().remove(); - } + monitorList[monitor.id]?.prometheus()?.remove(); bean.name = monitor.name; bean.type = monitor.type;