-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathperf-metrics-capture.ts
More file actions
130 lines (113 loc) · 3.23 KB
/
Copy pathperf-metrics-capture.ts
File metadata and controls
130 lines (113 loc) · 3.23 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import fs from "node:fs";
import path from "node:path";
import { getPerfMetricsSnapshot, resetPerfMetrics } from "./perf-metrics.js";
const PERF_METRICS_FILE_ENV = "ACPX_PERF_METRICS_FILE";
let installed = false;
let flushed = false;
let captureFilePath: string | undefined;
let captureRole = "cli";
let captureArgv: string[] = [];
let captureSequence = 0;
type CaptureReason = "checkpoint" | "exit" | "signal";
function shouldCapture(): boolean {
return typeof captureFilePath === "string" && captureFilePath.trim().length > 0;
}
function buildPayload(reason: CaptureReason): Record<string, unknown> {
return {
timestamp: new Date().toISOString(),
pid: process.pid,
ppid: process.ppid,
role: captureRole,
argv: captureArgv,
cwd: process.cwd(),
sequence: captureSequence,
reason,
metrics: getPerfMetricsSnapshot(),
};
}
function payloadHasMetrics(payload: Record<string, unknown>): boolean {
const metrics = payload.metrics as {
counters?: Record<string, number>;
gauges?: Record<string, number>;
timings?: Record<string, unknown>;
};
return [metrics.counters, metrics.gauges, metrics.timings].some(
(entries) => Object.keys(entries ?? {}).length > 0,
);
}
function appendPerfMetricsPayload(payload: Record<string, unknown>): void {
fs.mkdirSync(path.dirname(captureFilePath!), { recursive: true });
fs.appendFileSync(captureFilePath!, `${JSON.stringify(payload)}\n`, "utf8");
captureSequence += 1;
}
function writePerfMetricsCapture(reason: CaptureReason, resetAfterWrite: boolean): boolean {
if (!shouldCapture()) {
return false;
}
const payload = buildPayload(reason);
if (!payloadHasMetrics(payload)) {
return false;
}
try {
appendPerfMetricsPayload(payload);
if (resetAfterWrite) {
resetPerfMetrics();
}
return true;
} catch {
// metrics capture is best-effort only
return false;
}
}
export function checkpointPerfMetricsCapture(): void {
flushed = false;
writePerfMetricsCapture("checkpoint", true);
}
export function flushPerfMetricsCapture(reason: CaptureReason = "exit"): void {
if (flushed || !shouldCapture()) {
return;
}
flushed = true;
writePerfMetricsCapture(reason, false);
}
export function installPerfMetricsCapture(
options: {
argv?: string[];
role?: string;
filePath?: string;
} = {},
): void {
captureFilePath = options.filePath ?? process.env[PERF_METRICS_FILE_ENV];
if (!shouldCapture()) {
return;
}
resetPerfMetrics();
captureRole = options.role ?? captureRole;
captureArgv = options.argv ?? [];
captureSequence = 0;
flushed = false;
if (installed) {
return;
}
installed = true;
process.once("exit", () => {
flushPerfMetricsCapture("exit");
});
for (const signal of ["SIGINT", "SIGTERM"] as const) {
const handler = () => {
flushPerfMetricsCapture("signal");
process.removeListener(signal, handler);
process.kill(process.pid, signal);
};
process.once(signal, handler);
}
}
export function perfMetricsCaptureFileFromEnv(
env: NodeJS.ProcessEnv = process.env,
): string | undefined {
const value = env[PERF_METRICS_FILE_ENV];
if (typeof value !== "string" || value.trim().length === 0) {
return undefined;
}
return value;
}