From 71734f0932a61d7645568ea66eb4812149503d49 Mon Sep 17 00:00:00 2001 From: Anas Date: Sun, 21 Nov 2021 10:43:04 +0400 Subject: [PATCH 1/2] Added logs for create check function --- targets/heathcheckIo.js | 55 +++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/targets/heathcheckIo.js b/targets/heathcheckIo.js index d178fa4..c2286e3 100644 --- a/targets/heathcheckIo.js +++ b/targets/heathcheckIo.js @@ -31,6 +31,7 @@ const ping = async ({ interval, job, team }) => { const pingResponse = await connection.post(pingUrl, `team: ${team}, job: ${job}, interval: ${interval}`); logger.info(`Ping Response: ${pingResponse.data} for check: ${checkName} - ${pingUrl}`); } catch (err) { + delete pingUrlCache[checkName]; logger.error(`Failed to ping healthcheck ping-api: ${checkName}`, { errorMessage: err.message, errorStack: err.stack @@ -40,28 +41,40 @@ const ping = async ({ interval, job, team }) => { let pingUrlCache = {}; const getPingUrl = async (checkName, job = null, team = null) => { - if(pingUrlCache[checkName]){ - return pingUrlCache[checkName]; - } - const connection = createConnection(_.get(config, 'healthcheckio.checksApiConfig', {})) - let notificationChannels = _.get(config, `healthcheckio.alertConfig.notificationsChannel.${team ? team: 'default'}`); - const checkConfig = { - "name": checkName, - "timeout": _.get(config, `healthcheckio.alertConfig.thresholds.${job}.timeOut`) || _.get(config, `healthcheckio.alertConfig.thresholds.default.timeOut`), - "grace": _.get(config, `healthcheckio.alertConfig.thresholds.${job}.grace`) || _.get(config, `healthcheckio.alertConfig.thresholds.default.grace`), - "unique": ["name"], - "channels": notificationChannels - } - const response = await connection.post('api/v1/checks/', checkConfig); - const pingUrl = _.get(response, 'data.ping_url', ''); - if (!pingUrl) { - logger.error(`Failed to create/retrieve the check: ${checkName}`) - throw new Error(`Failed to create/retrieve healthcheck for ${checkName}`) - } + try { + if (pingUrlCache[checkName]) { + return pingUrlCache[checkName]; + } + const connection = createConnection(_.get(config, 'healthcheckio.checksApiConfig', {})) + let notificationChannels = _.get(config, `healthcheckio.alertConfig.notificationsChannel.${team ? team : 'default'}`); + const checkConfig = { + "name": checkName, + "timeout": _.get(config, `healthcheckio.alertConfig.thresholds.${job}.timeOut`) || _.get(config, `healthcheckio.alertConfig.thresholds.default.timeOut`), + "grace": _.get(config, `healthcheckio.alertConfig.thresholds.${job}.grace`) || _.get(config, `healthcheckio.alertConfig.thresholds.default.grace`), + "unique": ["name"], + "channels": notificationChannels + } + const response = await connection.post('api/v1/checks/', checkConfig); - //Cache the result - pingUrlCache[checkName] = pingUrl; - return pingUrl; + const pingUrl = _.get(response, 'data.ping_url', ''); + if (!pingUrl) { + logger.error(`Failed to create/retrieve the check: ${checkName}`) + throw new Error(`Failed to create/retrieve healthcheck for ${checkName}`) + } + + //Cache the result + pingUrlCache[checkName] = pingUrl; + return pingUrl; + } catch (err) { + logger.error(`Failed to create/get ping-api: ${checkName}-${job}`, { + checkName, job, team: team ? team : '', + errorMessage: err.message, + errorStack: err.stack, + error:err + }); + + throw err; + } } From 9d6fb5b6ae159f3f2288aec273b5245718db1ef8 Mon Sep 17 00:00:00 2001 From: Anas Date: Sun, 21 Nov 2021 11:50:37 +0400 Subject: [PATCH 2/2] Added debug logs --- targets/heathcheckIo.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/targets/heathcheckIo.js b/targets/heathcheckIo.js index c2286e3..8329982 100644 --- a/targets/heathcheckIo.js +++ b/targets/heathcheckIo.js @@ -54,8 +54,13 @@ const getPingUrl = async (checkName, job = null, team = null) => { "unique": ["name"], "channels": notificationChannels } + logger.info(`Debug - create check payload - ${checkName}`, { + checkConfig + }) const response = await connection.post('api/v1/checks/', checkConfig); - + logger.info(`Debug - create check response - ${checkName}`, { + response + }) const pingUrl = _.get(response, 'data.ping_url', ''); if (!pingUrl) { logger.error(`Failed to create/retrieve the check: ${checkName}`)