2019-09-14 02:01:03 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const statKeyTemplate = ctx => `windshaft.named-map-provider-cache.${ctx.metric}`;
|
|
|
|
|
2020-04-04 23:46:08 +08:00
|
|
|
module.exports = class NamedMapProviderCacheReporter {
|
2019-09-14 02:01:03 +08:00
|
|
|
constructor ({ namedMapProviderCache, intervalInMilliseconds } = {}) {
|
|
|
|
this.namedMapProviderCache = namedMapProviderCache;
|
2020-04-05 00:51:22 +08:00
|
|
|
this.intervalInMilliseconds = intervalInMilliseconds || 60000;
|
2019-09-14 02:01:03 +08:00
|
|
|
this.intervalId = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
start () {
|
|
|
|
const { providerCache: cache } = this.namedMapProviderCache;
|
|
|
|
const { statsClient: stats } = global;
|
|
|
|
|
|
|
|
this.intervalId = setInterval(() => {
|
|
|
|
const providers = cache.dump();
|
2020-03-21 01:50:22 +08:00
|
|
|
stats.gauge(statKeyTemplate({ metric: 'named-map.count' }), providers.length);
|
2019-09-14 02:01:03 +08:00
|
|
|
|
2019-09-16 19:08:10 +08:00
|
|
|
const namedMapInstantiations = providers.reduce((acc, { v: providers }) => {
|
2019-09-14 02:01:03 +08:00
|
|
|
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);
|
2019-09-14 02:01:03 +08:00
|
|
|
}, this.intervalInMilliseconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
stop () {
|
|
|
|
clearInterval(this.intervalId);
|
|
|
|
this.intervalId = null;
|
|
|
|
}
|
|
|
|
};
|