metrics: add route to query app metrics with the system
This commit is contained in:
@@ -66,7 +66,14 @@ async function onPeriodChange() {
|
||||
|
||||
if (period.value.hours === 0) return await liveRefresh();
|
||||
|
||||
const [error, metrics] = await systemModel.getMetrics({ fromSecs: period.value.hours * 60 * 60, intervalSecs: period.value.intervalSecs });
|
||||
const options = {
|
||||
fromSecs: period.value.hours * 60 * 60,
|
||||
intervalSecs: period.value.intervalSecs,
|
||||
system: true,
|
||||
appIds: [],
|
||||
serviceIds: []
|
||||
};
|
||||
const [error, metrics] = await systemModel.getMetrics(options);
|
||||
if (error) return console.error(error);
|
||||
|
||||
cpuGraphItem.value.setData(metrics.system.cpu);
|
||||
|
||||
@@ -86,7 +86,15 @@ function create() {
|
||||
async getMetrics(options) {
|
||||
let error, result;
|
||||
try {
|
||||
result = await fetcher.get(`${API_ORIGIN}/api/v1/system/metrics`, { fromSecs: options.fromSecs, intervalSecs: options.intervalSecs, access_token: accessToken });
|
||||
const query = [
|
||||
['fromSecs', options.fromSecs],
|
||||
['intervalSecs', options.intervalSecs],
|
||||
['system', String(!!options.system)],
|
||||
...options.appIds.map(id => ['appId', id]), // multiple appId=xx
|
||||
...options.serviceIds.map(id => ['serviceId', id]), // multiple serviceId=xx
|
||||
['access_token', accessToken]
|
||||
];
|
||||
result = await fetcher.get(`${API_ORIGIN}/api/v1/system/metrics`, query);
|
||||
} catch (e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
+8
-12
@@ -364,23 +364,19 @@ async function readSystemFromGraphite(options) {
|
||||
async function getSystem(options) {
|
||||
assert.strictEqual(typeof options, 'object');
|
||||
|
||||
const systemStats = await readSystemFromGraphite(options);
|
||||
const result = {};
|
||||
|
||||
const appStats = {};
|
||||
for (const app of await apps.list()) {
|
||||
appStats[app.id] = await getContainer(app.id, options);
|
||||
if (options.system) result.system = await readSystemFromGraphite(options);
|
||||
|
||||
for (const appId of options.appIds) {
|
||||
result[appId] = await getContainer(appId, options);
|
||||
}
|
||||
|
||||
const serviceStats = {};
|
||||
for (const serviceId of await services.listServices()) {
|
||||
serviceStats[serviceId] = await getContainer(serviceId, options);
|
||||
for (const serviceId of options.serviceIds) {
|
||||
result[serviceId] = await getContainer(serviceId, options);
|
||||
}
|
||||
|
||||
return {
|
||||
system: systemStats, // { cpu, memory, swap, block{Read,Write}{Rate,Total}, network{Read,Write}{Rate,Total}
|
||||
apps: appStats,
|
||||
services: serviceStats,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
async function getSystemStream(options) {
|
||||
|
||||
@@ -125,7 +125,11 @@ async function getMetrics(req, res, next) {
|
||||
const fromSecs = parseInt(req.query.fromSecs, 10);
|
||||
const intervalSecs = parseInt(req.query.intervalSecs, 10);
|
||||
const noNullPoints = !!req.query.noNullPoints;
|
||||
const [error, result] = await safe(metrics.getSystem({ fromSecs, intervalSecs, noNullPoints }));
|
||||
const system = req.query.system === 'true';
|
||||
const appIds = 'appId' in req.query ? (Array.isArray(req.query.appId) ? req.query.appId : [ req.query.appId ]) : [];
|
||||
const serviceIds = 'serviceId' in req.query ? (Array.isArray(req.query.serviceId) ? req.query.serviceId : [ req.query.serviceId ]) : [];
|
||||
|
||||
const [error, result] = await safe(metrics.getSystem({ fromSecs, intervalSecs, noNullPoints, system, appIds, serviceIds }));
|
||||
if (error) return next(new HttpError(500, error));
|
||||
|
||||
next(new HttpSuccess(200, result));
|
||||
|
||||
Reference in New Issue
Block a user