Skip to content

Commit 4ced97b

Browse files
committed
chore: Enable/disable metric collection based on flag
1 parent 257ff5d commit 4ced97b

File tree

1 file changed

+66
-47
lines changed
  • apps/agent-connector/src/app

1 file changed

+66
-47
lines changed

apps/agent-connector/src/app/app.ts

+66-47
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import fs from 'fs'
1212
import path from 'path'
1313
import os from 'os'
1414

15+
// Flag to enable/disable metric collection
16+
const ENABLE_METRICS = true
17+
1518
// Metrics tracking
1619
const agentMetrics = new Map()
1720

@@ -104,6 +107,7 @@ function createTextStreamGenerator(): TextStreamGenerator {
104107
}
105108

106109
function saveMetrics(metricKey: string, metrics: any) {
110+
if (!ENABLE_METRICS) return
107111
const fileName = `metrics-${metricKey}.json`
108112
const directoryPath = path.join(__dirname, './metrics')
109113
const filePath = path.join(directoryPath, fileName)
@@ -127,6 +131,7 @@ function saveMetrics(metricKey: string, metrics: any) {
127131
}
128132

129133
function collectMetrics(metricKey: string) {
134+
if (!ENABLE_METRICS) return
130135
const currentMemory = getMemoryUsage()
131136
const currentCPU = process.cpuUsage()
132137
const timestamp = Date.now()
@@ -183,6 +188,7 @@ function collectMetrics(metricKey: string) {
183188
}
184189

185190
function clearMetricsFiles() {
191+
if (!ENABLE_METRICS) return
186192
const directoryPath = path.join(__dirname, './metrics') // Correct path to the directory
187193

188194
// Check if the directory exists, if not create it
@@ -199,15 +205,17 @@ function clearMetricsFiles() {
199205

200206
// Function to save all metrics
201207
function saveAllMetrics() {
208+
if (!ENABLE_METRICS) return
202209
for (const [key, metrics] of agentMetrics.entries()) {
203210
saveMetrics(key, metrics)
204211
}
205212
}
206213

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+
}
211219

212220
const agentApp = await initApp()
213221

@@ -238,13 +246,47 @@ export async function app(fastify: FastifyInstance) {
238246
return
239247
}
240248

249+
let metricInterval: NodeJS.Timeout | null = null
241250
// Initialize metrics for this agent
242251
const metricKey = `${agentId}-${channelId}`
243252

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+
}
248290

249291
// Initialize connection management
250292
const pingInterval = setInterval(() => {
@@ -270,34 +312,6 @@ export async function app(fastify: FastifyInstance) {
270312

271313
const agent = new Agent(agentData, agentApp.get('pubsub'), agentApp)
272314

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-
301315
let currentSpeechStream: AsyncIterable<Uint8Array> | null = null
302316
let isSpeechStreamRunning = false // Flag to track if a stream is in progress
303317
let voiceId = 'Z7HNXT9nFlPyYvAISoB6'
@@ -360,7 +374,7 @@ export async function app(fastify: FastifyInstance) {
360374
const { type } = data
361375

362376
if (type === 'message') {
363-
agentMetrics.get(metricKey).messageCount++
377+
if (ENABLE_METRICS) agentMetrics.get(metricKey).messageCount++
364378
socket.send(
365379
JSON.stringify({
366380
id: new Date().toISOString(),
@@ -493,22 +507,27 @@ export async function app(fastify: FastifyInstance) {
493507
socket.on('close', () => {
494508
agent.removeAllListeners()
495509
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)
500512

501-
agentMetrics.delete(metricKey)
513+
if (ENABLE_METRICS) {
514+
saveMetrics(metricKey, agentMetrics.get(metricKey))
515+
516+
agentMetrics.delete(metricKey)
517+
}
502518
})
503519

504520
socket.on('error', () => {
505521
agent.removeAllListeners()
506522
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+
}
512531
})
513532
}
514533
)

0 commit comments

Comments
 (0)