Skip to content

Commit

Permalink
feat: add metrics middleware and integrate with outbound send (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
kalinkrustev authored Nov 14, 2024
1 parent d834ef7 commit af581cd
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 96 deletions.
101 changes: 48 additions & 53 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@
"genexec": "pkg -t node8-win ."
},
"dependencies": {
"@elastic/elasticsearch": "8.15.1",
"@elastic/elasticsearch": "8.15.2",
"@hapi/basic": "7.0.2",
"@hapi/boom": "10.0.1",
"@hapi/good": "9.0.1",
"@hapi/hapi": "21.3.12",
"@hapi/inert": "7.1.0",
"@hapi/vision": "7.0.3",
"@mojaloop/central-services-logger": "11.5.1",
"@mojaloop/central-services-metrics": "12.1.0",
"@mojaloop/ml-schema-transformer-lib": "^v2.3.4",
"@mojaloop/ml-testing-toolkit-shared-lib": "14.0.1",
"@mojaloop/sdk-standard-components": "^19.4.1",
Expand All @@ -92,7 +93,7 @@
"json-rules-engine": "7.0.0",
"lodash": "4.17.21",
"mongo-uri-builder": "4.0.0",
"mongoose": "8.8.0",
"mongoose": "8.8.1",
"multer": "1.4.4",
"mustache": "4.2.0",
"mv": "2.1.1",
Expand All @@ -114,7 +115,7 @@
"socket.io": "4.8.1",
"socket.io-client": "4.8.1",
"ulidx": "2.4.1",
"uuid": "11.0.2",
"uuid": "11.0.3",
"uuid4": "2.0.3",
"ws": "8.18.0"
},
Expand All @@ -125,7 +126,7 @@
"jest": "29.7.0",
"jest-junit": "16.0.0",
"nodemon": "3.1.7",
"npm-check-updates": "17.1.10",
"npm-check-updates": "17.1.11",
"nyc": "17.1.0",
"parse-strings-in-object": "1.6.0",
"pre-commit": "1.2.2",
Expand Down
5 changes: 5 additions & 0 deletions spec_files/system_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
"PASSWORD": "ttk",
"DATABASE": "ttk"
},
"METRICS": {
"timeout": 5000,
"defaultLabels": {},
"defaultMetrics": true
},
"OAUTH": {
"AUTH_ENABLED": false,
"APP_OAUTH_CLIENT_KEY": "ttk",
Expand Down
4 changes: 2 additions & 2 deletions src/lib/api-routes/outbound.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ router.post('/template/:traceID', [
// TODO: Change the following value to the dfspId based on the login incase HOSTING_ENABLED
const dfspId = req.user ? req.user.dfspId : null
if (req.query.sync && req.query.sync === 'true') {
const result = await outbound.OutboundSend(inputJson, traceID, dfspId, true)
const result = await outbound.OutboundSend(inputJson, traceID, dfspId, true, req.metrics)
return res.status(200).json(result)
} else {
outbound.OutboundSend(inputJson, traceID, dfspId)
outbound.OutboundSend(inputJson, traceID, dfspId, false, req.metrics)
return res.status(200).json({ status: 'OK' })
}
} catch (err) {
Expand Down
3 changes: 3 additions & 0 deletions src/lib/api-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ const initServer = () => {
app.use(cookieParser())
OAuthHelper.handleMiddleware()

// Metrics
app.use(require('./metrics')(Config.getSystemConfig().METRICS))

const verifyUserMiddleware = verifyUser()
// For admin API
app.use('/api/rules', verifyUserMiddleware, require('./api-routes/rules'))
Expand Down
51 changes: 51 additions & 0 deletions src/lib/metrics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const Metrics = require('@mojaloop/central-services-metrics')

module.exports = function metricsMiddleware (config = {}) {
Metrics.getDefaultRegister().clear()
Metrics.setup({
timeout: 5000,
defaultLabels: {},
...config
})

const client = Metrics.getClient()
const assertSuccess = new client.Counter({
name: 'assert_success_total',
help: 'Successful assertions',
labelNames: ['job', 'test']
})
const assertFail = new client.Counter({
name: 'assert_fail_total',
help: 'Failed assertions',
labelNames: ['job', 'test']
})
const testSuccess = new client.Counter({
name: 'test_success_total',
help: 'Successful tests',
labelNames: ['job']
})
const testFail = new client.Counter({
name: 'test_fail_total',
help: 'Failed tests',
labelNames: ['job']
})

const metrics = {
assertSuccess: { add (value, labels) { assertSuccess.inc(labels, value) } },
assertFail: { add (value, labels) { assertFail.inc(labels, value) } },
testSuccess: { add (value, labels) { testSuccess.inc(labels, value) } },
testFail: { add (value, labels) { testFail.inc(labels, value) } }
}

return (req, res, next) => {
if (req.url === '/metrics') {
res.set('Content-Type', 'text/plain; version=0.0.4')
Metrics.getMetricsForPrometheus().then(metrics => {
res.send(metrics)
})
} else {
req.metrics = metrics
next()
}
}
}
Loading

0 comments on commit af581cd

Please sign in to comment.