1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/lib/routes/client-api/metrics.js
ivaosthu f5e9ca3bbe Implement support for logger provider.
This allows the users of 'unleash-server' to programatically
change the logger implemnentation. #175
2020-02-20 08:33:33 +01:00

40 lines
1.1 KiB
JavaScript

'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;
};