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';
|
2021-02-23 08:56:42 +00:00
|
|
|
import dataSource from 'consul-ui/decorators/data-source';
|
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-11-09 09:25:35 +00:00
|
|
|
export default class MetricsService extends RepositoryService {
|
2020-12-17 16:35:01 +00:00
|
|
|
@service('ui-config') config;
|
2020-12-15 15:34:54 +00:00
|
|
|
@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
|
|
|
|
2021-02-23 08:56:42 +00:00
|
|
|
getModelName() {
|
|
|
|
return 'metrics';
|
|
|
|
}
|
|
|
|
|
2020-11-09 09:25:35 +00:00
|
|
|
init() {
|
|
|
|
super.init(...arguments);
|
2020-12-17 16:35:01 +00:00
|
|
|
const config = this.config.get();
|
2020-10-09 20:31:15 +00:00
|
|
|
// Inject whether or not the proxy is enabled as an option into the opaque
|
|
|
|
// JSON options the user provided.
|
2020-12-17 16:35:01 +00:00
|
|
|
const opts = config.metrics_provider_options || {};
|
|
|
|
opts.metrics_proxy_enabled = config.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
|
2020-12-17 16:35:01 +00:00
|
|
|
const provider = config.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
|
|
|
|
2021-02-23 08:56:42 +00:00
|
|
|
@dataSource('/:ns/:dc/metrics/summary-for-service/:slug/:protocol')
|
|
|
|
findServiceSummary(params, 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 = [
|
2021-02-23 08:56:42 +00:00
|
|
|
this.provider.serviceRecentSummarySeries(
|
|
|
|
params.slug,
|
|
|
|
params.dc,
|
|
|
|
params.ns,
|
|
|
|
params.protocol,
|
|
|
|
{}
|
|
|
|
),
|
|
|
|
this.provider.serviceRecentSummaryStats(
|
|
|
|
params.slug,
|
|
|
|
params.dc,
|
|
|
|
params.ns,
|
|
|
|
params.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
|
|
|
|
2021-02-23 08:56:42 +00:00
|
|
|
@dataSource('/:ns/:dc/metrics/upstream-summary-for-service/:slug/:protocol')
|
|
|
|
findUpstreamSummary(params, configuration = {}) {
|
2020-10-20 15:41:16 +00:00
|
|
|
if (this.error) {
|
|
|
|
return Promise.reject(this.error);
|
|
|
|
}
|
2021-02-23 08:56:42 +00:00
|
|
|
return this.provider
|
|
|
|
.upstreamRecentSummaryStats(params.slug, params.dc, params.ns, {})
|
|
|
|
.then(result => {
|
|
|
|
result.meta = {
|
|
|
|
interval: this.env.var('CONSUL_METRICS_POLL_INTERVAL') || 10000,
|
|
|
|
};
|
|
|
|
return result;
|
|
|
|
});
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
2020-10-09 20:31:15 +00:00
|
|
|
|
2021-02-23 08:56:42 +00:00
|
|
|
@dataSource('/:ns/:dc/metrics/downstream-summary-for-service/:slug/:protocol')
|
|
|
|
findDownstreamSummary(params, configuration = {}) {
|
2020-10-20 15:41:16 +00:00
|
|
|
if (this.error) {
|
|
|
|
return Promise.reject(this.error);
|
|
|
|
}
|
2021-02-23 08:56:42 +00:00
|
|
|
return this.provider
|
|
|
|
.downstreamRecentSummaryStats(params.slug, params.dc, params.ns, {})
|
|
|
|
.then(result => {
|
|
|
|
result.meta = {
|
|
|
|
interval: this.env.var('CONSUL_METRICS_POLL_INTERVAL') || 10000,
|
|
|
|
};
|
|
|
|
return result;
|
|
|
|
});
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
}
|