@@ -12,6 +12,9 @@ import fs from 'fs'
12
12
import path from 'path'
13
13
import os from 'os'
14
14
15
+ // Flag to enable/disable metric collection
16
+ const ENABLE_METRICS = true
17
+
15
18
// Metrics tracking
16
19
const agentMetrics = new Map ( )
17
20
@@ -104,6 +107,7 @@ function createTextStreamGenerator(): TextStreamGenerator {
104
107
}
105
108
106
109
function saveMetrics ( metricKey : string , metrics : any ) {
110
+ if ( ! ENABLE_METRICS ) return
107
111
const fileName = `metrics-${ metricKey } .json`
108
112
const directoryPath = path . join ( __dirname , './metrics' )
109
113
const filePath = path . join ( directoryPath , fileName )
@@ -127,6 +131,7 @@ function saveMetrics(metricKey: string, metrics: any) {
127
131
}
128
132
129
133
function collectMetrics ( metricKey : string ) {
134
+ if ( ! ENABLE_METRICS ) return
130
135
const currentMemory = getMemoryUsage ( )
131
136
const currentCPU = process . cpuUsage ( )
132
137
const timestamp = Date . now ( )
@@ -183,6 +188,7 @@ function collectMetrics(metricKey: string) {
183
188
}
184
189
185
190
function clearMetricsFiles ( ) {
191
+ if ( ! ENABLE_METRICS ) return
186
192
const directoryPath = path . join ( __dirname , './metrics' ) // Correct path to the directory
187
193
188
194
// Check if the directory exists, if not create it
@@ -199,15 +205,17 @@ function clearMetricsFiles() {
199
205
200
206
// Function to save all metrics
201
207
function saveAllMetrics ( ) {
208
+ if ( ! ENABLE_METRICS ) return
202
209
for ( const [ key , metrics ] of agentMetrics . entries ( ) ) {
203
210
saveMetrics ( key , metrics )
204
211
}
205
212
}
206
213
207
- setInterval ( saveAllMetrics , 30000 ) // Save all metrics every 30 seconds
208
-
209
- // clear all previous metrics
210
- clearMetricsFiles ( )
214
+ if ( ENABLE_METRICS ) {
215
+ setInterval ( saveAllMetrics , 30000 ) // Save all metrics every 30 seconds
216
+ // clear all previous metrics
217
+ clearMetricsFiles ( )
218
+ }
211
219
212
220
const agentApp = await initApp ( )
213
221
@@ -238,13 +246,47 @@ export async function app(fastify: FastifyInstance) {
238
246
return
239
247
}
240
248
249
+ let metricInterval : NodeJS . Timeout | null = null
241
250
// Initialize metrics for this agent
242
251
const metricKey = `${ agentId } -${ channelId } `
243
252
244
- agentMetrics . set ( metricKey , {
245
- messageCount : 0 ,
246
- metricsOverTime : [ ] ,
247
- } )
253
+ if ( ENABLE_METRICS ) {
254
+ agentMetrics . set ( metricKey , {
255
+ messageCount : 0 ,
256
+ metricsOverTime : [ ] ,
257
+ } )
258
+
259
+ // Record initial metrics
260
+ const initialMetrics = collectMetrics ( metricKey )
261
+
262
+ agentMetrics . get ( metricKey ) . metricsOverTime = [ initialMetrics ]
263
+
264
+ // Optionally, you can log the initial metrics
265
+ console . log ( 'Initial metrics recorded:' , initialMetrics )
266
+
267
+ // Then, update your metricInterval:
268
+ metricInterval = setInterval ( ( ) => {
269
+ if ( ! ENABLE_METRICS ) return
270
+ const currentMetrics = agentMetrics . get ( metricKey )
271
+
272
+ // Collect new metrics
273
+ const newMetrics = collectMetrics ( metricKey )
274
+
275
+ if ( ! newMetrics ) return
276
+
277
+ // Add to metricsOverTime array
278
+ currentMetrics . metricsOverTime . push ( newMetrics )
279
+
280
+ // Update message count
281
+ currentMetrics . messageCount = newMetrics . messageCount
282
+
283
+ // Save metrics every 10 seconds
284
+ if ( Date . now ( ) - currentMetrics . lastSaveTime > 10000 ) {
285
+ saveMetrics ( metricKey , currentMetrics )
286
+ currentMetrics . lastSaveTime = Date . now ( )
287
+ }
288
+ } , 1000 ) // Collect metrics every second
289
+ }
248
290
249
291
// Initialize connection management
250
292
const pingInterval = setInterval ( ( ) => {
@@ -270,34 +312,6 @@ export async function app(fastify: FastifyInstance) {
270
312
271
313
const agent = new Agent ( agentData , agentApp . get ( 'pubsub' ) , agentApp )
272
314
273
- // Record initial metrics
274
- const initialMetrics = collectMetrics ( metricKey )
275
-
276
- agentMetrics . get ( metricKey ) . metricsOverTime = [ initialMetrics ]
277
-
278
- // Optionally, you can log the initial metrics
279
- console . log ( 'Initial metrics recorded:' , initialMetrics )
280
-
281
- // Then, update your metricInterval:
282
- const metricInterval = setInterval ( ( ) => {
283
- const currentMetrics = agentMetrics . get ( metricKey )
284
-
285
- // Collect new metrics
286
- const newMetrics = collectMetrics ( metricKey )
287
-
288
- // Add to metricsOverTime array
289
- currentMetrics . metricsOverTime . push ( newMetrics )
290
-
291
- // Update message count
292
- currentMetrics . messageCount = newMetrics . messageCount
293
-
294
- // Save metrics every 10 seconds
295
- if ( Date . now ( ) - currentMetrics . lastSaveTime > 10000 ) {
296
- saveMetrics ( metricKey , currentMetrics )
297
- currentMetrics . lastSaveTime = Date . now ( )
298
- }
299
- } , 1000 ) // Collect metrics every second
300
-
301
315
let currentSpeechStream : AsyncIterable < Uint8Array > | null = null
302
316
let isSpeechStreamRunning = false // Flag to track if a stream is in progress
303
317
let voiceId = 'Z7HNXT9nFlPyYvAISoB6'
@@ -360,7 +374,7 @@ export async function app(fastify: FastifyInstance) {
360
374
const { type } = data
361
375
362
376
if ( type === 'message' ) {
363
- agentMetrics . get ( metricKey ) . messageCount ++
377
+ if ( ENABLE_METRICS ) agentMetrics . get ( metricKey ) . messageCount ++
364
378
socket . send (
365
379
JSON . stringify ( {
366
380
id : new Date ( ) . toISOString ( ) ,
@@ -493,22 +507,27 @@ export async function app(fastify: FastifyInstance) {
493
507
socket . on ( 'close' , ( ) => {
494
508
agent . removeAllListeners ( )
495
509
agent . onDestroy ( )
496
- clearInterval ( pingInterval ) // Clear the interval on connection close
497
- clearInterval ( metricInterval ) // Clear the interval on connection close
498
- // Save metrics to file
499
- saveMetrics ( metricKey , agentMetrics . get ( metricKey ) )
510
+ if ( pingInterval ) clearInterval ( pingInterval )
511
+ if ( metricInterval ) clearInterval ( metricInterval )
500
512
501
- agentMetrics . delete ( metricKey )
513
+ if ( ENABLE_METRICS ) {
514
+ saveMetrics ( metricKey , agentMetrics . get ( metricKey ) )
515
+
516
+ agentMetrics . delete ( metricKey )
517
+ }
502
518
} )
503
519
504
520
socket . on ( 'error' , ( ) => {
505
521
agent . removeAllListeners ( )
506
522
agent . onDestroy ( )
507
- clearInterval ( pingInterval ) // Clear the interval on connection close
508
- clearInterval ( metricInterval ) // Clear the interval on connection close
509
- // Save metrics to file
510
- saveMetrics ( metricKey , agentMetrics . get ( metricKey ) )
511
- agentMetrics . delete ( metricKey )
523
+ if ( pingInterval ) clearInterval ( pingInterval )
524
+ if ( metricInterval ) clearInterval ( metricInterval )
525
+
526
+ if ( ENABLE_METRICS ) {
527
+ saveMetrics ( metricKey , agentMetrics . get ( metricKey ) )
528
+
529
+ agentMetrics . delete ( metricKey )
530
+ }
512
531
} )
513
532
}
514
533
)
0 commit comments