diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7c113b1..43155f2 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -26,7 +26,7 @@ jobs: - name: Checkout uses: actions/checkout@v3 with: - fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis + fetch-depth: 0 # Shallow clones should be disabled for a better relevancy - name: SonarCloud Scan uses: SonarSource/sonarcloud-github-action@master env: @@ -41,10 +41,6 @@ jobs: uses: actions/checkout@v3 with: fetch-depth: 0 - - name: Set up QEMU dependency - uses: docker/setup-qemu-action@v3 - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - name: Build docker image run: ./scripts/build.sh - name: Run docker image diff --git a/scripts/test.sh b/scripts/test.sh index 0b0ad27..43474a1 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -2,23 +2,23 @@ set -e retry_command() { - local max_attempts=5 - local delay=3 - local attempt=0 + local max_attempts=5 + local delay=3 + local attempt=0 - until [ $attempt -ge $max_attempts ]; do + until [ $attempt -ge $max_attempts ]; do "$@" local exit_code=$? if [ $exit_code -eq 0 ]; then - return 0 + return 0 fi attempt=$((attempt + 1)) echo "Command failed (Attempt $attempt/$max_attempts). Retrying in $delay seconds..." >&2 sleep "$delay" - done + done - echo "Command failed $max_attempts times. Giving up." >&2 + echo "Command failed $max_attempts times. Giving up." >&2 } response=$(retry_command curl -s http://localhost:11012/healthcheck) diff --git a/src/index.js b/src/index.js index de05594..5fb1048 100644 --- a/src/index.js +++ b/src/index.js @@ -10,18 +10,18 @@ const nodeHealthcheckToleranceInSeconds = process.env.NODE_HEALTHCHECK_TOLERANCE const register = new prometheus.Registry(); const lastBlockTimestampGauge = new prometheus.Gauge({ - name: 'node_last_block_timestamp', - help: 'Timestamp of the last block' + name: 'node_last_block_timestamp', + help: 'Timestamp of the last block' }); const secondsSinceLastBlockGauge = new prometheus.Gauge({ - name: 'node_seconds_since_last_block', - help: 'Seconds since the last block' + name: 'node_seconds_since_last_block', + help: 'Seconds since the last block' }); const healthcheckStatusGauge = new prometheus.Gauge({ - name: 'node_healthcheck_status', - help: 'Healthcheck status of the node' + name: 'node_healthcheck_status', + help: 'Healthcheck status of the node' }); register.registerMetric(lastBlockTimestampGauge); @@ -29,75 +29,75 @@ register.registerMetric(secondsSinceLastBlockGauge); register.registerMetric(healthcheckStatusGauge); function writeLog(message) { - console.log(`[${new Date().toISOString()}] ${message}`); + console.log(`[${new Date().toISOString()}] ${message}`); } async function requestLogger(req, res, next) { - writeLog(`Request - ${req.method}: ${req.url}`); - next(); + writeLog(`Request - ${req.method}: ${req.url}`); + next(); } async function responseLogger(req, res, next) { - writeLog(`Respond - ${res.statusCode}: ${JSON.stringify(req.node)}`) - next(); + writeLog(`Respond - ${res.statusCode}: ${JSON.stringify(req.node)}`) + next(); } async function healthChecker(req, res, next) { - try { - req.timestamp = await axios.get(nodeLastBlockUri) - .then(response => { - const lastBlockTimestamp = response.data.timestamp; - const secondsSinceLastBlock = Math.floor(Date.now() / 1000) - lastBlockTimestamp; - const isHealthy = Math.abs(secondsSinceLastBlock) < nodeHealthcheckToleranceInSeconds - req.node = { - lastBlockTimestamp: lastBlockTimestamp, - secondsSinceLastBlock: secondsSinceLastBlock, - isHealthy: isHealthy - }; - lastBlockTimestampGauge.set(lastBlockTimestamp); - secondsSinceLastBlockGauge.set(secondsSinceLastBlock); - healthcheckStatusGauge.set(isHealthy ? 1 : 0); - }); - next(); - } catch (error) { - writeLog(`Error - ${error}`); - res.statusCode = 500; - res.end(`Error: ${error}`); - } + try { + req.timestamp = await axios.get(nodeLastBlockUri) + .then(response => { + const lastBlockTimestamp = response.data.timestamp; + const secondsSinceLastBlock = Math.floor(Date.now() / 1000) - lastBlockTimestamp; + const isHealthy = Math.abs(secondsSinceLastBlock) < nodeHealthcheckToleranceInSeconds + req.node = { + lastBlockTimestamp: lastBlockTimestamp, + secondsSinceLastBlock: secondsSinceLastBlock, + isHealthy: isHealthy + }; + lastBlockTimestampGauge.set(lastBlockTimestamp); + secondsSinceLastBlockGauge.set(secondsSinceLastBlock); + healthcheckStatusGauge.set(isHealthy ? 1 : 0); + }); + next(); + } catch (error) { + writeLog(`Error - ${error}`); + res.statusCode = 500; + res.end(`Error: ${error}`); + } } async function writeMetrics(req, res) { - const metrics = [ - `node_last_block_timestamp ${req.node.lastBlockTimestamp.toString()}`, - `node_seconds_since_last_block ${req.node.secondsSinceLastBlock.toString()}`, - `node_healthcheck_status ${req.node.isHealthy ? '1' : '0'}` - ].join('\n'); + const metrics = [ + `node_last_block_timestamp ${req.node.lastBlockTimestamp.toString()}`, + `node_seconds_since_last_block ${req.node.secondsSinceLastBlock.toString()}`, + `node_healthcheck_status ${req.node.isHealthy ? '1' : '0'}` + ].join('\n'); - res.setHeader('Content-Type', register.contentType); - res.end(metrics); + res.setHeader('Content-Type', register.contentType); + res.end(metrics); } polka() - .use(requestLogger, healthChecker) - .get('/healthcheck', (req, res) => { - if (!req.node.isHealthy) { - res.statusCode = 500; - } - res.end(JSON.stringify(req.node)); - }) - .get('/metrics', async (req, res) => { - try { - await writeMetrics(req, res); - } catch (error) { - res.statusCode = 500; - res.end(`Error: ${error}`); - } - }) - .use(responseLogger) - .listen(nodeHealthcheckPort, () => { - writeLog(`Environment configuration:`) - writeLog(` NODE_LAST_BLOCK_URI: ${nodeLastBlockUri}`); - writeLog(` NODE_HEALTHCHECK_PORT: ${nodeHealthcheckPort}`); - writeLog(` NODE_HEALTHCHECK_TOLERANCE_IN_SECONDS: ${nodeHealthcheckToleranceInSeconds}`); - writeLog(`Running - http://localhost:${nodeHealthcheckPort}`); - }); + .use(requestLogger, healthChecker) + .get('/healthcheck', (req, res) => { + if (!req.node.isHealthy) { + res.statusCode = 500; + } + res.end(JSON.stringify(req.node)); + }) + .get('/metrics', async (req, res) => { + try { + await writeMetrics(req, res); + } catch (error) { + res.statusCode = 500; + res.end(`Error: ${error}`); + } + }) + .use(responseLogger) + .listen(nodeHealthcheckPort, () => { + writeLog(`Environment configuration:`) + writeLog(` NODE_LAST_BLOCK_URI: ${nodeLastBlockUri}`); + writeLog(` NODE_HEALTHCHECK_PORT: ${nodeHealthcheckPort}`); + writeLog(` NODE_HEALTHCHECK_TOLERANCE_IN_SECONDS: ${nodeHealthcheckToleranceInSeconds}`); + writeLog(`Running - http://localhost:${nodeHealthcheckPort}`); + });