-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add metrics middleware and integrate with outbound send (#279)
- Loading branch information
1 parent
d834ef7
commit af581cd
Showing
8 changed files
with
268 additions
and
96 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |
Oops, something went wrong.