Windshaft-cartodb/lib/stats/reporter/named-map-provider-cache.js

34 lines
1.1 KiB
JavaScript
Raw Normal View History

'use strict';
const statKeyTemplate = ctx => `windshaft.named-map-provider-cache.${ctx.metric}`;
module.exports = class NamedMapProviderCacheReporter {
constructor ({ namedMapProviderCache, intervalInMilliseconds } = {}) {
this.namedMapProviderCache = namedMapProviderCache;
this.intervalInMilliseconds = intervalInMilliseconds;
this.intervalId = null;
}
start () {
const { providerCache: cache } = this.namedMapProviderCache;
const { statsClient: stats } = global;
this.intervalId = setInterval(() => {
const providers = cache.dump();
stats.gauge(statKeyTemplate({ metric: 'named-map.count' }), providers.length);
2019-09-16 19:08:10 +08:00
const namedMapInstantiations = providers.reduce((acc, { v: providers }) => {
acc += Object.keys(providers).length;
return acc;
}, 0);
2019-09-16 19:09:23 +08:00
stats.gauge(statKeyTemplate({ metric: 'named-map.instantiation.count' }), namedMapInstantiations);
}, this.intervalInMilliseconds);
}
stop () {
clearInterval(this.intervalId);
this.intervalId = null;
}
};