@ultimat3/core 31.17.0 → 31.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +2 -2
- package/src/process-metrics.ts +35 -3
package/README.md
CHANGED
|
@@ -94,7 +94,7 @@ JSON.stringify({ dsn }); // the value never prints: a Secret redacts by value, a
|
|
|
94
94
|
| OTel-shaped counter / gauge / histogram, same seam | `metrics.ts` |
|
|
95
95
|
| the `/metrics` scrape body | `metrics-text.ts` |
|
|
96
96
|
| the series every process emits, incl. what the chart scales on | `runtime-metrics.ts` |
|
|
97
|
-
| what the process itself costs: `process_resident_memory_bytes`, `process_heap_used_bytes`, `process_heap_total_bytes`, `process_external_memory_bytes`, `process_cpu_seconds_total`, `process_event_loop_lag_seconds`, `process_start_time_seconds`, `process_info{role}` — **server-only**, never on `@ultimat3/core/page` | `process-metrics.ts` (`startProcessMetrics`) |
|
|
97
|
+
| what the process itself costs: `process_resident_memory_bytes`, `process_heap_used_bytes`, `process_heap_total_bytes`, `process_external_memory_bytes`, `process_cpu_seconds_total`, `process_event_loop_lag_seconds`, `process_start_time_seconds`, `process_info{role}`, `process_memory_limit_bytes` (cgroup `memory.max`; absent when unbounded) — **server-only**, never on `@ultimat3/core/page` | `process-metrics.ts` (`startProcessMetrics`) |
|
|
98
98
|
| graceful drain, `/healthz`, `/readyz` | `lifecycle.ts` |
|
|
99
99
|
| what a health endpoint tells whom — `healthBody(report, role, detailed)`, `healthPeerListed(peers, address)`, `DEFAULT_HEALTH_DETAIL_PEERS`; the one rule `@ultimat3/http` and the sync node's own listener both call | `health-disclosure.ts` |
|
|
100
100
|
| the readiness grace between `/readyz` → 503 and the listener closing (`drain.readinessGraceMs`) | `lifecycle-grace.ts` |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/core",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.18.0",
|
|
4
4
|
"description": "Ultimate's foundation: errors, context, env, config, clock, ids, logging, telemetry, lifecycle",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -54,6 +54,6 @@
|
|
|
54
54
|
"test": "bun test"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@ultimat3/schema": "31.
|
|
57
|
+
"@ultimat3/schema": "31.18.0"
|
|
58
58
|
}
|
|
59
59
|
}
|
package/src/process-metrics.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
// Single responsibility: the series every Ultimate PROCESS emits about itself — memory, CPU,
|
|
2
|
-
// event-loop lag, start time and
|
|
3
|
-
// this names what the process costs, so "what is growing?" is a query
|
|
4
|
-
// nothing at import: the first `startProcessMetrics()` declares the instruments.
|
|
2
|
+
// event-loop lag, start time, the container's memory limit and its role. `runtime-metrics.ts`
|
|
3
|
+
// names what a role does; this names what the process costs, so "what is growing?" is a query.
|
|
4
|
+
// Runs nothing at import: the first `startProcessMetrics()` declares the instruments.
|
|
5
5
|
|
|
6
|
+
// why: one synchronous read of a kernel file at start; Bun.file is async and the start is not.
|
|
7
|
+
import { readFileSync } from 'node:fs';
|
|
6
8
|
import { type Clock, systemClock } from './clock';
|
|
7
9
|
import { finiteCount } from './finite-option';
|
|
8
10
|
import type { Counter, Gauge, Histogram } from './metrics';
|
|
@@ -54,11 +56,32 @@ export function readProcess(): ProcessReading {
|
|
|
54
56
|
};
|
|
55
57
|
}
|
|
56
58
|
|
|
59
|
+
/** Where cgroup v2 states the container's memory limit: bytes, or `max` for none. */
|
|
60
|
+
export const CGROUP_MEMORY_MAX = '/sys/fs/cgroup/memory.max';
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The memory limit the kernel enforces on this container, in bytes, or `undefined` for none: no
|
|
64
|
+
* file (not Linux, cgroup v1, no namespace), `max`, or anything that is not a whole number.
|
|
65
|
+
*/
|
|
66
|
+
export function readMemoryLimit(path: string = CGROUP_MEMORY_MAX): number | undefined {
|
|
67
|
+
let text: string;
|
|
68
|
+
try {
|
|
69
|
+
text = readFileSync(path, 'utf-8').trim();
|
|
70
|
+
} catch {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
if (!/^\d+$/.test(text)) return undefined;
|
|
74
|
+
const bytes = Number(text);
|
|
75
|
+
return Number.isSafeInteger(bytes) && bytes > 0 ? bytes : undefined;
|
|
76
|
+
}
|
|
77
|
+
|
|
57
78
|
export interface ProcessMetricsOptions {
|
|
58
79
|
/** What this process is: a `ROLE`, or `x dev`'s several joined. The `process_info` label. */
|
|
59
80
|
readonly role: string;
|
|
60
81
|
/** Defaults to `readProcess`. Injected by a test. */
|
|
61
82
|
readonly read?: (() => ProcessReading) | undefined;
|
|
83
|
+
/** Defaults to `readMemoryLimit`, read once per start. Injected by a test. */
|
|
84
|
+
readonly memoryLimit?: (() => number | undefined) | undefined;
|
|
62
85
|
readonly clock?: Clock | undefined;
|
|
63
86
|
/** The sampler's timer. Injected by a test; the default is an unref'd `setInterval`. */
|
|
64
87
|
readonly every?: ((tick: () => void, intervalMs: number) => () => void) | undefined;
|
|
@@ -86,6 +109,13 @@ let active: { readonly stopTimer: () => void } | undefined;
|
|
|
86
109
|
// The role `process_info` last said 1 for, so a start under another role sets it back to 0 —
|
|
87
110
|
// one process, one role, even across a stop and a start.
|
|
88
111
|
let infoRole: string | undefined;
|
|
112
|
+
// Declared by the first start that finds a limit and recorded once per start, never observed: an
|
|
113
|
+
// unbounded container then never names the series, and an alert on rss ÷ limit has nothing to divide.
|
|
114
|
+
const recordMemoryLimit = (bytes: number): void =>
|
|
115
|
+
gauge('process_memory_limit_bytes', {
|
|
116
|
+
unit: 'By',
|
|
117
|
+
description: 'The memory limit the kernel enforces on this container (cgroup memory.max)',
|
|
118
|
+
}).record(bytes);
|
|
89
119
|
|
|
90
120
|
/** 0 while stopped: a scrape between a stop and a start reads a flat line, never a throw. */
|
|
91
121
|
const observed = (pick: (reading: ProcessReading) => number) => (): number =>
|
|
@@ -178,6 +208,8 @@ export function startProcessMetrics(options: ProcessMetricsOptions): () => void
|
|
|
178
208
|
}
|
|
179
209
|
infoRole = options.role;
|
|
180
210
|
declared.info.record(1, { role: options.role });
|
|
211
|
+
const limit = (options.memoryLimit ?? readMemoryLimit)();
|
|
212
|
+
if (limit !== undefined) recordMemoryLimit(limit);
|
|
181
213
|
|
|
182
214
|
const countCpu = (): void => {
|
|
183
215
|
const cpu = read().cpuSeconds;
|