mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	add metrics client register
This commit is contained in:
		
							parent
							
								
									9b825ccaf0
								
							
						
					
					
						commit
						0e260265ee
					
				@ -1,16 +1,25 @@
 | 
				
			|||||||
'use strict';
 | 
					'use strict';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const POLL_INTERVAL = 10000;
 | 
					const POLL_INTERVAL = 10000;
 | 
				
			||||||
 | 
					const { EventEmitter } = require('events');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = class UnleashClientMetrics {
 | 
					module.exports = class UnleashClientMetricsService extends EventEmitter {
 | 
				
			||||||
    constructor (metricsDb) {
 | 
					    constructor (metricsDb) {
 | 
				
			||||||
 | 
					        super();
 | 
				
			||||||
        this.metricsDb = metricsDb;
 | 
					        this.metricsDb = metricsDb;
 | 
				
			||||||
        this.metrics = [];
 | 
					        this.metrics = [];
 | 
				
			||||||
        this.highestIdSeen = 0;
 | 
					        this.highestIdSeen = 0;
 | 
				
			||||||
        metricsDb.getMetricsLastWeek().then(metrics => {
 | 
					        this.fetch();
 | 
				
			||||||
            this.addMetrics(metrics);
 | 
					    }
 | 
				
			||||||
            this.startPoller();
 | 
					
 | 
				
			||||||
        });
 | 
					    fetch () {
 | 
				
			||||||
 | 
					        return this.metricsDb
 | 
				
			||||||
 | 
					            .getNewMetrics(this.highestIdSeen)
 | 
				
			||||||
 | 
					            .then(metrics => {
 | 
				
			||||||
 | 
					                this.startTimer();
 | 
				
			||||||
 | 
					                this.addMetrics(metrics);
 | 
				
			||||||
 | 
					                return metrics;
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    addMetrics (metrics) {
 | 
					    addMetrics (metrics) {
 | 
				
			||||||
@ -18,13 +27,11 @@ module.exports = class UnleashClientMetrics {
 | 
				
			|||||||
        if (this.metrics && this.metrics.length > 0) {
 | 
					        if (this.metrics && this.metrics.length > 0) {
 | 
				
			||||||
            this.highestIdSeen = this.metrics[this.metrics.length - 1].id;
 | 
					            this.highestIdSeen = this.metrics[this.metrics.length - 1].id;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        this.emit('metrics', metrics);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    startPoller () {
 | 
					    startTimer () {
 | 
				
			||||||
        setInterval(() => {
 | 
					        setInterval(() => this.fetch(), POLL_INTERVAL).unref();
 | 
				
			||||||
            this.metricsDb.getNewMetrics(this.highestIdSeen)
 | 
					 | 
				
			||||||
                .then(metrics => this.addMetrics(metrics));
 | 
					 | 
				
			||||||
        }, POLL_INTERVAL).unref();
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    getMetrics () {
 | 
					    getMetrics () {
 | 
				
			||||||
@ -32,6 +39,6 @@ module.exports = class UnleashClientMetrics {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    insert (metrics) {
 | 
					    insert (metrics) {
 | 
				
			||||||
        this.metricsDb.insert(metrics).then(() => console.log('new metrics inserted!'));
 | 
					        return this.metricsDb.insert(metrics);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
@ -14,8 +14,7 @@ module.exports = class UnleashClientMetrics {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    getState () {
 | 
					    getState () {
 | 
				
			||||||
        // TODO this payload will be WAY to big, need to flatten the store
 | 
					        // TODO need to flatten the store / possibly evict/flag stale clients
 | 
				
			||||||
        // and possibly evict/flag stale clients
 | 
					 | 
				
			||||||
        return {
 | 
					        return {
 | 
				
			||||||
            globalCount: this.globalCount,
 | 
					            globalCount: this.globalCount,
 | 
				
			||||||
            apps: this.apps,
 | 
					            apps: this.apps,
 | 
				
			||||||
@ -25,10 +24,13 @@ module.exports = class UnleashClientMetrics {
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    addPayload (data) {
 | 
					    registerClient (data) {
 | 
				
			||||||
        this.addApp(data.appName);
 | 
					        this.addClient(data.appName, data.instanceId, data.started);
 | 
				
			||||||
        this.addClient(data.appName, data.instanceId, data.clientInitTime);
 | 
					 | 
				
			||||||
        this.addStrategies(data.appName, data.strategies);
 | 
					        this.addStrategies(data.appName, data.strategies);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    addPayload (data) {
 | 
				
			||||||
 | 
					        this.addClient(data.appName, data.instanceId);
 | 
				
			||||||
        this.addBucket(data.appName, data.instanceId, data.bucket);
 | 
					        this.addBucket(data.appName, data.instanceId, data.bucket);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -70,7 +72,8 @@ module.exports = class UnleashClientMetrics {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    addClient (appName, instanceId, clientInitTime) {
 | 
					    addClient (appName, instanceId, started = new Date()) {
 | 
				
			||||||
 | 
					        this.addApp(appName);
 | 
				
			||||||
        if (instanceId) {
 | 
					        if (instanceId) {
 | 
				
			||||||
            if (this.clients[instanceId]) {
 | 
					            if (this.clients[instanceId]) {
 | 
				
			||||||
                this.clients[instanceId].ping = new Date();
 | 
					                this.clients[instanceId].ping = new Date();
 | 
				
			||||||
@ -78,7 +81,7 @@ module.exports = class UnleashClientMetrics {
 | 
				
			|||||||
                this.clients[instanceId] = {
 | 
					                this.clients[instanceId] = {
 | 
				
			||||||
                    appName,
 | 
					                    appName,
 | 
				
			||||||
                    count: 0,
 | 
					                    count: 0,
 | 
				
			||||||
                    clientInit: clientInitTime,
 | 
					                    started,
 | 
				
			||||||
                    init: new Date(),
 | 
					                    init: new Date(),
 | 
				
			||||||
                    ping: new Date(),
 | 
					                    ping: new Date(),
 | 
				
			||||||
                };
 | 
					                };
 | 
				
			||||||
 | 
				
			|||||||
@ -9,28 +9,38 @@ module.exports = function (app, config) {
 | 
				
			|||||||
    const metrics = new ClientMetrics();
 | 
					    const metrics = new ClientMetrics();
 | 
				
			||||||
    const service = new ClientMetricsService(metricsDb);
 | 
					    const service = new ClientMetricsService(metricsDb);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    app.get('/metrics', (req, res) => {
 | 
					    service.on('metrics', (entries) => {
 | 
				
			||||||
        res.json(service.getMetrics());
 | 
					        entries.forEach((m) => metrics.addPayload(m.metrics));
 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Your stuff:
 | 
					 | 
				
			||||||
        // res.json(metrics.getState());
 | 
					 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    app.post('/metrics', (req, res) => {
 | 
					    app.get('/service-metrics', (req, res) => {
 | 
				
			||||||
        // TODO: validate input and reply with http errorcode
 | 
					        res.json(service.getMetrics());
 | 
				
			||||||
        try {
 | 
					 | 
				
			||||||
            // not required with header: Content-Type: application/json
 | 
					 | 
				
			||||||
            // const data = JSON.parse(req.body);
 | 
					 | 
				
			||||||
            // metrics.addPayload(data);
 | 
					 | 
				
			||||||
            service.insert(req.body);
 | 
					 | 
				
			||||||
        } catch (e) {
 | 
					 | 
				
			||||||
            logger.error('Error recieving metrics', e);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        res.end();
 | 
					 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    app.get('/metrics', (req, res) => {
 | 
					    app.get('/metrics', (req, res) => {
 | 
				
			||||||
        res.json(metrics.getState());
 | 
					        res.json(metrics.getState());
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    app.post('/client/metrics', (req, res) => {
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            const data = typeof req.body === 'string' ? JSON.parse(req.body) : req.body;
 | 
				
			||||||
 | 
					            metrics.addPayload(data);
 | 
				
			||||||
 | 
					            service.insert(data);
 | 
				
			||||||
 | 
					        } catch (e) {
 | 
				
			||||||
 | 
					            logger.error('Error receiving metrics', e);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        res.end();
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    app.post('/client/register', (req, res) => {
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            const data = typeof req.body === 'string' ? JSON.parse(req.body) : req.body;
 | 
				
			||||||
 | 
					            metrics.registerClient(data);
 | 
				
			||||||
 | 
					        } catch (e) {
 | 
				
			||||||
 | 
					            logger.error('Error registering client', e);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        res.end();
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user