2023-03-14 13:18:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-14 13:18:55 +00:00
|
|
|
*/
|
|
|
|
|
2020-10-09 20:31:15 +00:00
|
|
|
/*eslint no-console: "off"*/
|
2020-11-16 23:37:19 +00:00
|
|
|
(function () {
|
2020-11-11 16:59:15 +00:00
|
|
|
var emptySeries = { unitSuffix: '', labels: {}, data: [] };
|
2020-10-20 15:41:16 +00:00
|
|
|
|
2020-10-09 20:31:15 +00:00
|
|
|
var prometheusProvider = {
|
|
|
|
options: {},
|
|
|
|
|
|
|
|
/**
|
2020-10-20 15:41:16 +00:00
|
|
|
* init is called when the provider is first loaded.
|
2020-10-09 20:31:15 +00:00
|
|
|
*
|
|
|
|
* options.providerOptions contains any operator configured parameters
|
|
|
|
* specified in the Consul agent config that is serving the UI.
|
|
|
|
*
|
2020-11-04 09:33:37 +00:00
|
|
|
* Consul will provide:
|
|
|
|
*
|
|
|
|
* 1. A boolean options.metrics_proxy_enabled to indicate whether the agent
|
|
|
|
* has a metrics proxy configured.
|
|
|
|
* 2. A fetch-like options.fetch which is a thin fetch wrapper that prefixes
|
|
|
|
* any url with the url of Consul's proxy endpoint and adds your current
|
|
|
|
* Consul ACL token to the request headers. Otherwise it functions like the
|
|
|
|
* browsers native fetch
|
2020-10-20 15:41:16 +00:00
|
|
|
*
|
|
|
|
* The provider should throw an Exception if the options are not valid for
|
|
|
|
* example because it requires a metrics proxy and one is not configured.
|
2020-10-09 20:31:15 +00:00
|
|
|
*/
|
2020-11-16 23:37:19 +00:00
|
|
|
init: function (options) {
|
2020-10-09 20:31:15 +00:00
|
|
|
this.options = options;
|
2020-10-20 15:41:16 +00:00
|
|
|
if (!this.options.metrics_proxy_enabled) {
|
2020-11-11 16:59:15 +00:00
|
|
|
throw new Error(
|
|
|
|
'prometheus metrics provider currently requires the ui_config.metrics_proxy to be configured in the Consul agent.'
|
|
|
|
);
|
2020-10-20 15:41:16 +00:00
|
|
|
}
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-04 09:33:37 +00:00
|
|
|
// simple httpGet function that also encodes query parameters
|
|
|
|
// before passing the constructed url through to native fetch
|
|
|
|
// any errors should throw an error with a statusCode property
|
2020-11-16 23:37:19 +00:00
|
|
|
httpGet: function (url, queryParams, headers) {
|
2020-11-04 09:33:37 +00:00
|
|
|
if (queryParams) {
|
|
|
|
var separator = url.indexOf('?') !== -1 ? '&' : '?';
|
2020-11-11 16:59:15 +00:00
|
|
|
var qs = Object.keys(queryParams)
|
2020-11-16 23:37:19 +00:00
|
|
|
.map(function (key) {
|
2020-11-11 16:59:15 +00:00
|
|
|
return encodeURIComponent(key) + '=' + encodeURIComponent(queryParams[key]);
|
|
|
|
})
|
|
|
|
.join('&');
|
2020-11-04 09:33:37 +00:00
|
|
|
url = url + separator + qs;
|
|
|
|
}
|
|
|
|
// fetch the url along with any headers
|
2020-11-16 23:37:19 +00:00
|
|
|
return this.options.fetch(url, { headers: headers || {} }).then(function (response) {
|
2020-11-11 16:59:15 +00:00
|
|
|
if (response.ok) {
|
|
|
|
return response.json();
|
|
|
|
} else {
|
|
|
|
// throw a statusCode error if any errors are received
|
|
|
|
var e = new Error('HTTP Error: ' + response.statusText);
|
|
|
|
e.statusCode = response.status;
|
|
|
|
throw e;
|
2020-11-04 09:33:37 +00:00
|
|
|
}
|
2020-11-11 16:59:15 +00:00
|
|
|
});
|
2020-11-04 09:33:37 +00:00
|
|
|
},
|
|
|
|
|
2020-10-09 20:31:15 +00:00
|
|
|
/**
|
|
|
|
* serviceRecentSummarySeries should return time series for a recent time
|
2020-10-26 19:48:23 +00:00
|
|
|
* period summarizing the usage of the named service in the indicated
|
|
|
|
* datacenter. In Consul Enterprise a non-empty namespace is also provided.
|
2020-10-09 20:31:15 +00:00
|
|
|
*
|
2020-10-20 15:41:16 +00:00
|
|
|
* If these metrics aren't available then an empty series array may be
|
|
|
|
* returned.
|
2020-10-09 20:31:15 +00:00
|
|
|
*
|
|
|
|
* The period may (later) be specified in options.startTime and
|
|
|
|
* options.endTime.
|
|
|
|
*
|
|
|
|
* The service's protocol must be given as one of Consul's supported
|
|
|
|
* protocols e.g. "tcp", "http", "http2", "grpc". If it is empty or the
|
2020-10-20 15:41:16 +00:00
|
|
|
* provider doesn't recognize the protocol, it should treat it as "tcp" and
|
|
|
|
* provide basic connection stats.
|
2020-10-09 20:31:15 +00:00
|
|
|
*
|
|
|
|
* The expected return value is a promise which resolves to an object that
|
|
|
|
* should look like the following:
|
|
|
|
*
|
|
|
|
* {
|
2020-10-20 15:41:16 +00:00
|
|
|
* // The unitSuffix is shown after the value in tooltips. Values will be
|
|
|
|
* // rounded and shortened. Larger values will already have a suffix
|
|
|
|
* // like "10k". The suffix provided here is concatenated directly
|
|
|
|
* // allowing for suffixes like "mbps/kbps" by using a suffix of "bps".
|
|
|
|
* // If the unit doesn't make sense in this format, include a
|
|
|
|
* // leading space for example " rps" would show as "1.2k rps".
|
|
|
|
* unitSuffix: " rps",
|
|
|
|
*
|
|
|
|
* // The set of labels to graph. The key should exactly correspond to a
|
|
|
|
* // property of every data point in the array below except for the
|
|
|
|
* // special case "Total" which is used to show the sum of all the
|
|
|
|
* // stacked graph values. The key is displayed in the tooltop so it
|
|
|
|
* // should be human-friendly but as concise as possible. The value is a
|
|
|
|
* // longer description that is displayed in the graph's key on request
|
|
|
|
* // to explain exactly what the metrics mean.
|
|
|
|
* labels: {
|
|
|
|
* "Total": "Total inbound requests per second.",
|
2020-10-26 19:48:23 +00:00
|
|
|
* "Successes": "Successful responses (with an HTTP response code ...",
|
|
|
|
* "Errors": "Error responses (with an HTTP response code in the ...",
|
2020-10-20 15:41:16 +00:00
|
|
|
* },
|
|
|
|
*
|
|
|
|
* data: [
|
2020-10-09 20:31:15 +00:00
|
|
|
* {
|
2020-10-20 15:41:16 +00:00
|
|
|
* time: 1600944516286, // milliseconds since Unix epoch
|
|
|
|
* "Successes": 1234.5,
|
|
|
|
* "Errors": 2.3,
|
2020-10-09 20:31:15 +00:00
|
|
|
* },
|
|
|
|
* ...
|
|
|
|
* ]
|
|
|
|
* }
|
|
|
|
*
|
2020-10-20 15:41:16 +00:00
|
|
|
* Every data point object should have a value for every series label
|
|
|
|
* (except for "Total") otherwise it will be assumed to be "0".
|
2020-10-09 20:31:15 +00:00
|
|
|
*/
|
2020-11-16 23:37:19 +00:00
|
|
|
serviceRecentSummarySeries: function (service, dc, nspace, protocol, options) {
|
2020-10-09 20:31:15 +00:00
|
|
|
// Fetch time-series
|
2020-11-11 16:59:15 +00:00
|
|
|
var series = [];
|
|
|
|
var labels = [];
|
2020-10-09 20:31:15 +00:00
|
|
|
|
|
|
|
// Set the start and end range here so that all queries end up with
|
|
|
|
// identical time axes. Later we might accept these as options.
|
2020-11-11 16:59:15 +00:00
|
|
|
var now = new Date().getTime() / 1000;
|
|
|
|
options.start = now - 15 * 60;
|
2020-10-09 20:31:15 +00:00
|
|
|
options.end = now;
|
|
|
|
|
|
|
|
if (this.hasL7Metrics(protocol)) {
|
2020-11-16 23:37:19 +00:00
|
|
|
return this.fetchRequestRateSeries(service, dc, nspace, options);
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 15:41:16 +00:00
|
|
|
// Fallback to just L4 metrics.
|
2020-11-16 23:37:19 +00:00
|
|
|
return this.fetchDataRateSeries(service, dc, nspace, options);
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* serviceRecentSummaryStats should return four summary statistics for a
|
2020-10-26 19:48:23 +00:00
|
|
|
* recent time period for the named service in the indicated datacenter. In
|
|
|
|
* Consul Enterprise a non-empty namespace is also provided.
|
2020-10-09 20:31:15 +00:00
|
|
|
*
|
|
|
|
* If these metrics aren't available then an empty array may be returned.
|
|
|
|
*
|
|
|
|
* The period may (later) be specified in options.startTime and
|
|
|
|
* options.endTime.
|
|
|
|
*
|
|
|
|
* The service's protocol must be given as one of Consul's supported
|
|
|
|
* protocols e.g. "tcp", "http", "http2", "grpc". If it is empty or the
|
|
|
|
* provider doesn't recognize it it should treat it as "tcp" and provide
|
|
|
|
* just basic connection stats.
|
|
|
|
*
|
|
|
|
* The expected return value is a promise which resolves to an object that
|
|
|
|
* should look like the following:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* stats: [ // We expect four of these for now.
|
|
|
|
* {
|
|
|
|
* // label should be 3 chars or fewer as an abbreviation
|
|
|
|
* label: "SR",
|
2020-10-26 19:48:23 +00:00
|
|
|
*
|
2020-10-09 20:31:15 +00:00
|
|
|
* // desc describes the stat in a tooltip
|
|
|
|
* desc: "Success Rate - the percentage of all requests that were not 5xx status",
|
2020-10-26 19:48:23 +00:00
|
|
|
*
|
2020-10-09 20:31:15 +00:00
|
|
|
* // value is a string allowing the provider to format it and add
|
|
|
|
* // units as appropriate. It should be as compact as possible.
|
|
|
|
* value: "98%",
|
|
|
|
* }
|
|
|
|
* ]
|
|
|
|
* }
|
|
|
|
*/
|
2020-11-16 23:37:19 +00:00
|
|
|
serviceRecentSummaryStats: function (service, dc, nspace, protocol, options) {
|
2020-10-09 20:31:15 +00:00
|
|
|
// Fetch stats
|
|
|
|
var stats = [];
|
|
|
|
if (this.hasL7Metrics(protocol)) {
|
2020-11-16 23:37:19 +00:00
|
|
|
stats.push(this.fetchRPS(service, dc, nspace, 'service', options));
|
|
|
|
stats.push(this.fetchER(service, dc, nspace, 'service', options));
|
|
|
|
stats.push(this.fetchPercentile(50, service, dc, nspace, 'service', options));
|
|
|
|
stats.push(this.fetchPercentile(99, service, dc, nspace, 'service', options));
|
2020-10-09 20:31:15 +00:00
|
|
|
} else {
|
|
|
|
// Fallback to just L4 metrics.
|
2020-11-16 23:37:19 +00:00
|
|
|
stats.push(this.fetchConnRate(service, dc, nspace, 'service', options));
|
|
|
|
stats.push(this.fetchServiceRx(service, dc, nspace, 'service', options));
|
|
|
|
stats.push(this.fetchServiceTx(service, dc, nspace, 'service', options));
|
|
|
|
stats.push(this.fetchServiceNoRoute(service, dc, nspace, 'service', options));
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
2020-11-11 16:59:15 +00:00
|
|
|
return this.fetchStats(stats);
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* upstreamRecentSummaryStats should return four summary statistics for each
|
2020-10-26 19:48:23 +00:00
|
|
|
* upstream service over a recent time period, relative to the named service
|
|
|
|
* in the indicated datacenter. In Consul Enterprise a non-empty namespace
|
|
|
|
* is also provided.
|
|
|
|
*
|
|
|
|
* Note that the upstreams themselves might be in different datacenters but
|
|
|
|
* we only pass the target service DC since typically these metrics should
|
|
|
|
* be from the outbound listener of the target service in this DC even if
|
|
|
|
* they eventually end up in another DC.
|
2020-10-09 20:31:15 +00:00
|
|
|
*
|
|
|
|
* If these metrics aren't available then an empty array may be returned.
|
|
|
|
*
|
|
|
|
* The period may (later) be specified in options.startTime and
|
|
|
|
* options.endTime.
|
|
|
|
*
|
|
|
|
* The expected return value format is shown below:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* stats: {
|
|
|
|
* // Each upstream will appear as an entry keyed by the upstream
|
|
|
|
* // service name. The value is an array of stats with the same
|
|
|
|
* // format as serviceRecentSummaryStats response.stats. Note that
|
|
|
|
* // different upstreams might show different stats depending on
|
|
|
|
* // their protocol.
|
|
|
|
* "upstream_name": [
|
|
|
|
* {label: "SR", desc: "...", value: "99%"},
|
|
|
|
* ...
|
|
|
|
* ],
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*/
|
2020-11-16 23:37:19 +00:00
|
|
|
upstreamRecentSummaryStats: function (service, dc, nspace, options) {
|
|
|
|
return this.fetchRecentSummaryStats(service, dc, nspace, 'upstream', options);
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2020-10-20 15:41:16 +00:00
|
|
|
* downstreamRecentSummaryStats should return four summary statistics for
|
2020-10-26 19:48:23 +00:00
|
|
|
* each downstream service over a recent time period, relative to the named
|
|
|
|
* service in the indicated datacenter. In Consul Enterprise a non-empty
|
|
|
|
* namespace is also provided.
|
|
|
|
*
|
|
|
|
* Note that the service may have downstreams in different datacenters. For
|
|
|
|
* some metrics systems which are per-datacenter this makes it hard to query
|
|
|
|
* for all downstream metrics from one source. For now the UI will only show
|
|
|
|
* downstreams in the same datacenter as the target service. In the future
|
|
|
|
* this method may be called multiple times, once for each DC that contains
|
|
|
|
* downstream services to gather metrics from each. In that case a separate
|
|
|
|
* option for target datacenter will be used since the target service's DC
|
|
|
|
* is still needed to correctly identify the outbound clusters that will
|
|
|
|
* route to it from the remote DC.
|
2020-10-09 20:31:15 +00:00
|
|
|
*
|
|
|
|
* If these metrics aren't available then an empty array may be returned.
|
|
|
|
*
|
|
|
|
* The period may (later) be specified in options.startTime and
|
|
|
|
* options.endTime.
|
|
|
|
*
|
|
|
|
* The expected return value format is shown below:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* stats: {
|
2020-11-16 23:37:19 +00:00
|
|
|
* // Each downstream will appear as an entry keyed by "service.namespace.dc".
|
|
|
|
* // The value is an array of stats with the same
|
2020-10-20 15:41:16 +00:00
|
|
|
* // format as serviceRecentSummaryStats response.stats. Different
|
|
|
|
* // downstreams may display different stats if required although the
|
|
|
|
* // protocol should be the same for all as it is the target
|
|
|
|
* // service's protocol that matters here.
|
2020-11-16 23:37:19 +00:00
|
|
|
* "web.default.dc1": [
|
2020-10-09 20:31:15 +00:00
|
|
|
* {label: "SR", desc: "...", value: "99%"},
|
|
|
|
* ...
|
|
|
|
* ],
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*/
|
2020-11-16 23:37:19 +00:00
|
|
|
downstreamRecentSummaryStats: function (service, dc, nspace, options) {
|
|
|
|
return this.fetchRecentSummaryStats(service, dc, nspace, 'downstream', options);
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
fetchRecentSummaryStats: function (service, dc, nspace, type, options) {
|
2020-10-09 20:31:15 +00:00
|
|
|
// Fetch stats
|
|
|
|
var stats = [];
|
|
|
|
|
|
|
|
// We don't know which upstreams are HTTP/TCP so just fetch all of them.
|
|
|
|
|
|
|
|
// HTTP
|
2020-11-16 23:37:19 +00:00
|
|
|
stats.push(this.fetchRPS(service, dc, nspace, type, options));
|
|
|
|
stats.push(this.fetchER(service, dc, nspace, type, options));
|
|
|
|
stats.push(this.fetchPercentile(50, service, dc, nspace, type, options));
|
|
|
|
stats.push(this.fetchPercentile(99, service, dc, nspace, type, options));
|
2020-10-09 20:31:15 +00:00
|
|
|
|
|
|
|
// L4
|
2020-11-16 23:37:19 +00:00
|
|
|
stats.push(this.fetchConnRate(service, dc, nspace, type, options));
|
|
|
|
stats.push(this.fetchServiceRx(service, dc, nspace, type, options));
|
|
|
|
stats.push(this.fetchServiceTx(service, dc, nspace, type, options));
|
|
|
|
stats.push(this.fetchServiceNoRoute(service, dc, nspace, type, options));
|
2020-10-09 20:31:15 +00:00
|
|
|
|
2020-11-11 16:59:15 +00:00
|
|
|
return this.fetchStatsGrouped(stats);
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
hasL7Metrics: function (protocol) {
|
2020-11-11 16:59:15 +00:00
|
|
|
return protocol === 'http' || protocol === 'http2' || protocol === 'grpc';
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
fetchStats: function (statsPromises) {
|
|
|
|
var all = Promise.all(statsPromises).then(function (results) {
|
2020-10-09 20:31:15 +00:00
|
|
|
var data = {
|
2020-11-11 16:59:15 +00:00
|
|
|
stats: [],
|
|
|
|
};
|
2020-10-09 20:31:15 +00:00
|
|
|
// Add all non-empty stats
|
|
|
|
for (var i = 0; i < statsPromises.length; i++) {
|
|
|
|
if (results[i].value) {
|
2020-10-27 14:51:15 +00:00
|
|
|
data.stats.push(results[i]);
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-11 16:59:15 +00:00
|
|
|
return data;
|
|
|
|
});
|
2020-10-09 20:31:15 +00:00
|
|
|
|
|
|
|
// Fetch the metrics async, and return a promise to the result.
|
2020-11-11 16:59:15 +00:00
|
|
|
return all;
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
fetchStatsGrouped: function (statsPromises) {
|
|
|
|
var all = Promise.all(statsPromises).then(function (results) {
|
2020-10-09 20:31:15 +00:00
|
|
|
var data = {
|
2020-11-11 16:59:15 +00:00
|
|
|
stats: {},
|
|
|
|
};
|
2020-10-09 20:31:15 +00:00
|
|
|
// Add all non-empty stats
|
|
|
|
for (var i = 0; i < statsPromises.length; i++) {
|
2020-10-27 14:51:15 +00:00
|
|
|
if (results[i]) {
|
|
|
|
for (var group in results[i]) {
|
|
|
|
if (!results[i].hasOwnProperty(group)) continue;
|
2020-10-09 20:31:15 +00:00
|
|
|
if (!data.stats[group]) {
|
2020-11-11 16:59:15 +00:00
|
|
|
data.stats[group] = [];
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
2020-11-11 16:59:15 +00:00
|
|
|
data.stats[group].push(results[i][group]);
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-11 16:59:15 +00:00
|
|
|
return data;
|
|
|
|
});
|
2020-10-09 20:31:15 +00:00
|
|
|
|
|
|
|
// Fetch the metrics async, and return a promise to the result.
|
2020-11-11 16:59:15 +00:00
|
|
|
return all;
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
reformatSeries: function (unitSuffix, labelMap) {
|
|
|
|
return function (response) {
|
2020-10-20 15:41:16 +00:00
|
|
|
// Handle empty result sets gracefully.
|
2020-11-11 16:59:15 +00:00
|
|
|
if (
|
|
|
|
!response.data ||
|
|
|
|
!response.data.result ||
|
|
|
|
response.data.result.length == 0 ||
|
|
|
|
!response.data.result[0].values ||
|
|
|
|
response.data.result[0].values.length == 0
|
|
|
|
) {
|
2020-10-20 15:41:16 +00:00
|
|
|
return emptySeries;
|
|
|
|
}
|
|
|
|
// Reformat the prometheus data to be the format we want with stacked
|
|
|
|
// values as object properties.
|
2020-10-09 20:31:15 +00:00
|
|
|
|
2020-10-20 15:41:16 +00:00
|
|
|
// Populate time values first based on first result since Prometheus will
|
|
|
|
// always return all the same points for all series in the query.
|
2020-11-16 23:37:19 +00:00
|
|
|
let series = response.data.result[0].values.map(function (d, i) {
|
2020-10-20 15:41:16 +00:00
|
|
|
return {
|
|
|
|
time: Math.round(d[0] * 1000),
|
|
|
|
};
|
|
|
|
});
|
2020-10-09 20:31:15 +00:00
|
|
|
|
2020-10-20 15:41:16 +00:00
|
|
|
// Then for each series returned populate the labels and values in the
|
|
|
|
// points.
|
2020-11-16 23:37:19 +00:00
|
|
|
response.data.result.map(function (d) {
|
|
|
|
d.values.map(function (p, i) {
|
2020-10-20 15:41:16 +00:00
|
|
|
series[i][d.metric.label] = parseFloat(p[1]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
unitSuffix: unitSuffix,
|
|
|
|
labels: labelMap,
|
2020-11-11 16:59:15 +00:00
|
|
|
data: series,
|
2020-10-20 15:41:16 +00:00
|
|
|
};
|
|
|
|
};
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
fetchRequestRateSeries: function (service, dc, nspace, options) {
|
2020-10-20 15:41:16 +00:00
|
|
|
// We need the sum of all non-500 error rates as one value and the 500
|
|
|
|
// error rate as a separate series so that they stack to show the full
|
|
|
|
// request rate. Some creative label replacement makes this possible in
|
|
|
|
// one query.
|
2020-11-11 16:59:15 +00:00
|
|
|
var q =
|
|
|
|
`sum by (label) (` +
|
2020-10-20 15:41:16 +00:00
|
|
|
// The outer label_replace catches 5xx error and relabels them as
|
|
|
|
// err=yes
|
2020-11-11 16:59:15 +00:00
|
|
|
`label_replace(` +
|
|
|
|
// The inner label_replace relabels all !5xx rates as err=no so they
|
|
|
|
// will get summed together.
|
|
|
|
`label_replace(` +
|
|
|
|
// Get rate of requests to the service
|
2020-11-16 23:37:19 +00:00
|
|
|
`irate(envoy_listener_http_downstream_rq_xx{` +
|
|
|
|
`consul_source_service="${service}",` +
|
|
|
|
`consul_source_datacenter="${dc}",` +
|
|
|
|
`consul_source_namespace="${nspace}",` +
|
|
|
|
`envoy_http_conn_manager_prefix="public_listener"}[10m])` +
|
2020-11-11 16:59:15 +00:00
|
|
|
// ... inner replacement matches all code classes except "5" and
|
|
|
|
// applies err=no
|
|
|
|
`, "label", "Successes", "envoy_response_code_class", "[^5]")` +
|
|
|
|
// ... outer replacement matches code=5 and applies err=yes
|
|
|
|
`, "label", "Errors", "envoy_response_code_class", "5")` +
|
|
|
|
`)`;
|
2020-10-20 15:41:16 +00:00
|
|
|
var labelMap = {
|
|
|
|
Total: 'Total inbound requests per second',
|
2020-11-11 16:59:15 +00:00
|
|
|
Successes:
|
|
|
|
'Successful responses (with an HTTP response code not in the 5xx range) per second.',
|
2020-10-20 15:41:16 +00:00
|
|
|
Errors: 'Error responses (with an HTTP response code in the 5xx range) per second.',
|
|
|
|
};
|
2020-11-11 16:59:15 +00:00
|
|
|
return this.fetchSeries(q, options).then(this.reformatSeries(' rps', labelMap));
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
fetchDataRateSeries: function (service, dc, nspace, options) {
|
2020-10-20 15:41:16 +00:00
|
|
|
// 8 * converts from bytes/second to bits/second
|
2020-11-11 16:59:15 +00:00
|
|
|
var q =
|
|
|
|
`8 * sum by (label) (` +
|
2020-10-20 15:41:16 +00:00
|
|
|
// Label replace generates a unique label per rx/tx metric to stop them
|
|
|
|
// being summed together.
|
2020-11-11 16:59:15 +00:00
|
|
|
`label_replace(` +
|
|
|
|
// Get the tx rate
|
2020-11-16 23:37:19 +00:00
|
|
|
`irate(envoy_tcp_downstream_cx_tx_bytes_total{` +
|
|
|
|
`consul_source_service="${service}",` +
|
|
|
|
`consul_source_datacenter="${dc}",` +
|
|
|
|
`consul_source_namespace="${nspace}",` +
|
|
|
|
`envoy_tcp_prefix="public_listener"}[10m])` +
|
2020-11-11 16:59:15 +00:00
|
|
|
// Match all and apply the tx label
|
|
|
|
`, "label", "Outbound", "__name__", ".*"` +
|
2020-10-20 15:41:16 +00:00
|
|
|
// Union those vectors with the RX ones
|
2020-11-11 16:59:15 +00:00
|
|
|
`) or label_replace(` +
|
|
|
|
// Get the rx rate
|
2020-11-16 23:37:19 +00:00
|
|
|
`irate(envoy_tcp_downstream_cx_rx_bytes_total{` +
|
|
|
|
`consul_source_service="${service}",` +
|
|
|
|
`consul_source_datacenter="${dc}",` +
|
|
|
|
`consul_source_namespace="${nspace}",` +
|
|
|
|
`envoy_tcp_prefix="public_listener"}[10m])` +
|
2020-11-11 16:59:15 +00:00
|
|
|
// Match all and apply the rx label
|
|
|
|
`, "label", "Inbound", "__name__", ".*"` +
|
|
|
|
`)` +
|
|
|
|
`)`;
|
2020-10-20 15:41:16 +00:00
|
|
|
var labelMap = {
|
|
|
|
Total: 'Total bandwidth',
|
|
|
|
Inbound: 'Inbound data rate (data recieved) from the network in bits per second.',
|
|
|
|
Outbound: 'Outbound data rate (data transmitted) from the network in bits per second.',
|
|
|
|
};
|
2020-11-11 16:59:15 +00:00
|
|
|
return this.fetchSeries(q, options).then(this.reformatSeries('bps', labelMap));
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
makeSubject: function (service, dc, nspace, type) {
|
|
|
|
var entity = `${nspace}/${service} (${dc})`;
|
2020-11-11 16:59:15 +00:00
|
|
|
if (type == 'upstream') {
|
2020-10-09 20:31:15 +00:00
|
|
|
// {{GROUP}} is a placeholder that is replaced by the upstream name
|
2020-11-16 23:37:19 +00:00
|
|
|
return `${entity} → {{GROUP}}`;
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
2020-11-11 16:59:15 +00:00
|
|
|
if (type == 'downstream') {
|
2020-10-09 20:31:15 +00:00
|
|
|
// {{GROUP}} is a placeholder that is replaced by the downstream name
|
2020-11-16 23:37:19 +00:00
|
|
|
return `{{GROUP}} → ${entity}`;
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
2020-11-16 23:37:19 +00:00
|
|
|
return entity;
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
makeHTTPSelector: function (service, dc, nspace, type) {
|
2020-10-09 20:31:15 +00:00
|
|
|
// Downstreams are totally different
|
2020-11-11 16:59:15 +00:00
|
|
|
if (type == 'downstream') {
|
2020-11-16 23:37:19 +00:00
|
|
|
return `consul_destination_service="${service}",consul_destination_datacenter="${dc}",consul_destination_namespace="${nspace}"`;
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
2020-11-16 23:37:19 +00:00
|
|
|
var lc = `consul_source_service="${service}",consul_source_datacenter="${dc}",consul_source_namespace="${nspace}"`;
|
2020-11-11 16:59:15 +00:00
|
|
|
if (type == 'upstream') {
|
2020-11-16 23:37:19 +00:00
|
|
|
lc += `,envoy_http_conn_manager_prefix="upstream"`;
|
2020-10-09 20:31:15 +00:00
|
|
|
} else {
|
|
|
|
// Only care about inbound public listener
|
2020-11-16 23:37:19 +00:00
|
|
|
lc += `,envoy_http_conn_manager_prefix="public_listener"`;
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
2020-11-11 16:59:15 +00:00
|
|
|
return lc;
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
makeTCPSelector: function (service, dc, nspace, type) {
|
2020-10-09 20:31:15 +00:00
|
|
|
// Downstreams are totally different
|
2020-11-11 16:59:15 +00:00
|
|
|
if (type == 'downstream') {
|
2020-11-16 23:37:19 +00:00
|
|
|
return `consul_destination_service="${service}",consul_destination_datacenter="${dc}",consul_destination_namespace="${nspace}"`;
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
2020-11-16 23:37:19 +00:00
|
|
|
var lc = `consul_source_service="${service}",consul_source_datacenter="${dc}",consul_source_namespace="${nspace}"`;
|
2020-11-11 16:59:15 +00:00
|
|
|
if (type == 'upstream') {
|
2020-11-16 23:37:19 +00:00
|
|
|
lc += `,envoy_tcp_prefix=~"upstream.*"`;
|
2020-10-09 20:31:15 +00:00
|
|
|
} else {
|
|
|
|
// Only care about inbound public listener
|
2020-11-16 23:37:19 +00:00
|
|
|
lc += `,envoy_tcp_prefix="public_listener"`;
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
2020-11-11 16:59:15 +00:00
|
|
|
return lc;
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
groupQuery: function (type, q) {
|
2020-11-11 16:59:15 +00:00
|
|
|
if (type == 'upstream') {
|
2020-11-16 23:37:19 +00:00
|
|
|
q += ' by (consul_upstream_service,consul_upstream_datacenter,consul_upstream_namespace)';
|
2020-11-11 16:59:15 +00:00
|
|
|
} else if (type == 'downstream') {
|
2020-11-16 23:37:19 +00:00
|
|
|
q += ' by (consul_source_service,consul_source_datacenter,consul_source_namespace)';
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
2020-11-11 16:59:15 +00:00
|
|
|
return q;
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
groupByInfix: function (type) {
|
2020-11-11 16:59:15 +00:00
|
|
|
if (type == 'upstream') {
|
2020-11-16 23:37:19 +00:00
|
|
|
return 'upstream';
|
2020-11-11 16:59:15 +00:00
|
|
|
} else if (type == 'downstream') {
|
2020-11-16 23:37:19 +00:00
|
|
|
return 'source';
|
|
|
|
} else {
|
2020-11-11 16:59:15 +00:00
|
|
|
return false;
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
metricPrefixHTTP: function (type) {
|
2020-11-11 16:59:15 +00:00
|
|
|
if (type == 'downstream') {
|
|
|
|
return 'envoy_cluster_upstream_rq';
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
2020-11-11 16:59:15 +00:00
|
|
|
return 'envoy_http_downstream_rq';
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
metricPrefixTCP: function (type) {
|
2020-11-11 16:59:15 +00:00
|
|
|
if (type == 'downstream') {
|
|
|
|
return 'envoy_cluster_upstream_cx';
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
2020-11-11 16:59:15 +00:00
|
|
|
return 'envoy_tcp_downstream_cx';
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
fetchRPS: function (service, dc, nspace, type, options) {
|
|
|
|
var sel = this.makeHTTPSelector(service, dc, nspace, type);
|
|
|
|
var subject = this.makeSubject(service, dc, nspace, type);
|
2020-11-11 16:59:15 +00:00
|
|
|
var metricPfx = this.metricPrefixHTTP(type);
|
|
|
|
var q = `sum(rate(${metricPfx}_completed{${sel}}[15m]))`;
|
|
|
|
return this.fetchStat(
|
2020-11-16 23:37:19 +00:00
|
|
|
this.groupQuery(type, q),
|
2020-11-11 16:59:15 +00:00
|
|
|
'RPS',
|
2020-10-09 20:31:15 +00:00
|
|
|
`<b>${subject}</b> request rate averaged over the last 15 minutes`,
|
|
|
|
shortNumStr,
|
2020-11-16 23:37:19 +00:00
|
|
|
this.groupByInfix(type)
|
2020-11-11 16:59:15 +00:00
|
|
|
);
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
fetchER: function (service, dc, nspace, type, options) {
|
|
|
|
var sel = this.makeHTTPSelector(service, dc, nspace, type);
|
|
|
|
var subject = this.makeSubject(service, dc, nspace, type);
|
2020-11-11 16:59:15 +00:00
|
|
|
var groupBy = '';
|
|
|
|
if (type == 'upstream') {
|
2020-11-16 23:37:19 +00:00
|
|
|
groupBy +=
|
|
|
|
' by (consul_upstream_service,consul_upstream_datacenter,consul_upstream_namespace)';
|
2020-11-11 16:59:15 +00:00
|
|
|
} else if (type == 'downstream') {
|
2020-11-16 23:37:19 +00:00
|
|
|
groupBy += ' by (consul_source_service,consul_source_datacenter,consul_source_namespace)';
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
2020-11-11 16:59:15 +00:00
|
|
|
var metricPfx = this.metricPrefixHTTP(type);
|
|
|
|
var q = `sum(rate(${metricPfx}_xx{${sel},envoy_response_code_class="5"}[15m]))${groupBy}/sum(rate(${metricPfx}_xx{${sel}}[15m]))${groupBy}`;
|
|
|
|
return this.fetchStat(
|
|
|
|
q,
|
|
|
|
'ER',
|
2020-10-09 20:31:15 +00:00
|
|
|
`Percentage of <b>${subject}</b> requests which were 5xx status over the last 15 minutes`,
|
2020-11-16 23:37:19 +00:00
|
|
|
function (val) {
|
2020-11-11 16:59:15 +00:00
|
|
|
return shortNumStr(val) + '%';
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
2020-11-16 23:37:19 +00:00
|
|
|
this.groupByInfix(type)
|
2020-11-11 16:59:15 +00:00
|
|
|
);
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
fetchPercentile: function (percentile, service, dc, nspace, type, options) {
|
|
|
|
var sel = this.makeHTTPSelector(service, dc, nspace, type);
|
|
|
|
var subject = this.makeSubject(service, dc, nspace, type);
|
2020-11-11 16:59:15 +00:00
|
|
|
var groupBy = 'le';
|
|
|
|
if (type == 'upstream') {
|
2020-11-16 23:37:19 +00:00
|
|
|
groupBy += ',consul_upstream_service,consul_upstream_datacenter,consul_upstream_namespace';
|
2020-11-11 16:59:15 +00:00
|
|
|
} else if (type == 'downstream') {
|
2020-11-16 23:37:19 +00:00
|
|
|
groupBy += ',consul_source_service,consul_source_datacenter,consul_source_namespace';
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
2020-11-11 16:59:15 +00:00
|
|
|
var metricPfx = this.metricPrefixHTTP(type);
|
|
|
|
var q = `histogram_quantile(${percentile /
|
|
|
|
100}, sum by(${groupBy}) (rate(${metricPfx}_time_bucket{${sel}}[15m])))`;
|
|
|
|
return this.fetchStat(
|
|
|
|
q,
|
2020-10-09 20:31:15 +00:00
|
|
|
`P${percentile}`,
|
|
|
|
`<b>${subject}</b> ${percentile}th percentile request service time over the last 15 minutes`,
|
|
|
|
shortTimeStr,
|
2020-11-16 23:37:19 +00:00
|
|
|
this.groupByInfix(type)
|
2020-11-11 16:59:15 +00:00
|
|
|
);
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
fetchConnRate: function (service, dc, nspace, type, options) {
|
|
|
|
var sel = this.makeTCPSelector(service, dc, nspace, type);
|
|
|
|
var subject = this.makeSubject(service, dc, nspace, type);
|
2020-11-11 16:59:15 +00:00
|
|
|
var metricPfx = this.metricPrefixTCP(type);
|
|
|
|
var q = `sum(rate(${metricPfx}_total{${sel}}[15m]))`;
|
|
|
|
return this.fetchStat(
|
2020-11-16 23:37:19 +00:00
|
|
|
this.groupQuery(type, q),
|
2020-11-11 16:59:15 +00:00
|
|
|
'CR',
|
2020-10-09 20:31:15 +00:00
|
|
|
`<b>${subject}</b> inbound TCP connections per second averaged over the last 15 minutes`,
|
|
|
|
shortNumStr,
|
2020-11-16 23:37:19 +00:00
|
|
|
this.groupByInfix(type)
|
2020-11-11 16:59:15 +00:00
|
|
|
);
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
fetchServiceRx: function (service, dc, nspace, type, options) {
|
|
|
|
var sel = this.makeTCPSelector(service, dc, nspace, type);
|
|
|
|
var subject = this.makeSubject(service, dc, nspace, type);
|
2020-11-11 16:59:15 +00:00
|
|
|
var metricPfx = this.metricPrefixTCP(type);
|
|
|
|
var q = `8 * sum(rate(${metricPfx}_rx_bytes_total{${sel}}[15m]))`;
|
|
|
|
return this.fetchStat(
|
2020-11-16 23:37:19 +00:00
|
|
|
this.groupQuery(type, q),
|
2020-11-11 16:59:15 +00:00
|
|
|
'RX',
|
2020-10-09 20:31:15 +00:00
|
|
|
`<b>${subject}</b> received bits per second averaged over the last 15 minutes`,
|
|
|
|
shortNumStr,
|
2020-11-16 23:37:19 +00:00
|
|
|
this.groupByInfix(type)
|
2020-11-11 16:59:15 +00:00
|
|
|
);
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
fetchServiceTx: function (service, dc, nspace, type, options) {
|
|
|
|
var sel = this.makeTCPSelector(service, dc, nspace, type);
|
|
|
|
var subject = this.makeSubject(service, dc, nspace, type);
|
2020-11-11 16:59:15 +00:00
|
|
|
var metricPfx = this.metricPrefixTCP(type);
|
|
|
|
var q = `8 * sum(rate(${metricPfx}_tx_bytes_total{${sel}}[15m]))`;
|
|
|
|
var self = this;
|
|
|
|
return this.fetchStat(
|
2020-11-16 23:37:19 +00:00
|
|
|
this.groupQuery(type, q),
|
2020-11-11 16:59:15 +00:00
|
|
|
'TX',
|
2020-10-09 20:31:15 +00:00
|
|
|
`<b>${subject}</b> transmitted bits per second averaged over the last 15 minutes`,
|
|
|
|
shortNumStr,
|
2020-11-16 23:37:19 +00:00
|
|
|
this.groupByInfix(type)
|
2020-11-11 16:59:15 +00:00
|
|
|
);
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
fetchServiceNoRoute: function (service, dc, nspace, type, options) {
|
|
|
|
var sel = this.makeTCPSelector(service, dc, nspace, type);
|
|
|
|
var subject = this.makeSubject(service, dc, nspace, type);
|
2020-11-11 16:59:15 +00:00
|
|
|
var metricPfx = this.metricPrefixTCP(type);
|
|
|
|
var metric = '_no_route';
|
|
|
|
if (type == 'downstream') {
|
|
|
|
metric = '_connect_fail';
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
2020-11-11 16:59:15 +00:00
|
|
|
var q = `sum(rate(${metricPfx}${metric}{${sel}}[15m]))`;
|
|
|
|
return this.fetchStat(
|
2020-11-16 23:37:19 +00:00
|
|
|
this.groupQuery(type, q),
|
2020-11-11 16:59:15 +00:00
|
|
|
'NR',
|
2020-10-09 20:31:15 +00:00
|
|
|
`<b>${subject}</b> unroutable (failed) connections per second averaged over the last 15 minutes`,
|
|
|
|
shortNumStr,
|
2020-11-16 23:37:19 +00:00
|
|
|
this.groupByInfix(type)
|
2020-11-11 16:59:15 +00:00
|
|
|
);
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
fetchStat: function (promql, label, desc, formatter, groupByInfix) {
|
|
|
|
if (!groupByInfix) {
|
2020-10-09 20:31:15 +00:00
|
|
|
// If we don't have a grouped result and its just a single stat, return
|
|
|
|
// no result as a zero not a missing stat.
|
2020-11-11 16:59:15 +00:00
|
|
|
promql += ' OR on() vector(0)';
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
|
|
|
var params = {
|
|
|
|
query: promql,
|
2020-11-11 16:59:15 +00:00
|
|
|
time: new Date().getTime() / 1000,
|
|
|
|
};
|
2020-11-16 23:37:19 +00:00
|
|
|
return this.httpGet('/api/v1/query', params).then(function (response) {
|
|
|
|
if (!groupByInfix) {
|
2020-10-09 20:31:15 +00:00
|
|
|
// Not grouped, expect just one stat value return that
|
2020-11-11 16:59:15 +00:00
|
|
|
var v = parseFloat(response.data.result[0].value[1]);
|
2020-10-09 20:31:15 +00:00
|
|
|
return {
|
|
|
|
label: label,
|
|
|
|
desc: desc,
|
2020-11-16 15:22:24 +00:00
|
|
|
value: isNaN(v) ? '-' : formatter(v),
|
2020-11-11 16:59:15 +00:00
|
|
|
};
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var data = {};
|
|
|
|
for (var i = 0; i < response.data.result.length; i++) {
|
|
|
|
var res = response.data.result[i];
|
|
|
|
var v = parseFloat(res.value[1]);
|
2020-11-16 23:37:19 +00:00
|
|
|
var service = res.metric['consul_' + groupByInfix + '_service'];
|
|
|
|
var nspace = res.metric['consul_' + groupByInfix + '_namespace'];
|
|
|
|
var datacenter = res.metric['consul_' + groupByInfix + '_datacenter'];
|
|
|
|
var groupName = `${service}.${nspace}.${datacenter}`;
|
2020-10-09 20:31:15 +00:00
|
|
|
data[groupName] = {
|
|
|
|
label: label,
|
|
|
|
desc: desc.replace('{{GROUP}}', groupName),
|
2020-11-16 15:22:24 +00:00
|
|
|
value: isNaN(v) ? '-' : formatter(v),
|
2020-11-11 16:59:15 +00:00
|
|
|
};
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
|
|
|
return data;
|
2020-11-11 16:59:15 +00:00
|
|
|
});
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
|
|
|
|
2020-11-16 23:37:19 +00:00
|
|
|
fetchSeries: function (promql, options) {
|
2020-10-09 20:31:15 +00:00
|
|
|
var params = {
|
|
|
|
query: promql,
|
|
|
|
start: options.start,
|
|
|
|
end: options.end,
|
2020-11-11 16:59:15 +00:00
|
|
|
step: '10s',
|
|
|
|
timeout: '8s',
|
|
|
|
};
|
|
|
|
return this.httpGet('/api/v1/query_range', params);
|
2020-10-09 20:31:15 +00:00
|
|
|
},
|
2020-11-11 16:59:15 +00:00
|
|
|
};
|
2020-10-09 20:31:15 +00:00
|
|
|
|
|
|
|
// Helper functions
|
|
|
|
function shortNumStr(n) {
|
|
|
|
if (n < 1e3) {
|
2020-11-11 16:59:15 +00:00
|
|
|
if (Number.isInteger(n)) return '' + n;
|
2020-10-09 20:31:15 +00:00
|
|
|
if (n >= 100) {
|
|
|
|
// Go to 3 significant figures but wrap it in Number to avoid scientific
|
|
|
|
// notation lie 2.3e+2 for 230.
|
2020-11-11 16:59:15 +00:00
|
|
|
return Number(n.toPrecision(3));
|
|
|
|
}
|
|
|
|
if (n < 1) {
|
2020-10-09 20:31:15 +00:00
|
|
|
// Very small numbers show with limited precision to prevent long string
|
|
|
|
// of 0.000000.
|
|
|
|
return Number(n.toFixed(2));
|
|
|
|
} else {
|
|
|
|
// Two sig figs is enough below this
|
|
|
|
return Number(n.toPrecision(2));
|
|
|
|
}
|
|
|
|
}
|
2020-11-11 16:59:15 +00:00
|
|
|
if (n >= 1e3 && n < 1e6) return +(n / 1e3).toPrecision(3) + 'k';
|
|
|
|
if (n >= 1e6 && n < 1e9) return +(n / 1e6).toPrecision(3) + 'm';
|
|
|
|
if (n >= 1e9 && n < 1e12) return +(n / 1e9).toPrecision(3) + 'g';
|
|
|
|
if (n >= 1e12) return +(n / 1e12).toFixed(0) + 't';
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function shortTimeStr(n) {
|
2020-11-11 16:59:15 +00:00
|
|
|
if (n < 1e3) return Math.round(n) + 'ms';
|
2020-10-09 20:31:15 +00:00
|
|
|
|
2020-11-11 16:59:15 +00:00
|
|
|
var secs = n / 1e3;
|
|
|
|
if (secs < 60) return secs.toFixed(1) + 's';
|
2020-10-09 20:31:15 +00:00
|
|
|
|
2020-11-11 16:59:15 +00:00
|
|
|
var mins = secs / 60;
|
|
|
|
if (mins < 60) return mins.toFixed(1) + 'm';
|
2020-10-09 20:31:15 +00:00
|
|
|
|
2020-11-11 16:59:15 +00:00
|
|
|
var hours = mins / 60;
|
|
|
|
if (hours < 24) return hours.toFixed(1) + 'h';
|
2020-10-09 20:31:15 +00:00
|
|
|
|
2020-11-11 16:59:15 +00:00
|
|
|
var days = hours / 24;
|
|
|
|
return days.toFixed(1) + 'd';
|
2020-10-09 20:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* global consul:writable */
|
2020-11-11 16:59:15 +00:00
|
|
|
window.consul.registerMetricsProvider('prometheus', prometheusProvider);
|
|
|
|
})();
|