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
|
|
|
import { env } from 'consul-ui/env';
|
|
|
|
|
|
|
|
// meta is used by DataSource to configure polling. The interval controls how
|
|
|
|
// long between each poll to the metrics provider. TODO - make this configurable
|
|
|
|
// in the UI settings.
|
|
|
|
const meta = {
|
|
|
|
interval: env('CONSUL_METRICS_POLL_INTERVAL') || 10000,
|
|
|
|
};
|
|
|
|
|
2020-11-09 09:25:35 +00:00
|
|
|
export default class MetricsService extends RepositoryService {
|
|
|
|
@service('ui-config')
|
|
|
|
cfg;
|
|
|
|
|
|
|
|
@service('client/http')
|
|
|
|
client;
|
|
|
|
|
|
|
|
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-11-16 23:37:19 +00:00
|
|
|
return Promise.all(promises).then(function (results) {
|
2020-10-09 20:31:15 +00:00
|
|
|
return {
|
|
|
|
meta: meta,
|
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-11-16 23:37:19 +00:00
|
|
|
return this.provider.upstreamRecentSummaryStats(slug, dc, nspace, {}).then(function (result) {
|
2020-10-09 20:31:15 +00:00
|
|
|
result.meta = meta;
|
|
|
|
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-11-16 23:37:19 +00:00
|
|
|
return this.provider.downstreamRecentSummaryStats(slug, dc, nspace, {}).then(function (result) {
|
2020-10-09 20:31:15 +00:00
|
|
|
result.meta = meta;
|
|
|
|
return result;
|
|
|
|
});
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
}
|