Merge pull request #499 from mchangrh/getStatus-redis

add redis process time to getStatus
This commit is contained in:
Ajay Ramachandran 2022-09-29 16:52:16 -04:00 committed by GitHub
commit 847f1bbabb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 6 deletions

View file

@ -3,25 +3,41 @@ import { Logger } from "../utils/logger";
import { Request, Response } from "express"; import { Request, Response } from "express";
import os from "os"; import os from "os";
import redis from "../utils/redis"; import redis from "../utils/redis";
import { promiseOrTimeout } from "../utils/promise";
export async function getStatus(req: Request, res: Response): Promise<Response> { export async function getStatus(req: Request, res: Response): Promise<Response> {
const startTime = Date.now(); const startTime = Date.now();
let value = req.params.value as string[] | string; let value = req.params.value as string[] | string;
value = Array.isArray(value) ? value[0] : value; value = Array.isArray(value) ? value[0] : value;
let processTime, redisProcessTime = -1;
try { try {
const dbVersion = (await db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])).value; const dbVersion = await promiseOrTimeout(db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"]), 5000)
.then(e => {
processTime = Date.now() - startTime;
return e.value;
})
.catch(e => {
Logger.error(`status: SQL query timed out: ${e}`);
return -1;
});
let statusRequests: unknown = 0; let statusRequests: unknown = 0;
try { const numberRequests = await promiseOrTimeout(redis.increment("statusRequest"), 5000)
const numberRequests = await redis.increment("statusRequest"); .then(e => {
statusRequests = numberRequests?.[0]; redisProcessTime = Date.now() - startTime;
} catch (error) { } // eslint-disable-line no-empty return e;
}).catch(e => {
Logger.error(`status: redis increment timed out ${e}`);
return [-1];
});
statusRequests = numberRequests?.[0];
const statusValues: Record<string, any> = { const statusValues: Record<string, any> = {
uptime: process.uptime(), uptime: process.uptime(),
commit: (global as any).HEADCOMMIT || "unknown", commit: (global as any).HEADCOMMIT || "unknown",
db: Number(dbVersion), db: Number(dbVersion),
startTime, startTime,
processTime: Date.now() - startTime, processTime,
redisProcessTime,
loadavg: os.loadavg().slice(1), // only return 5 & 15 minute load average loadavg: os.loadavg().slice(1), // only return 5 & 15 minute load average
statusRequests, statusRequests,
hostname: os.hostname() hostname: os.hostname()

View file

@ -110,4 +110,16 @@ describe("getStatus", () => {
}) })
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should be able to get redis latency", function (done) {
if (!config.redis?.enabled) this.skip();
client.get(endpoint)
.then(res => {
assert.strictEqual(res.status, 200);
const data = res.data;
assert.ok(data.redisProcessTime >= 0);
done();
})
.catch(err => done(err));
});
}); });