Windshaft-cartodb/lib/stats/reporter/named-map-provider.js
2020-03-20 18:50:22 +01:00

34 lines
1.1 KiB
JavaScript

'use strict';
const statKeyTemplate = ctx => `windshaft.named-map-provider-cache.${ctx.metric}`;
module.exports = class NamedMapProviderReporter {
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);
const namedMapInstantiations = providers.reduce((acc, { v: providers }) => {
acc += Object.keys(providers).length;
return acc;
}, 0);
stats.gauge(statKeyTemplate({ metric: 'named-map.instantiation.count' }), namedMapInstantiations);
}, this.intervalInMilliseconds);
}
stop () {
clearInterval(this.intervalId);
this.intervalId = null;
}
};