From 841117aaf4b7a56d3753b1d1fbead7a8e4ce887e Mon Sep 17 00:00:00 2001 From: Sergey Peshkov Date: Sun, 22 Sep 2019 19:47:22 +0300 Subject: [PATCH] fix(metrics): fix endpoint metric name --- lib/endpoints_metrics.js | 35 +++++++++++++++++++++++++++++++++++ lib/server.js | 3 +++ package.json | 1 + test/api/metrics.test.js | 13 ++++++++++++- 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 lib/endpoints_metrics.js diff --git a/lib/endpoints_metrics.js b/lib/endpoints_metrics.js new file mode 100644 index 00000000..e77f3030 --- /dev/null +++ b/lib/endpoints_metrics.js @@ -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()); +}; diff --git a/lib/server.js b/lib/server.js index c7d406cb..aa4b44d4 100644 --- a/lib/server.js +++ b/lib/server.js @@ -10,6 +10,7 @@ const middlewares = require('./middlewares'); const integrations = require('./integrations'); const categories = require('./categories'); const metrics = require('./metrics'); +const endpointsMetrics = require('./endpoints_metrics'); const db = require('./sequelize'); const server = express(); @@ -29,6 +30,7 @@ process.on('unhandledRejection', (err) => { GeneralRouter.get('/healthcheck', middlewares.healthcheck); GeneralRouter.get('/metrics', metrics.getMetrics); +GeneralRouter.get('/metrics/requests', endpointsMetrics.getEndpointMetrics); GeneralRouter.use(middlewares.authenticateUser); // integrations and codes @@ -49,6 +51,7 @@ GeneralRouter.get('/categories/:category_id', categories.findCategory, categorie GeneralRouter.put('/categories/:category_id', categories.findCategory, categories.updateCategory); GeneralRouter.delete('/categories/:category_id', categories.findCategory, categories.deleteCategory); +server.use(endpointsMetrics.addEndpointMetrics); server.use('/', GeneralRouter); server.use(middlewares.notFound); server.use(middlewares.errorHandler); diff --git a/package.json b/package.json index 7de332ca..1d821616 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "joi": "^14.3.1", "moment": "^2.24.0", "morgan": "^1.9.1", + "on-finished": "^2.3.0", "pg": "^7.12.1", "prom-client": "^11.5.3", "request": "^2.88.0", diff --git a/test/api/metrics.test.js b/test/api/metrics.test.js index e071d07b..03ee249d 100644 --- a/test/api/metrics.test.js +++ b/test/api/metrics.test.js @@ -14,7 +14,7 @@ describe('Metrics requests', () => { mock.cleanAll(); }); - test('should return data correctly', async () => { + test('should return data correctly on /metrics', async () => { await generator.createCategory({}); const integration = await generator.createIntegration({}); @@ -29,4 +29,15 @@ describe('Metrics requests', () => { expect(res.statusCode).toEqual(200); }); + + + test('should return data correctly on /metrics/requests', async () => { + const res = await request({ + uri: '/metrics/requests', + method: 'GET', + json: false + }); + + expect(res.statusCode).toEqual(200); + }); });