From 99b4c11123410aff707b517f03ac670ae732dfc1 Mon Sep 17 00:00:00 2001 From: Vee Date: Thu, 14 Sep 2023 03:46:50 +0900 Subject: [PATCH] autostake: Health: Send logs with tenant to Slack --- src/autostake/Health.mjs | 44 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/autostake/Health.mjs b/src/autostake/Health.mjs index 3521ca462..41fde3728 100644 --- a/src/autostake/Health.mjs +++ b/src/autostake/Health.mjs @@ -4,8 +4,9 @@ import { timeStamp } from '../utils/Helpers.mjs' class Health { constructor(config, opts) { - const { address, uuid, name, apiKey, timeout, gracePeriod } = config || {} + const { tenant, address, uuid, name, apiKey, timeout, gracePeriod } = config || {} const { dryRun, networkName } = opts || {} + this.tenant = tenant this.address = address || 'https://hc-ping.com' this.name = name || networkName this.gracePeriod = gracePeriod || 86400 // default 24 hours @@ -20,7 +21,9 @@ class Health { // This is necessary as the default provider - hc-ping.com - has a built in ping mechanism // whereas providing self-hosted addresses do NOT. // https://healthchecks.selfhosted.com/ping/{uuid} rather than https://hc-ping.com/{uuid} - this.address = this.address + "/ping" + if (!this.address.includes("slack")) { + this.address = this.address + "/ping" + } } } @@ -80,10 +83,43 @@ class Health { if (!this.uuid) return if (this.dryRun) return timeStamp('DRYRUN: Skipping health check ping') + let target_url = "" + let dat2 = "" + if (this.address.includes("hc-ping") || this.address.includes("/ping")) { + target_url = _.compact([this.address, this.uuid, action]).join('/') + dat2 = logs.join("\n") + } else { + let msg = "" + + if (typeof action === 'undefined') { + msg = "restake: " + this.name + " " + this.tenant + "\n" + "OK\n" + logs.join("\n") + } else { + msg = "restake: " + this.name + " " + this.tenant + "\n" + action + "\n" + logs.join("\n") + } + + let dat = { + text: msg + } + + target_url = _.compact([this.address]).join('/') + dat2 = JSON.stringify(dat) + + // if tx failed, alert #cosmos-alerts in addition to #cosmos-restake-logs + if (action == "fail") { + axios.request({ + method: 'POST', + url: this.uuid, + data: dat2 + }).catch(error => { + timeStamp('Health extra ping failed', error.message) + }) + } + } + return axios.request({ method: 'POST', - url: _.compact([this.address, this.uuid, action]).join('/'), - data: logs.join("\n") + url: target_url, + data: dat2 }).catch(error => { timeStamp('Health ping failed', error.message) })