mirror of
https://github.com/Unleash/unleash.git
synced 2024-12-28 00:06:53 +01:00
f5e9ca3bbe
This allows the users of 'unleash-server' to programatically change the logger implemnentation. #175
40 lines
1.1 KiB
JavaScript
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;
|
|
};
|