Windshaft-cartodb/lib/stats/reporter/renderer.js

85 lines
2.8 KiB
JavaScript
Raw Normal View History

'use strict';
// - 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) {
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 () {
var self = this;
2019-10-22 01:07:24 +08:00
this.renderersStatsIntervalId = setInterval(function () {
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;
}
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;
2019-10-22 01:07:24 +08:00
_rendererStats.cache.grid += stats.cache.grid;
_rendererStats.cache.png += stats.cache.png;
2019-10-22 01:07:24 +08:00
return _rendererStats;
},
{
pool: {
count: 0,
unused: 0,
waiting: 0
},
2019-10-22 01:07:24 +08:00
cache: {
png: 0,
grid: 0
}
2019-10-22 01:07:24 +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);
this.rendererCache.on('err', rendererCacheErrorListener);
this.rendererCache.on('gc', gcTimingListener);
};
2019-10-22 01:07:24 +08:00
function rendererCacheErrorListener () {
global.statsClient.increment('windshaft.rendercache.error');
}
2019-10-22 01:07:24 +08:00
function gcTimingListener (gcTime) {
global.statsClient.timing('windshaft.rendercache.gc', gcTime);
}
2019-10-22 01:07:24 +08:00
RendererStatsReporter.prototype.stop = function () {
this.rendererCache.removeListener('err', rendererCacheErrorListener);
this.rendererCache.removeListener('gc', gcTimingListener);
clearInterval(this.renderersStatsIntervalId);
this.renderersStatsIntervalId = null;
};