-
Notifications
You must be signed in to change notification settings - Fork 773
Refactor health monitoring logic and snapshot collection #333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,38 @@ | ||
| import type { ISdk } from "iii-sdk"; | ||
| import type { HealthSnapshot } from "../types.js"; | ||
| import type { StateKV } from "../state/kv.js"; | ||
| import { KV } from "../state/schema.js"; | ||
| import { evaluateHealth } from "./thresholds.js"; | ||
| const endMark = performance.now(); | ||
| const eventLoopLag = endMark - startMark; | ||
|
|
||
|
Comment on lines
+1
to
3
|
||
| export function registerHealthMonitor( | ||
| sdk: ISdk, | ||
| kv: StateKV, | ||
| ): { stop: () => void } { | ||
| let connectionState = "connected"; | ||
| let prevCpuUsage = process.cpuUsage(); | ||
| let prevCpuTime = Date.now(); | ||
| const snapshot: HealthSnapshot = { | ||
| cpuUsage: cpuPercent, | ||
| memoryRss: mem.rss / 1024 / 1024, | ||
| memoryHeapUsed: mem.heapUsed / 1024 / 1024, | ||
| eventLoopLag, | ||
| uptime, | ||
| connectionState, | ||
| timestamp: now, | ||
| }; | ||
|
Comment on lines
+4
to
+12
|
||
|
|
||
| if (typeof sdk.on === "function") { | ||
| sdk.on("connection_state", (state?: unknown) => { | ||
| connectionState = state as string; | ||
| }); | ||
| } | ||
| const status = evaluateHealth(snapshot); | ||
|
|
||
| // Feature: Persistence & State-Aware Alerting | ||
| const lastStatus = await kv.get(KV.LAST_HEALTH_STATUS); | ||
| if (status.isCritical && lastStatus !== "critical") { | ||
| sdk.emit?.("health_alert", { snapshot, status }); | ||
| } | ||
|
Comment on lines
+17
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle first-run case in alerting logic. On the first monitor run, 🛡️ Proposed fix to skip alerting on first run const lastStatus = await kv.get(KV.LAST_HEALTH_STATUS);
-if (status.isCritical && lastStatus !== "critical") {
+if (status.isCritical && lastStatus && lastStatus !== "critical") {
sdk.emit?.("health_alert", { snapshot, status });
}Or explicitly handle first run: const lastStatus = await kv.get(KV.LAST_HEALTH_STATUS);
-if (status.isCritical && lastStatus !== "critical") {
+if (status.isCritical && lastStatus !== null && lastStatus !== "critical") {
sdk.emit?.("health_alert", { snapshot, status });
}🤖 Prompt for AI Agents |
||
|
|
||
| async function collectHealth(): Promise<HealthSnapshot> { | ||
| const mem = process.memoryUsage(); | ||
| const currentCpu = process.cpuUsage(); | ||
| const now = Date.now(); | ||
| const uptime = process.uptime(); | ||
| await kv.set(KV.LATEST_HEALTH, snapshot); | ||
| await kv.set(KV.LAST_HEALTH_STATUS, status.level); | ||
|
|
||
| const elapsedMs = now - prevCpuTime; | ||
| const userDelta = currentCpu.user - prevCpuUsage.user; | ||
| const systemDelta = currentCpu.system - prevCpuUsage.system; | ||
| const cpuPercent = | ||
| elapsedMs > 0 ? ((userDelta + systemDelta) / 1000 / elapsedMs) * 100 : 0; | ||
| prevCpuUsage = currentCpu; | ||
| prevCpuTime = now; | ||
| return snapshot; | ||
| } | ||
|
|
||
| const startMark = performance.now(); | ||
| await new Promise((resolve) => setImmediate(resolve)); | ||
| const interval = setInterval(collectHealth, 5000); | ||
| return { | ||
| stop: () => { | ||
| clearInterval(interval); | ||
| sdk.emit?.("monitor_stopped", { at: Date.now() }); | ||
| } | ||
| }; | ||
|
Comment on lines
+28
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling, initial call, and interval.unref(). Three concerns with the monitoring loop:
🔧 Proposed fix+collectHealth().catch(() => {});
const interval = setInterval(collectHealth, 5000);
+interval.unref();
return {
stop: () => {
clearInterval(interval);
sdk.emit?.("monitor_stopped", { at: Date.now() });
}
};For better error handling, consider logging errors instead of silently catching: -collectHealth().catch(() => {});
+collectHealth().catch((err) => {
+ sdk.emit?.("monitor_error", { error: String(err) });
+});
const interval = setInterval(() => {
- collectHealth();
+ collectHealth().catch((err) => {
+ sdk.emit?.("monitor_error", { error: String(err) });
+ });
}, 5000);🧰 Tools🪛 Biome (2.4.15)[error] 29-34: Illegal return statement outside of a function (parse) 🤖 Prompt for AI Agents |
||
| } | ||
| const eventLoopLagMs = performance.now() - startMark; | ||
|
|
||
| let workers: HealthSnapshot["workers"] = []; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 46
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 3011
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 4100
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 887
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 1209
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 965
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 3195
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 146
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 587
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 50501
Define
startMarkbefore using it in the event loop lag calculation.The variable
startMarkis referenced on line 2 but is never defined anywhere in the function. This will cause a runtime error. EnsurestartMarkis captured at the start ofcollectHealth()withconst startMark = performance.now();before theendMarkcalculation.🤖 Prompt for AI Agents