forked from Lilly-Protocol/lily-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.service.ts
More file actions
42 lines (37 loc) · 1.16 KB
/
Copy pathmetrics.service.ts
File metadata and controls
42 lines (37 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { monitorEventLoopDelay, type IntervalHistogram } from "node:perf_hooks";
import { env } from "../../config/env";
import type { ProcessMetrics } from "./metrics.types";
let histogram: IntervalHistogram | null = null;
const getHistogram = (): IntervalHistogram => {
if (!histogram) {
histogram = monitorEventLoopDelay({ resolution: 20 });
histogram.enable();
}
return histogram;
};
export const getEventLoopLagMs = (): number => {
const h = getHistogram();
const mean = h.mean;
if (!Number.isFinite(mean) || mean <= 0) {
return 0;
}
return Number((mean / 1_000_000).toFixed(2));
};
export const metricsService = {
getMetrics: (): ProcessMetrics => {
const memory = process.memoryUsage();
return {
uptimeSeconds: Math.floor(process.uptime()),
memoryUsage: {
rssBytes: memory.rss,
heapTotalBytes: memory.heapTotal,
heapUsedBytes: memory.heapUsed,
externalBytes: memory.external,
},
eventLoopLagMs: getEventLoopLagMs(),
nodeVersion: process.version,
environment: env.NODE_ENV,
timestamp: new Date().toISOString(),
};
},
};