openreplay/assist/utils/health.js
Alexander 4dfcafc572
Assist metrics (#1627)
* feat(assist): added default metrics

* feat(assist): added assist_ label to easy metrics sort

* feat(assist): finished metrics template implementation (now available on /metrics)

* feat(assist): removed unnecessary _ symbol from app name

* feat(assist): integrated metrics to rest api and websockets handlers

* feat(assist): fixed wrong naming issue

* feat(assist): added connection type to some metrics

* feat(assist): added url route parser for metrics stats
2023-11-09 15:06:11 +01:00

60 lines
1.6 KiB
JavaScript

const express = require('express');
const HOST = process.env.LISTEN_HOST || '0.0.0.0';
const PORT = process.env.HEALTH_PORT || 8888;
const register = require('./metrics').register;
const {request_logger} = require("./helper");
const debug = process.env.debug === "1";
const respond = function (res, data) {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({"data": data}));
}
const check_health = async function (req, res) {
debug && console.log("[WS]looking for all available sessions");
respond(res, {
"health": true,
"details": {
"version": process.env.npm_package_version
}
});
}
const healthApp = express();
healthApp.use(express.json());
healthApp.use(express.urlencoded({extended: true}));
healthApp.use(request_logger("[healthApp]"));
healthApp.get(['/'], (req, res) => {
res.statusCode = 200;
res.end("healthApp ok!");
}
);
healthApp.get('/health', check_health);
healthApp.get('/shutdown', (req, res) => {
console.log("Requested shutdown");
res.statusCode = 200;
res.end("ok!");
process.kill(1, "SIGTERM");
}
);
healthApp.get('/metrics', async (req, res) => {
try {
res.set('Content-Type', register.contentType);
res.end(await register.metrics());
} catch (ex) {
res.status(500).end(ex);
}
});
const listen_cb = async function () {
console.log(`Health App listening on http://${HOST}:${PORT}`);
console.log('Press Ctrl+C to quit.');
}
module.exports = {
healthApp,
PORT,
listen_cb
};