1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/lib/routes/client-api/metrics.js

40 lines
1.1 KiB
JavaScript
Raw Normal View History

'use strict';
const { Router } = require('express');
const joi = require('joi');
const logger = require('../../logger')('client-api/metrics.js');
const { clientMetricsSchema } = require('./metrics-schema');
exports.router = config => {
const { clientMetricsStore, clientInstanceStore } = config.stores;
const router = Router();
router.post('/', (req, res) => {
const data = req.body;
const clientIp = req.ip;
joi.validate(data, clientMetricsSchema, (err, cleaned) => {
if (err) {
logger.warn('Invalid metrics posted', err);
return res.status(400).json(err);
}
clientMetricsStore
.insert(cleaned)
.then(() =>
clientInstanceStore.insert({
appName: cleaned.appName,
instanceId: cleaned.instanceId,
clientIp,
})
)
.catch(err => logger.error('failed to store metrics', err));
res.status(202).end();
});
});
return router;
};