Skip to content

Commit

Permalink
fix(metrics): fix endpoint metric name
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed Sep 22, 2019
1 parent 8a34634 commit 841117a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
35 changes: 35 additions & 0 deletions lib/endpoints_metrics.js
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());
};
3 changes: 3 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 12 additions & 1 deletion test/api/metrics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
Expand All @@ -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);
});
});

0 comments on commit 841117a

Please sign in to comment.