mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-12-04 06:21:59 +09:00
fix(monitoring): isolate request completion intervals
Signed-off-by: Rudy Celekli <rudy@gradiahq.com>
This commit is contained in:
2 files changed
+20
No files matched your search
@@ -0,0 +1,14 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { MonitoringHooks } from '../v3/@claude-flow/cli/src/production/monitoring.ts';
|
||||
test('request completion is idempotent', () => {
|
||||
const m = new MonitoringHooks(); const end = m.startRequest('one'); end(); end();
|
||||
assert.equal(m.getPerformanceMetrics().activeRequests, 0);
|
||||
assert.equal(m.getMetrics('response_time_ms').length, 1);
|
||||
});
|
||||
test('completion from before reset cannot corrupt the new monitoring interval', () => {
|
||||
const m = new MonitoringHooks(); const oldEnd = m.startRequest('old'); m.reset(); const end = m.startRequest('new'); oldEnd();
|
||||
assert.equal(m.getPerformanceMetrics().activeRequests, 1);
|
||||
assert.equal(m.getMetrics('response_time_ms').length, 0);
|
||||
end(); assert.equal(m.getPerformanceMetrics().activeRequests, 0);
|
||||
});
|
||||
@@ -110,6 +110,7 @@ export class MonitoringHooks {
|
||||
private healthStatus: HealthStatus;
|
||||
private startTime = Date.now();
|
||||
private activeRequests = 0;
|
||||
private requestGeneration = 0;
|
||||
private requestCount = 0;
|
||||
private errorCount = 0;
|
||||
private healthChecks: Map<string, () => Promise<{ healthy: boolean; message?: string }>> = new Map();
|
||||
@@ -191,10 +192,14 @@ export class MonitoringHooks {
|
||||
this.requestCount++;
|
||||
const startTime = Date.now();
|
||||
|
||||
const generation = this.requestGeneration;
|
||||
let completed = false;
|
||||
this.counter('request_started', 1, { requestId });
|
||||
|
||||
// Return end function
|
||||
return () => {
|
||||
if (completed || generation !== this.requestGeneration) return;
|
||||
completed = true;
|
||||
this.activeRequests--;
|
||||
const duration = Date.now() - startTime;
|
||||
this.responseTimes.push(duration);
|
||||
@@ -394,6 +399,7 @@ export class MonitoringHooks {
|
||||
* Reset all metrics
|
||||
*/
|
||||
reset(): void {
|
||||
this.requestGeneration++;
|
||||
this.metrics = [];
|
||||
this.responseTimes = [];
|
||||
this.alerts = [];
|
||||
|
||||
Reference in new issue
Block a user