@zintrust/workers 0.1.29 → 0.1.30
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 +16 -1
- package/dist/AnomalyDetection.d.ts +4 -0
- package/dist/AnomalyDetection.js +8 -0
- package/dist/BroadcastWorker.d.ts +2 -0
- package/dist/CanaryController.js +49 -5
- package/dist/ChaosEngineering.js +13 -0
- package/dist/ClusterLock.js +21 -10
- package/dist/DeadLetterQueue.js +12 -8
- package/dist/MultiQueueWorker.d.ts +1 -1
- package/dist/MultiQueueWorker.js +12 -7
- package/dist/NotificationWorker.d.ts +2 -0
- package/dist/PriorityQueue.d.ts +2 -2
- package/dist/PriorityQueue.js +20 -21
- package/dist/ResourceMonitor.js +65 -38
- package/dist/WorkerFactory.d.ts +23 -3
- package/dist/WorkerFactory.js +420 -40
- package/dist/WorkerInit.js +8 -3
- package/dist/WorkerMetrics.d.ts +2 -1
- package/dist/WorkerMetrics.js +152 -93
- package/dist/WorkerRegistry.d.ts +6 -0
- package/dist/WorkerRegistry.js +70 -1
- package/dist/WorkerShutdown.d.ts +21 -0
- package/dist/WorkerShutdown.js +82 -9
- package/dist/WorkerShutdownDurableObject.d.ts +12 -0
- package/dist/WorkerShutdownDurableObject.js +41 -0
- package/dist/build-manifest.json +171 -99
- package/dist/createQueueWorker.d.ts +2 -0
- package/dist/createQueueWorker.js +42 -27
- package/dist/dashboard/types.d.ts +5 -0
- package/dist/dashboard/workers-api.js +136 -43
- package/dist/http/WorkerApiController.js +1 -0
- package/dist/http/WorkerController.js +133 -85
- package/dist/http/WorkerMonitoringService.d.ts +11 -0
- package/dist/http/WorkerMonitoringService.js +62 -0
- package/dist/http/middleware/CustomValidation.js +1 -1
- package/dist/http/middleware/EditWorkerValidation.d.ts +1 -1
- package/dist/http/middleware/EditWorkerValidation.js +7 -6
- package/dist/http/middleware/ProcessorPathSanitizer.js +101 -35
- package/dist/http/middleware/WorkerValidationChain.js +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/routes/workers.js +48 -6
- package/dist/storage/WorkerStore.d.ts +4 -1
- package/dist/storage/WorkerStore.js +55 -7
- package/dist/telemetry/api/TelemetryAPI.d.ts +46 -0
- package/dist/telemetry/api/TelemetryAPI.js +219 -0
- package/dist/telemetry/api/TelemetryMonitoringService.d.ts +17 -0
- package/dist/telemetry/api/TelemetryMonitoringService.js +113 -0
- package/dist/telemetry/components/AlertPanel.d.ts +1 -0
- package/dist/telemetry/components/AlertPanel.js +13 -0
- package/dist/telemetry/components/CostTracking.d.ts +1 -0
- package/dist/telemetry/components/CostTracking.js +14 -0
- package/dist/telemetry/components/ResourceUsageChart.d.ts +1 -0
- package/dist/telemetry/components/ResourceUsageChart.js +11 -0
- package/dist/telemetry/components/WorkerHealthChart.d.ts +1 -0
- package/dist/telemetry/components/WorkerHealthChart.js +11 -0
- package/dist/telemetry/index.d.ts +15 -0
- package/dist/telemetry/index.js +60 -0
- package/dist/telemetry/routes/dashboard.d.ts +6 -0
- package/dist/telemetry/routes/dashboard.js +608 -0
- package/dist/ui/router/EmbeddedAssets.d.ts +4 -0
- package/dist/ui/router/EmbeddedAssets.js +13 -0
- package/dist/ui/router/ui.js +100 -4
- package/package.json +10 -6
- package/src/AnomalyDetection.ts +9 -0
- package/src/CanaryController.ts +41 -5
- package/src/ChaosEngineering.ts +14 -0
- package/src/ClusterLock.ts +22 -9
- package/src/DeadLetterQueue.ts +13 -8
- package/src/MultiQueueWorker.ts +15 -8
- package/src/PriorityQueue.ts +21 -22
- package/src/ResourceMonitor.ts +72 -40
- package/src/WorkerFactory.ts +545 -49
- package/src/WorkerInit.ts +8 -3
- package/src/WorkerMetrics.ts +183 -105
- package/src/WorkerRegistry.ts +80 -1
- package/src/WorkerShutdown.ts +115 -9
- package/src/WorkerShutdownDurableObject.ts +64 -0
- package/src/createQueueWorker.ts +73 -30
- package/src/dashboard/types.ts +5 -0
- package/src/dashboard/workers-api.ts +165 -52
- package/src/http/WorkerApiController.ts +1 -0
- package/src/http/WorkerController.ts +167 -90
- package/src/http/WorkerMonitoringService.ts +77 -0
- package/src/http/middleware/CustomValidation.ts +1 -1
- package/src/http/middleware/EditWorkerValidation.ts +7 -6
- package/src/http/middleware/ProcessorPathSanitizer.ts +123 -36
- package/src/http/middleware/WorkerValidationChain.ts +1 -0
- package/src/index.ts +6 -1
- package/src/routes/workers.ts +66 -9
- package/src/storage/WorkerStore.ts +59 -9
- package/src/telemetry/api/TelemetryAPI.ts +292 -0
- package/src/telemetry/api/TelemetryMonitoringService.ts +149 -0
- package/src/telemetry/components/AlertPanel.ts +13 -0
- package/src/telemetry/components/CostTracking.ts +14 -0
- package/src/telemetry/components/ResourceUsageChart.ts +11 -0
- package/src/telemetry/components/WorkerHealthChart.ts +11 -0
- package/src/telemetry/index.ts +121 -0
- package/src/telemetry/public/assets/zintrust-logo.svg +15 -0
- package/src/telemetry/routes/dashboard.ts +638 -0
- package/src/telemetry/styles/tailwind.css +1 -0
- package/src/telemetry/styles/zintrust-theme.css +8 -0
- package/src/ui/router/EmbeddedAssets.ts +13 -0
- package/src/ui/router/ui.ts +112 -5
- package/src/ui/workers/index.html +2 -2
- package/src/ui/workers/main.js +232 -61
- package/src/ui/workers/zintrust.svg +30 -0
- package/dist/dashboard/workers-dashboard-ui.d.ts +0 -3
- package/dist/dashboard/workers-dashboard-ui.js +0 -1026
- package/dist/dashboard/workers-dashboard.d.ts +0 -4
- package/dist/dashboard/workers-dashboard.js +0 -904
package/src/ResourceMonitor.ts
CHANGED
|
@@ -88,6 +88,15 @@ const safeUptime = (): number => {
|
|
|
88
88
|
}
|
|
89
89
|
};
|
|
90
90
|
|
|
91
|
+
const safeProcessCpuUsage = (): NodeJS.CpuUsage => {
|
|
92
|
+
if (!process?.cpuUsage) return { user: 0, system: 0 };
|
|
93
|
+
try {
|
|
94
|
+
return process.cpuUsage();
|
|
95
|
+
} catch {
|
|
96
|
+
return { user: 0, system: 0 };
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
91
100
|
export type ResourceSnapshot = {
|
|
92
101
|
timestamp: Date;
|
|
93
102
|
cpu: {
|
|
@@ -182,6 +191,10 @@ let monitoringInterval: NodeJS.Timeout | null = null;
|
|
|
182
191
|
const resourceHistory = new Map<string, ResourceSnapshot[]>();
|
|
183
192
|
const alertHistory = new Map<string, ResourceAlert[]>();
|
|
184
193
|
|
|
194
|
+
// Memory management constants
|
|
195
|
+
const MAX_HISTORY_SIZE = 1000; // Keep last 1000 snapshots per worker
|
|
196
|
+
const MAX_ALERT_HISTORY = 100; // Keep last 100 alerts per worker
|
|
197
|
+
|
|
185
198
|
// Resource thresholds
|
|
186
199
|
const THRESHOLDS = {
|
|
187
200
|
cpu: { warning: 70, critical: 90 },
|
|
@@ -190,36 +203,64 @@ const THRESHOLDS = {
|
|
|
190
203
|
costPerHour: { warning: 10, critical: 50 },
|
|
191
204
|
};
|
|
192
205
|
|
|
193
|
-
//
|
|
194
|
-
let
|
|
195
|
-
|
|
206
|
+
// Store previous CPU measurements for calculating real-time usage
|
|
207
|
+
let previousCpuTimes: {
|
|
208
|
+
idle: number;
|
|
209
|
+
total: number;
|
|
210
|
+
timestamp: number;
|
|
211
|
+
} | null = null;
|
|
196
212
|
|
|
197
213
|
/**
|
|
198
|
-
* Helper: Calculate CPU usage percentage
|
|
214
|
+
* Helper: Calculate CPU usage percentage (real-time)
|
|
199
215
|
*/
|
|
200
216
|
const calculateCpuUsage = (): number => {
|
|
201
|
-
const
|
|
202
|
-
|
|
217
|
+
const os = getOsModule();
|
|
218
|
+
if (!os?.cpus) return 0;
|
|
203
219
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
220
|
+
try {
|
|
221
|
+
const cpus = os.cpus();
|
|
222
|
+
let totalIdle = 0;
|
|
223
|
+
let totalTick = 0;
|
|
224
|
+
|
|
225
|
+
cpus.forEach((cpu) => {
|
|
226
|
+
for (const type in cpu.times) {
|
|
227
|
+
totalTick += cpu.times[type as keyof typeof cpu.times];
|
|
228
|
+
}
|
|
229
|
+
totalIdle += cpu.times.idle;
|
|
230
|
+
});
|
|
209
231
|
|
|
210
|
-
|
|
211
|
-
const systemDelta = currentCpuUsage.system - previousCpuUsage.system;
|
|
212
|
-
const timeDelta = (currentTimestamp - previousCpuTimestamp) * 1000; // Convert ms to microseconds
|
|
232
|
+
const now = Date.now();
|
|
213
233
|
|
|
214
|
-
|
|
215
|
-
|
|
234
|
+
// If we have previous measurements, calculate real-time usage
|
|
235
|
+
if (previousCpuTimes) {
|
|
236
|
+
const timeDiff = now - previousCpuTimes.timestamp;
|
|
216
237
|
|
|
217
|
-
|
|
238
|
+
// Only calculate if enough time has passed (minimum 100ms)
|
|
239
|
+
if (timeDiff >= 100) {
|
|
240
|
+
const idleDiff = totalIdle - previousCpuTimes.idle;
|
|
241
|
+
const totalDiff = totalTick - previousCpuTimes.total;
|
|
218
242
|
|
|
219
|
-
|
|
220
|
-
|
|
243
|
+
// Calculate CPU usage percentage for this interval
|
|
244
|
+
const cpuPercentage = totalDiff > 0 ? ((totalDiff - idleDiff) / totalDiff) * 100 : 0;
|
|
221
245
|
|
|
222
|
-
|
|
246
|
+
// Update previous measurements
|
|
247
|
+
previousCpuTimes = { idle: totalIdle, total: totalTick, timestamp: now };
|
|
248
|
+
|
|
249
|
+
return Math.min(100, Math.max(0, cpuPercentage));
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// First measurement or not enough time passed
|
|
254
|
+
previousCpuTimes = { idle: totalIdle, total: totalTick, timestamp: now };
|
|
255
|
+
|
|
256
|
+
// For first call, return a reasonable estimate or use load average
|
|
257
|
+
const loadAvg = safeLoadAverage();
|
|
258
|
+
const cpuCount = safeCpuCount();
|
|
259
|
+
return Math.min(100, Math.max(0, (loadAvg[0] / cpuCount) * 100));
|
|
260
|
+
} catch (error) {
|
|
261
|
+
Logger.error('Failed to calculate system CPU usage', error as Error);
|
|
262
|
+
return 0;
|
|
263
|
+
}
|
|
223
264
|
};
|
|
224
265
|
|
|
225
266
|
/**
|
|
@@ -268,7 +309,7 @@ const captureSnapshot = (): ResourceSnapshot => {
|
|
|
268
309
|
pid: process.pid,
|
|
269
310
|
uptime: process.uptime(),
|
|
270
311
|
memoryUsage: process.memoryUsage(),
|
|
271
|
-
cpuUsage:
|
|
312
|
+
cpuUsage: safeProcessCpuUsage(),
|
|
272
313
|
},
|
|
273
314
|
};
|
|
274
315
|
};
|
|
@@ -439,17 +480,10 @@ const storeAlert = (alert: ResourceAlert): void => {
|
|
|
439
480
|
|
|
440
481
|
history.push(alert);
|
|
441
482
|
|
|
442
|
-
//
|
|
443
|
-
if (history.length >
|
|
444
|
-
history.
|
|
483
|
+
// Trim alert history to prevent memory leaks
|
|
484
|
+
if (history.length > MAX_ALERT_HISTORY) {
|
|
485
|
+
alertHistory.set(alert.workerName, history.slice(-MAX_ALERT_HISTORY));
|
|
445
486
|
}
|
|
446
|
-
|
|
447
|
-
Logger.warn(`Resource alert: ${alert.workerName}`, {
|
|
448
|
-
type: alert.alertType,
|
|
449
|
-
severity: alert.severity,
|
|
450
|
-
message: alert.message,
|
|
451
|
-
recommendation: alert.recommendation,
|
|
452
|
-
});
|
|
453
487
|
};
|
|
454
488
|
|
|
455
489
|
/**
|
|
@@ -500,7 +534,7 @@ const calculateTrend = (
|
|
|
500
534
|
|
|
501
535
|
// Simple linear regression for trend
|
|
502
536
|
const firstValue = values[0];
|
|
503
|
-
const lastValue = values.
|
|
537
|
+
const lastValue = values[values.length - 1] ?? values[0];
|
|
504
538
|
const changePercentage = ((lastValue - firstValue) / firstValue) * 100;
|
|
505
539
|
|
|
506
540
|
let trend: ResourceTrend['trend'];
|
|
@@ -566,10 +600,6 @@ export const ResourceMonitor = Object.freeze({
|
|
|
566
600
|
return;
|
|
567
601
|
}
|
|
568
602
|
|
|
569
|
-
// Initial snapshot
|
|
570
|
-
previousCpuUsage = process.cpuUsage();
|
|
571
|
-
previousCpuTimestamp = Date.now();
|
|
572
|
-
|
|
573
603
|
monitoringInterval = setInterval(() => {
|
|
574
604
|
const snapshot = captureSnapshot();
|
|
575
605
|
// Store snapshot for later analysis
|
|
@@ -611,14 +641,16 @@ export const ResourceMonitor = Object.freeze({
|
|
|
611
641
|
|
|
612
642
|
history.push(snapshot);
|
|
613
643
|
|
|
614
|
-
//
|
|
615
|
-
if (history.length >
|
|
616
|
-
history.
|
|
644
|
+
// Trim resource history to prevent memory leaks
|
|
645
|
+
if (history.length > MAX_HISTORY_SIZE) {
|
|
646
|
+
resourceHistory.set(workerName, history.slice(-MAX_HISTORY_SIZE));
|
|
617
647
|
}
|
|
618
648
|
|
|
619
649
|
// Check thresholds
|
|
620
650
|
const alerts = checkThresholds(workerName, snapshot, cost);
|
|
621
|
-
alerts.forEach(
|
|
651
|
+
alerts.forEach((element) => {
|
|
652
|
+
storeAlert(element);
|
|
653
|
+
});
|
|
622
654
|
|
|
623
655
|
return {
|
|
624
656
|
workerName,
|