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'; | ||||
| 
 | ||||
| const POLL_INTERVAL = 10000; | ||||
| const { EventEmitter } = require('events'); | ||||
| 
 | ||||
| module.exports = class UnleashClientMetrics { | ||||
| module.exports = class UnleashClientMetricsService extends EventEmitter { | ||||
|     constructor (metricsDb) { | ||||
|         super(); | ||||
|         this.metricsDb = metricsDb; | ||||
|         this.metrics = []; | ||||
|         this.highestIdSeen = 0; | ||||
|         metricsDb.getMetricsLastWeek().then(metrics => { | ||||
|             this.addMetrics(metrics); | ||||
|             this.startPoller(); | ||||
|         }); | ||||
|         this.fetch(); | ||||
|     } | ||||
| 
 | ||||
|     fetch () { | ||||
|         return this.metricsDb | ||||
|             .getNewMetrics(this.highestIdSeen) | ||||
|             .then(metrics => { | ||||
|                 this.startTimer(); | ||||
|                 this.addMetrics(metrics); | ||||
|                 return metrics; | ||||
|             }); | ||||
|     } | ||||
| 
 | ||||
|     addMetrics (metrics) { | ||||
| @ -18,13 +27,11 @@ module.exports = class UnleashClientMetrics { | ||||
|         if (this.metrics && this.metrics.length > 0) { | ||||
|             this.highestIdSeen = this.metrics[this.metrics.length - 1].id; | ||||
|         } | ||||
|         this.emit('metrics', metrics); | ||||
|     } | ||||
| 
 | ||||
|     startPoller () { | ||||
|         setInterval(() => { | ||||
|             this.metricsDb.getNewMetrics(this.highestIdSeen) | ||||
|                 .then(metrics => this.addMetrics(metrics)); | ||||
|         }, POLL_INTERVAL).unref(); | ||||
|     startTimer () { | ||||
|         setInterval(() => this.fetch(), POLL_INTERVAL).unref(); | ||||
|     } | ||||
| 
 | ||||
|     getMetrics () { | ||||
| @ -32,6 +39,6 @@ module.exports = class UnleashClientMetrics { | ||||
|     } | ||||
| 
 | ||||
|     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 () { | ||||
|         // TODO this payload will be WAY to big, need to flatten the store
 | ||||
|         // and possibly evict/flag stale clients
 | ||||
|         // TODO need to flatten the store / possibly evict/flag stale clients
 | ||||
|         return { | ||||
|             globalCount: this.globalCount, | ||||
|             apps: this.apps, | ||||
| @ -25,10 +24,13 @@ module.exports = class UnleashClientMetrics { | ||||
|         }; | ||||
|     } | ||||
| 
 | ||||
|     addPayload (data) { | ||||
|         this.addApp(data.appName); | ||||
|         this.addClient(data.appName, data.instanceId, data.clientInitTime); | ||||
|     registerClient (data) { | ||||
|         this.addClient(data.appName, data.instanceId, data.started); | ||||
|         this.addStrategies(data.appName, data.strategies); | ||||
|     } | ||||
| 
 | ||||
|     addPayload (data) { | ||||
|         this.addClient(data.appName, data.instanceId); | ||||
|         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 (this.clients[instanceId]) { | ||||
|                 this.clients[instanceId].ping = new Date(); | ||||
| @ -78,7 +81,7 @@ module.exports = class UnleashClientMetrics { | ||||
|                 this.clients[instanceId] = { | ||||
|                     appName, | ||||
|                     count: 0, | ||||
|                     clientInit: clientInitTime, | ||||
|                     started, | ||||
|                     init: new Date(), | ||||
|                     ping: new Date(), | ||||
|                 }; | ||||
|  | ||||
| @ -9,28 +9,38 @@ module.exports = function (app, config) { | ||||
|     const metrics = new ClientMetrics(); | ||||
|     const service = new ClientMetricsService(metricsDb); | ||||
| 
 | ||||
|     app.get('/metrics', (req, res) => { | ||||
|         res.json(service.getMetrics()); | ||||
| 
 | ||||
|         // Your stuff:
 | ||||
|         // res.json(metrics.getState());
 | ||||
|     service.on('metrics', (entries) => { | ||||
|         entries.forEach((m) => metrics.addPayload(m.metrics)); | ||||
|     }); | ||||
| 
 | ||||
|     app.post('/metrics', (req, res) => { | ||||
|         // TODO: validate input and reply with http errorcode
 | ||||
|         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('/service-metrics', (req, res) => { | ||||
|         res.json(service.getMetrics()); | ||||
|     }); | ||||
| 
 | ||||
|     app.get('/metrics', (req, res) => { | ||||
|         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