apps: add live metrics route
This commit is contained in:
@@ -66,6 +66,7 @@ exports = module.exports = {
|
||||
downloadBackup,
|
||||
|
||||
getMetrics,
|
||||
getMetricStream,
|
||||
|
||||
load
|
||||
};
|
||||
@@ -1080,3 +1081,30 @@ async function getMetrics(req, res, next) {
|
||||
|
||||
next(new HttpSuccess(200, result));
|
||||
}
|
||||
|
||||
async function getMetricStream(req, res, next) {
|
||||
if (req.headers.accept !== 'text/event-stream') return next(new HttpError(400, 'This API call requires EventStream'));
|
||||
|
||||
const intervalMsecs = typeof req.query.intervalMsecs !== 'undefined' ? parseInt(req.query.intervalMsecs, 10) : 5000;
|
||||
if (!intervalMsecs || intervalMsecs < 100) return next(new HttpError(400, 'intervalSecs query param must be atleast 100'));
|
||||
|
||||
const [error, metricStream] = await safe(metrics.getContainerStream(req.resources.app.id, { intervalMsecs }));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache',
|
||||
'Connection': 'keep-alive',
|
||||
'X-Accel-Buffering': 'no', // disable nginx buffering
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
});
|
||||
res.write('retry: 3000\n');
|
||||
res.on('close', () => metricStream.destroy());
|
||||
metricStream.on('data', function (data) {
|
||||
const obj = JSON.parse(data);
|
||||
const sse = `data: ${JSON.stringify(obj)}\n\n`;
|
||||
res.write(sse);
|
||||
});
|
||||
metricStream.on('end', res.end.bind(res));
|
||||
metricStream.on('error', res.end.bind(res, null));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user