From 1de18e7bec819f3d124f77a1051269d067486d86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6ran=20Sander?= Date: Fri, 9 Dec 2022 09:13:06 +0000 Subject: [PATCH] fix: Allow empty New Relic settings in config file's uptime section Fixes #562 --- src/lib/post_to_new_relic.js | 61 +++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/src/lib/post_to_new_relic.js b/src/lib/post_to_new_relic.js index de092225..2b379cee 100755 --- a/src/lib/post_to_new_relic.js +++ b/src/lib/post_to_new_relic.js @@ -103,38 +103,43 @@ async function postButlerUptimeToNewRelic(fields) { // Send data to all New Relic accounts that are enabled for this metric/event // // Get New Relic accounts - const nrAccounts = globals.config.Butler.thirdPartyToolsCredentials.newRelic; + const nrAccounts = globals.config.get('Butler.thirdPartyToolsCredentials.newRelic'); globals.logger.debug(`UPTIME NEW RELIC: Complete New Relic config=${JSON.stringify(nrAccounts)}`); - // eslint-disable-next-line no-restricted-syntax - for (const accountName of globals.config.Butler.uptimeMonitor.storeNewRelic.destinationAccount) { - globals.logger.debug(`UPTIME NEW RELIC: Current loop New Relic config=${JSON.stringify(accountName)}`); - - // Is there any config available for the current account? - const newRelicConfig = nrAccounts.filter((item) => item.accountName === accountName); - globals.logger.debug(`UPTIME NEW RELIC: New Relic config=${JSON.stringify(newRelicConfig)}`); - - if (newRelicConfig.length === 0) { - globals.logger.error(`UPTIME NEW RELIC: New Relic config "${accountName}" does not exist in the Butler config file.`); - } else { - headers['Api-Key'] = newRelicConfig[0].insertApiKey; - - // eslint-disable-next-line no-await-in-loop - const res = await axios.post(remoteUrl, payload, { headers, timeout: 5000 }); - - globals.logger.debug( - `UPTIME NEW RELIC: Result code from posting to New Relic account ${newRelicConfig[0].accountId}: ${res.status}, ${res.statusText}` - ); - if (res.status === 200 || res.status === 202) { - // Posting done without error - globals.logger.verbose( - `UPTIME NEW RELIC: Sent Butler memory usage data to New Relic account ${newRelicConfig[0].accountId}` - ); - // reply.type('application/json; charset=utf-8').code(201).send(JSON.stringify(request.body)); + // Are there any NR destinations defined for uptime metrics? + const nrDestAccounts = globals.config.get('Butler.uptimeMonitor.storeNewRelic.destinationAccount'); + + if (nrDestAccounts) { + // eslint-disable-next-line no-restricted-syntax + for (const accountName of globals.config.Butler.uptimeMonitor.storeNewRelic.destinationAccount) { + globals.logger.debug(`UPTIME NEW RELIC: Current loop New Relic config=${JSON.stringify(accountName)}`); + + // Is there any config available for the current account? + const newRelicConfig = nrAccounts.filter((item) => item.accountName === accountName); + globals.logger.debug(`UPTIME NEW RELIC: New Relic config=${JSON.stringify(newRelicConfig)}`); + + if (newRelicConfig.length === 0) { + globals.logger.error(`UPTIME NEW RELIC: New Relic config "${accountName}" does not exist in the Butler config file.`); } else { - globals.logger.error( - `UPTIME NEW RELIC: Error code from posting memory usage data to New Relic account ${newRelicConfig[0].accountId}: ${res.status}, ${res.statusText}` + headers['Api-Key'] = newRelicConfig[0].insertApiKey; + + // eslint-disable-next-line no-await-in-loop + const res = await axios.post(remoteUrl, payload, { headers, timeout: 5000 }); + + globals.logger.debug( + `UPTIME NEW RELIC: Result code from posting to New Relic account ${newRelicConfig[0].accountId}: ${res.status}, ${res.statusText}` ); + if (res.status === 200 || res.status === 202) { + // Posting done without error + globals.logger.verbose( + `UPTIME NEW RELIC: Sent Butler memory usage data to New Relic account ${newRelicConfig[0].accountId}` + ); + // reply.type('application/json; charset=utf-8').code(201).send(JSON.stringify(request.body)); + } else { + globals.logger.error( + `UPTIME NEW RELIC: Error code from posting memory usage data to New Relic account ${newRelicConfig[0].accountId}: ${res.status}, ${res.statusText}` + ); + } } } }