mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-25 00:07:47 +01:00
chore(modernize): Modernize ClientMetricsController
This commit is contained in:
parent
8913c4fac5
commit
80a5d5c581
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
const { Router } = require('express');
|
const { Router } = require('express');
|
||||||
const FeatureController = require('./feature.js');
|
const FeatureController = require('./feature.js');
|
||||||
const metrics = require('./metrics.js');
|
const ClientMetricsController = require('./metrics.js');
|
||||||
const register = require('./register.js');
|
const register = require('./register.js');
|
||||||
const clientApi = require('./client-api.json');
|
const clientApi = require('./client-api.json');
|
||||||
|
|
||||||
@ -13,7 +13,7 @@ class ClientApi {
|
|||||||
|
|
||||||
router.get('/', this.index);
|
router.get('/', this.index);
|
||||||
router.use('/features', new FeatureController(config).router());
|
router.use('/features', new FeatureController(config).router());
|
||||||
router.use('/metrics', metrics.router(config));
|
router.use('/metrics', new ClientMetricsController(config).router());
|
||||||
router.use('/register', register.router(config));
|
router.use('/register', register.router(config));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,34 +6,44 @@ const logger = require('../../logger')('client-api/metrics.js');
|
|||||||
|
|
||||||
const { clientMetricsSchema } = require('./metrics-schema');
|
const { clientMetricsSchema } = require('./metrics-schema');
|
||||||
|
|
||||||
exports.router = config => {
|
class ClientMetricsController {
|
||||||
const { clientMetricsStore, clientInstanceStore } = config.stores;
|
constructor({ stores: { clientMetricsStore, clientInstanceStore } }) {
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
this.clientMetricsStore = clientMetricsStore;
|
||||||
|
this.clientInstanceStore = clientInstanceStore;
|
||||||
|
this._router = router;
|
||||||
|
|
||||||
router.post('/', (req, res) => {
|
router.post('/', (req, res) => this.registerMetrics(req, res));
|
||||||
|
}
|
||||||
|
|
||||||
|
async registerMetrics(req, res) {
|
||||||
const data = req.body;
|
const data = req.body;
|
||||||
const clientIp = req.ip;
|
const clientIp = req.ip;
|
||||||
|
|
||||||
joi.validate(data, clientMetricsSchema, (err, cleaned) => {
|
const { error, value } = joi.validate(data, clientMetricsSchema);
|
||||||
if (err) {
|
|
||||||
logger.warn('Invalid metrics posted', err);
|
|
||||||
return res.status(400).json(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
clientMetricsStore
|
if (error) {
|
||||||
.insert(cleaned)
|
logger.warn('Invalid metrics posted', error);
|
||||||
.then(() =>
|
return res.status(400).json(error);
|
||||||
clientInstanceStore.insert({
|
}
|
||||||
appName: cleaned.appName,
|
|
||||||
instanceId: cleaned.instanceId,
|
|
||||||
clientIp,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
.catch(err => logger.error('failed to store metrics', err));
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.clientMetricsStore.insert(value);
|
||||||
|
await this.clientInstanceStore.insert({
|
||||||
|
appName: value.appName,
|
||||||
|
instanceId: value.instanceId,
|
||||||
|
clientIp,
|
||||||
|
});
|
||||||
res.status(202).end();
|
res.status(202).end();
|
||||||
});
|
} catch (e) {
|
||||||
});
|
logger.error('failed to store metrics', e);
|
||||||
|
res.status(500).end();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return router;
|
router() {
|
||||||
};
|
return this._router;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = ClientMetricsController;
|
||||||
|
Loading…
Reference in New Issue
Block a user