3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 20:35:02 +00:00

Add adhoc REST call debug stats

This commit is contained in:
Dragory 2021-09-11 21:14:47 +03:00
parent 3bc0015dbe
commit 07f23d4137
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
6 changed files with 79 additions and 8 deletions

View file

@ -0,0 +1,35 @@
import { sorter } from "./utils";
Error.stackTraceLimit = Infinity;
type CallStats = { method: string; path: string; source: string; count: number };
const restCallStats: Map<string, CallStats> = new Map();
const looseSnowflakeRegex = /\d{15,}/g;
const queryParamsRegex = /\?.*$/g;
export function logRestCall(method: string, path: string) {
const anonymizedPath = path.replace(looseSnowflakeRegex, "0000").replace(queryParamsRegex, "");
const stackLines = (new Error().stack || "").split("\n").slice(10); // Remove initial fluff
const firstSrcLine = stackLines.findIndex((line) => line.includes("/backend/src"));
const source = stackLines
.slice(firstSrcLine !== -1 ? firstSrcLine : -5)
.filter((l) => !l.includes("processTicksAndRejections"))
.join("\n");
const key = `${method}|${anonymizedPath}|${source}`;
if (!restCallStats.has(key)) {
restCallStats.set(key, {
method,
path: anonymizedPath,
source,
count: 0,
});
}
restCallStats.get(key)!.count++;
}
export function getTopRestCallStats(count: number): CallStats[] {
const stats = Array.from(restCallStats.values());
stats.sort(sorter("count", "DESC"));
return stats.slice(0, count);
}