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
ivaosthu ccaab0c47b fix: LogProvider as option injected to unleash.
Instead of instructing users to do static calls
in to Unleash, she should instead be allwed to
specify the log provider as an option to Unleash.

This commit introduces the "getLogger" option,
a function responsible for creating a logger.
2020-02-20 08:34:24 +01:00

45 lines
1.3 KiB
JavaScript

'use strict';
const joi = require('joi');
const Controller = require('../controller');
const { clientMetricsSchema } = require('./metrics-schema');
class ClientMetricsController extends Controller {
constructor({ clientMetricsStore, clientInstanceStore }, getLogger) {
super();
this.logger = getLogger('/api/client/metrics');
this.clientMetricsStore = clientMetricsStore;
this.clientInstanceStore = clientInstanceStore;
this.post('/', this.registerMetrics);
}
async registerMetrics(req, res) {
const data = req.body;
const clientIp = req.ip;
const { error, value } = joi.validate(data, clientMetricsSchema);
if (error) {
this.logger.warn('Invalid metrics posted', error);
return res.status(400).json(error);
}
try {
await this.clientMetricsStore.insert(value);
await this.clientInstanceStore.insert({
appName: value.appName,
instanceId: value.instanceId,
clientIp,
});
res.status(202).end();
} catch (e) {
this.logger.error('failed to store metrics', e);
res.status(500).end();
}
}
}
module.exports = ClientMetricsController;