Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
feat(push-to-gateway): add ability to retry pushing metrics (#9)
Browse files Browse the repository at this point in the history
* feat(push-to-gateway): add ability to retry pushing metrics

* feat(push-to-gateway): fix review - use a constants file
  • Loading branch information
happypoulp authored Feb 18, 2022
1 parent 8dd6e16 commit 65b480a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
7 changes: 7 additions & 0 deletions config/jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"verbose": true,
"bail": true,
"rootDir": "../",
"roots": ["<rootDir>/src"],
"silent": true
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Github action for compute and monitor of test coverage via jest",
"main": "index.js",
"scripts": {
"test": "jest src/"
"test": "jest src/ --config ./config/jest.config.json"
},
"repository": {
"type": "git",
Expand Down
4 changes: 4 additions & 0 deletions src/monitor/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
MAX_ATTEMPTS: 5,
RETRY_DELAY: 1000, // ms
}
28 changes: 21 additions & 7 deletions src/monitor/push-metrics-for-folder.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

const path = require("path")

const { MAX_ATTEMPTS, RETRY_DELAY } = require('./constants')
const { getMetricsList } = require('./metrics-list.js')
const { formatMetricPayload } = require('./format-metric-payload.js')
const { pushToGateway } = require('./push-to-pushgateway.js')
Expand All @@ -36,14 +37,27 @@ const pushMetricsForFolder = ({ coverageArtifactsPath, folderName, jobName, push
const metricsPayload = getMetricsList(testStats, coverageStats)
.reduce((metricsAsText, metric) => metricsAsText + formatMetricPayload(metric), "")

// Sent metrics to Prometheus
pushToGateway(`${pushGatewayUri}/metrics/job/${jobName}/folder_name/${folderName}`, metricsPayload, (err, data) => {
if (err) {
throw new Error(err.message)
}
// Sent metrics to Prometheus - try MAX_ATTEMPTS times
const pushAttempt = (attemptsCount) => {
console.log(`Push metrics attempt #${attemptsCount}...`)
pushToGateway(`${pushGatewayUri}/metrics/job/${jobName}/folder_name/${folderName}`, metricsPayload, (err, data) => {
if (err) {
if (attemptsCount < MAX_ATTEMPTS) {
console.error("Failed to push metrics:", err.message, ` - will retry in ${RETRY_DELAY}ms...`)
// Wait and retry
setTimeout(() => pushAttempt(attemptsCount + 1), RETRY_DELAY)
}
else {
throw err
}
}
else {
console.log("Done -", data.body)
}
})
}

console.log("Done -", data.body)
})
pushAttempt(0)
}

// If script is used from command-line as standalone
Expand Down
23 changes: 23 additions & 0 deletions src/monitor/push-metrics-for-folder.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require("path")
const fs = require("fs")
const { MAX_ATTEMPTS } = require('./constants')
const { pushToGateway } = require('./push-to-pushgateway.js')
const { pushMetricsForFolder } = require("./push-metrics-for-folder")

Expand Down Expand Up @@ -52,4 +53,26 @@ describe("pushMetricsForFolder", () => {
expect(lastMockCallArgs[0]).toEqual(`http://pushgateway/metrics/job/test-jobname/folder_name/${FOLDERNAME}`)
expect(lastMockCallArgs[1]).toEqual("{\"foo\":\"bar\"}")
})

it(`should attempt ${MAX_ATTEMPTS} times to push metrics before failing`, () => {
jest.useFakeTimers()

let attemptsCount = 0
const pushToGatewayMock = jest.fn((url, payload, callback) => {
callback(new Error("Test error."))
attemptsCount++
jest.runAllTimers()
})
pushToGateway.mockImplementation(pushToGatewayMock)

expect(() => {
pushMetricsForFolder({
coverageArtifactsPath: ".",
folderName: FOLDERNAME,
jobName: "test-jobname",
pushGatewayUri: "http://pushgateway",
})
}).toThrow(/Test error/)
expect(attemptsCount).toEqual(MAX_ATTEMPTS)
})
})

0 comments on commit 65b480a

Please sign in to comment.