2020-10-09 20:31:15 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
2020-11-09 09:25:35 +00:00
|
|
|
import RepositoryService from 'consul-ui/services/repository';
|
2020-10-09 20:31:15 +00:00
|
|
|
|
2020-12-15 15:34:54 +00:00
|
|
|
// CONSUL_METRICS_POLL_INTERVAL controls how long between each poll to the
|
|
|
|
// metrics provider
|
2020-10-09 20:31:15 +00:00
|
|
|
|
2020-11-09 09:25:35 +00:00
|
|
|
export default class MetricsService extends RepositoryService {
|
2020-12-15 15:34:54 +00:00
|
|
|
@service('ui-config') cfg;
|
|
|
|
@service('env') env;
|
|
|
|
@service('client/http') client;
|
2020-11-09 09:25:35 +00:00
|
|
|
|
|
|
|
error = null;
|
2020-10-09 20:31:15 +00:00
|
|
|
|
2020-11-09 09:25:35 +00:00
|
|
|
init() {
|
|
|
|
super.init(...arguments);
|
2020-10-09 20:31:15 +00:00
|
|
|
const uiCfg = this.cfg.get();
|
|
|
|
// Inject whether or not the proxy is enabled as an option into the opaque
|
|
|
|
// JSON options the user provided.
|
|
|
|
const opts = uiCfg.metrics_provider_options || {};
|
|
|
|
opts.metrics_proxy_enabled = uiCfg.metrics_proxy_enabled;
|
2020-11-04 09:33:37 +00:00
|
|
|
// Inject a convenience function for dialing through the metrics proxy.
|
|
|
|
opts.fetch = (path, params) =>
|
|
|
|
this.client.fetchWithToken(`/v1/internal/ui/metrics-proxy${path}`, params);
|
2020-10-09 20:31:15 +00:00
|
|
|
// Inject the base app URL
|
|
|
|
const provider = uiCfg.metrics_provider || 'prometheus';
|
2020-10-20 15:41:16 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
this.provider = window.consul.getMetricsProvider(provider, opts);
|
2020-10-21 14:23:16 +00:00
|
|
|
} catch (e) {
|
2020-10-20 15:41:16 +00:00
|
|
|
this.error = new Error(`metrics provider not initialized: ${e}`);
|
|
|
|
// Show the user the error once for debugging their provider outside UI
|
|
|
|
// Dev.
|
2020-10-21 14:23:16 +00:00
|
|
|
console.error(this.error); // eslint-disable-line no-console
|
2020-10-20 15:41:16 +00:00
|
|
|
}
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
2020-10-09 20:31:15 +00:00
|
|
|
|
2020-11-09 09:25:35 +00:00
|
|
|
findServiceSummary(protocol, slug, dc, nspace, configuration = {}) {
|
2020-10-20 15:41:16 +00:00
|
|
|
if (this.error) {
|
|
|
|
return Promise.reject(this.error);
|
|
|
|
}
|
2020-10-09 20:31:15 +00:00
|
|
|
const promises = [
|
2020-11-16 23:37:19 +00:00
|
|
|
this.provider.serviceRecentSummarySeries(slug, dc, nspace, protocol, {}),
|
|
|
|
this.provider.serviceRecentSummaryStats(slug, dc, nspace, protocol, {}),
|
2020-10-09 20:31:15 +00:00
|
|
|
];
|
2020-12-15 15:34:54 +00:00
|
|
|
return Promise.all(promises).then(results => {
|
2020-10-09 20:31:15 +00:00
|
|
|
return {
|
2020-12-15 15:34:54 +00:00
|
|
|
meta: {
|
|
|
|
interval: this.env.var('CONSUL_METRICS_POLL_INTERVAL') || 10000,
|
|
|
|
},
|
2020-10-20 15:41:16 +00:00
|
|
|
series: results[0],
|
2020-10-09 20:31:15 +00:00
|
|
|
stats: results[1].stats,
|
|
|
|
};
|
|
|
|
});
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
2020-10-09 20:31:15 +00:00
|
|
|
|
2020-11-09 09:25:35 +00:00
|
|
|
findUpstreamSummary(slug, dc, nspace, configuration = {}) {
|
2020-10-20 15:41:16 +00:00
|
|
|
if (this.error) {
|
|
|
|
return Promise.reject(this.error);
|
|
|
|
}
|
2020-12-15 15:34:54 +00:00
|
|
|
return this.provider.upstreamRecentSummaryStats(slug, dc, nspace, {}).then(result => {
|
|
|
|
result.meta = {
|
|
|
|
interval: this.env.var('CONSUL_METRICS_POLL_INTERVAL') || 10000,
|
|
|
|
};
|
2020-10-09 20:31:15 +00:00
|
|
|
return result;
|
|
|
|
});
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
2020-10-09 20:31:15 +00:00
|
|
|
|
2020-11-09 09:25:35 +00:00
|
|
|
findDownstreamSummary(slug, dc, nspace, configuration = {}) {
|
2020-10-20 15:41:16 +00:00
|
|
|
if (this.error) {
|
|
|
|
return Promise.reject(this.error);
|
|
|
|
}
|
2020-12-15 15:34:54 +00:00
|
|
|
return this.provider.downstreamRecentSummaryStats(slug, dc, nspace, {}).then(result => {
|
|
|
|
result.meta = {
|
|
|
|
interval: this.env.var('CONSUL_METRICS_POLL_INTERVAL') || 10000,
|
|
|
|
};
|
2020-10-09 20:31:15 +00:00
|
|
|
return result;
|
|
|
|
});
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
}
|