-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(metrics): fix endpoint metric name
- Loading branch information
1 parent
8a34634
commit 841117a
Showing
4 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
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,35 @@ | ||
const onFinished = require('on-finished'); | ||
const { | ||
Counter, | ||
Registry | ||
} = require('prom-client'); | ||
|
||
const endpointsRegistry = new Registry(); | ||
const responseCounter = new Counter({ | ||
name: 'discounts_requests_total', | ||
help: 'Amount of total HTTP requests', | ||
labelNames: ['status', 'endpoint', 'method'], | ||
registers: [endpointsRegistry] | ||
}); | ||
|
||
exports.addEndpointMetrics = async (req, res, next) => { | ||
const callbackOnFinished = () => { | ||
const labelsObject = { | ||
status: res.statusCode, | ||
endpoint: req.originalUrl, | ||
method: req.method | ||
}; | ||
|
||
responseCounter.inc(labelsObject); | ||
}; | ||
|
||
req.startTime = Date.now(); | ||
|
||
onFinished(res, callbackOnFinished); | ||
return next(); | ||
}; | ||
|
||
exports.getEndpointMetrics = async (req, res) => { | ||
res.set('Content-Type', endpointsRegistry.contentType); | ||
res.end(endpointsRegistry.metrics()); | ||
}; |
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