2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2015-09-15 00:47:01 +08:00
|
|
|
// - Reports stats about:
|
|
|
|
// * Total number of renderers
|
|
|
|
// * For mapnik renderers:
|
|
|
|
// - the mapnik-pool status: count, unused and waiting
|
|
|
|
// - the internally cached objects: png and grid
|
|
|
|
|
|
|
|
var _ = require('underscore');
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function RendererStatsReporter (rendererCache, statsInterval) {
|
2015-09-15 00:47:01 +08:00
|
|
|
this.rendererCache = rendererCache;
|
|
|
|
this.statsInterval = statsInterval || 6e4;
|
|
|
|
this.renderersStatsIntervalId = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = RendererStatsReporter;
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
RendererStatsReporter.prototype.start = function () {
|
2015-09-15 00:47:01 +08:00
|
|
|
var self = this;
|
2019-10-22 01:07:24 +08:00
|
|
|
this.renderersStatsIntervalId = setInterval(function () {
|
2015-09-15 00:47:01 +08:00
|
|
|
var rendererCacheEntries = self.rendererCache.renderers;
|
|
|
|
|
|
|
|
if (!rendererCacheEntries) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
global.statsClient.gauge('windshaft.rendercache.count', _.keys(rendererCacheEntries).length);
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
var renderersStats = _.reduce(rendererCacheEntries, function (_rendererStats, cacheEntry) {
|
|
|
|
var stats = cacheEntry.renderer && cacheEntry.renderer.getStats && cacheEntry.renderer.getStats();
|
|
|
|
if (!stats) {
|
|
|
|
return _rendererStats;
|
|
|
|
}
|
2015-09-15 00:47:01 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
_rendererStats.pool.count += stats.pool.count;
|
|
|
|
_rendererStats.pool.unused += stats.pool.unused;
|
|
|
|
_rendererStats.pool.waiting += stats.pool.waiting;
|
2015-09-15 00:47:01 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
_rendererStats.cache.grid += stats.cache.grid;
|
|
|
|
_rendererStats.cache.png += stats.cache.png;
|
2015-09-15 00:47:01 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
return _rendererStats;
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pool: {
|
|
|
|
count: 0,
|
|
|
|
unused: 0,
|
|
|
|
waiting: 0
|
2015-09-15 00:47:01 +08:00
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
cache: {
|
|
|
|
png: 0,
|
|
|
|
grid: 0
|
2015-09-15 00:47:01 +08:00
|
|
|
}
|
2019-10-22 01:07:24 +08:00
|
|
|
}
|
2015-09-15 00:47:01 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
global.statsClient.gauge('windshaft.mapnik-cache.png', renderersStats.cache.png);
|
|
|
|
global.statsClient.gauge('windshaft.mapnik-cache.grid', renderersStats.cache.grid);
|
|
|
|
|
|
|
|
global.statsClient.gauge('windshaft.mapnik-pool.count', renderersStats.pool.count);
|
|
|
|
global.statsClient.gauge('windshaft.mapnik-pool.unused', renderersStats.pool.unused);
|
|
|
|
global.statsClient.gauge('windshaft.mapnik-pool.waiting', renderersStats.pool.waiting);
|
|
|
|
}, this.statsInterval);
|
|
|
|
|
2015-09-15 01:24:24 +08:00
|
|
|
this.rendererCache.on('err', rendererCacheErrorListener);
|
|
|
|
this.rendererCache.on('gc', gcTimingListener);
|
2015-09-15 00:47:01 +08:00
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function rendererCacheErrorListener () {
|
2015-09-15 01:24:24 +08:00
|
|
|
global.statsClient.increment('windshaft.rendercache.error');
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function gcTimingListener (gcTime) {
|
2015-09-15 01:24:24 +08:00
|
|
|
global.statsClient.timing('windshaft.rendercache.gc', gcTime);
|
|
|
|
}
|
2015-09-15 00:47:01 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
RendererStatsReporter.prototype.stop = function () {
|
2015-09-15 01:24:24 +08:00
|
|
|
this.rendererCache.removeListener('err', rendererCacheErrorListener);
|
|
|
|
this.rendererCache.removeListener('gc', gcTimingListener);
|
|
|
|
|
2015-09-15 00:47:01 +08:00
|
|
|
clearInterval(this.renderersStatsIntervalId);
|
|
|
|
this.renderersStatsIntervalId = null;
|
|
|
|
};
|