2020-11-11 16:59:15 +00:00
|
|
|
(function(global) {
|
|
|
|
// Current interface is these three methods.
|
|
|
|
const requiredMethods = [
|
|
|
|
'init',
|
|
|
|
'serviceRecentSummarySeries',
|
|
|
|
'serviceRecentSummaryStats',
|
|
|
|
'upstreamRecentSummaryStats',
|
|
|
|
'downstreamRecentSummaryStats',
|
|
|
|
];
|
|
|
|
|
|
|
|
// This is a bit gross but we want to support simple extensibility by
|
|
|
|
// including JS in the browser without forcing operators to setup a whole
|
|
|
|
// transpiling stack. So for now we use a window global as a thin registry for
|
|
|
|
// these providers.
|
|
|
|
class Consul {
|
|
|
|
constructor() {
|
|
|
|
this.registry = {};
|
|
|
|
this.providers = {};
|
|
|
|
}
|
2020-10-09 20:31:15 +00:00
|
|
|
|
2020-11-11 16:59:15 +00:00
|
|
|
registerMetricsProvider(name, provider) {
|
|
|
|
// Basic check that it matches the type we expect
|
|
|
|
for (var m of requiredMethods) {
|
|
|
|
if (typeof provider[m] !== 'function') {
|
|
|
|
throw new Error(`Can't register metrics provider '${name}': missing ${m} method.`);
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-11 16:59:15 +00:00
|
|
|
this.registry[name] = provider;
|
|
|
|
}
|
2020-10-09 20:31:15 +00:00
|
|
|
|
2020-11-11 16:59:15 +00:00
|
|
|
getMetricsProvider(name, options) {
|
|
|
|
if (!(name in this.registry)) {
|
|
|
|
throw new Error(`Metrics Provider '${name}' is not registered.`);
|
|
|
|
}
|
|
|
|
if (name in this.providers) {
|
2020-10-09 20:31:15 +00:00
|
|
|
return this.providers[name];
|
|
|
|
}
|
|
|
|
|
2020-11-11 16:59:15 +00:00
|
|
|
this.providers[name] = Object.create(this.registry[name]);
|
|
|
|
this.providers[name].init(options);
|
2020-10-09 20:31:15 +00:00
|
|
|
|
2020-11-11 16:59:15 +00:00
|
|
|
return this.providers[name];
|
|
|
|
}
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 16:59:15 +00:00
|
|
|
global.consul = new Consul();
|
|
|
|
})(window);
|