metrics: add stream api for system info

This commit is contained in:
Girish Ramakrishnan
2025-05-21 17:15:04 +02:00
parent 7e3162d287
commit c0f0084e56
6 changed files with 83 additions and 27 deletions

View File

@@ -9,6 +9,7 @@ exports = module.exports = {
getLogs,
getLogStream,
getMetrics,
getMetricStream,
getBlockDevices,
getCpus,
};
@@ -130,6 +131,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 [error, metricStream] = await safe(metrics.getSystemStream());
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));
}
async function getBlockDevices(req, res, next) {
const [error, devices] = await safe(system.getBlockDevices());
if (error) return next(new HttpError(500, error));