diff --git a/package.json b/package.json index 86d9fed..864aa70 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "express-rate-limit": "^6.7.0", "ioredis": "^5.3.2", "morgan": "^1.10.0", + "prom-client": "^15.0.0", "redis": "^4.6.10", "response-time": "^2.3.2", "socket.io-redis": "^6.1.1", diff --git a/src/wsConnectionManager.ts b/src/wsConnectionManager.ts index 60928ea..e6adf28 100644 --- a/src/wsConnectionManager.ts +++ b/src/wsConnectionManager.ts @@ -5,6 +5,7 @@ import compression from 'compression'; import { WebSocket, WebSocketServer } from 'ws'; import { sleep } from './utils/utils'; import { RedisClient } from './utils/redisClient'; +import { register, Gauge } from 'prom-client'; // Set up env constants require('dotenv').config(); @@ -14,6 +15,11 @@ app.use(cors({ origin: '*' })); app.use(compression()); app.set('trust proxy', 1); +const wsConnectionsGauge = new Gauge({ + name: 'websocket_connections', + help: 'Number of active WebSocket connections', +}); + const server = http.createServer(app); const wss = new WebSocketServer({ server, path: '/ws' }); @@ -65,6 +71,7 @@ async function main() { wss.on('connection', (ws: WebSocket) => { console.log('Client connected'); + wsConnectionsGauge.inc(); ws.on('message', async (msg) => { let parsedMessage: any; @@ -148,6 +155,8 @@ async function main() { // Handle disconnection ws.on('close', () => { + wsConnectionsGauge.dec(); + // Clear any existing intervals and timeouts clearInterval(pingIntervalId); clearTimeout(pongTimeoutId); @@ -162,10 +171,12 @@ async function main() { ws.on('disconnect', () => { console.log('Client disconnected'); + wsConnectionsGauge.dec(); }); ws.on('error', (error) => { console.error('Socket error:', error); + wsConnectionsGauge.dec(); }); }); @@ -173,6 +184,11 @@ async function main() { console.log(`connection manager running on ${WS_PORT}`); }); + app.get('/metrics', async (req, res) => { + res.set('Content-Type', register.contentType); + res.end(await register.metrics()); + }); + server.on('error', (error) => { console.error('Server error:', error); }); diff --git a/yarn.lock b/yarn.lock index 267aeae..41925b1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -357,6 +357,11 @@ resolved "https://registry.yarnpkg.com/@opentelemetry/api/-/api-1.3.0.tgz#27c6f776ac3c1c616651e506a89f438a0ed6a055" integrity sha512-YveTnGNsFFixTKJz09Oi4zYkiLT5af3WpZDu4aIUM7xX+2bHAkOJayFTVQd6zB8kkWPpbua4Ha6Ql00grdLlJQ== +"@opentelemetry/api@^1.4.0": + version "1.7.0" + resolved "https://registry.yarnpkg.com/@opentelemetry/api/-/api-1.7.0.tgz#b139c81999c23e3c8d3c0a7234480e945920fc40" + integrity sha512-AdY5wvN0P2vXBi3b29hxZgSFvdhdxPB9+f0B6s//P9Q8nibRWeA3cHm8UmLpio9ABigkVHJ5NMPk+Mz8VCCyrw== + "@opentelemetry/auto-instrumentations-node@^0.31.1": version "0.31.2" resolved "https://registry.yarnpkg.com/@opentelemetry/auto-instrumentations-node/-/auto-instrumentations-node-0.31.2.tgz#56984f2c2def73fced692c36431b9010a97f475f" @@ -1699,6 +1704,11 @@ bindings@^1.3.0: dependencies: file-uri-to-path "1.0.0" +bintrees@1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/bintrees/-/bintrees-1.0.2.tgz#49f896d6e858a4a499df85c38fb399b9aff840f8" + integrity sha512-VOMgTMwjAaUG580SXn3LacVgjurrbMme7ZZNYGSSV7mmtY6QQRh0Eg3pwIcntQ77DErK1L0NxkbetjcoXzVwKw== + bn.js@^5.0.0, bn.js@^5.1.0, bn.js@^5.1.2, bn.js@^5.2.0, bn.js@^5.2.1: version "5.2.1" resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.2.1.tgz#0bc527a6a0d18d0aa8d5b0538ce4a77dccfa7b70" @@ -3373,6 +3383,14 @@ progress@^2.0.0: resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8" integrity sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA== +prom-client@^15.0.0: + version "15.0.0" + resolved "https://registry.yarnpkg.com/prom-client/-/prom-client-15.0.0.tgz#067da874a2aa5d2e21bd5cdba9f24a8178bdab6a" + integrity sha512-UocpgIrKyA2TKLVZDSfm8rGkL13C19YrQBAiG3xo3aDFWcHedxRxI3z+cIcucoxpSO0h5lff5iv/SXoxyeopeA== + dependencies: + "@opentelemetry/api" "^1.4.0" + tdigest "^0.1.1" + proxy-addr@^2.0.7, proxy-addr@~2.0.7: version "2.0.7" resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-2.0.7.tgz#f19fe69ceab311eeb94b42e70e8c2070f9ba1025" @@ -3864,6 +3882,13 @@ table@^6.0.9: string-width "^4.2.3" strip-ansi "^6.0.1" +tdigest@^0.1.1: + version "0.1.2" + resolved "https://registry.yarnpkg.com/tdigest/-/tdigest-0.1.2.tgz#96c64bac4ff10746b910b0e23b515794e12faced" + integrity sha512-+G0LLgjjo9BZX2MfdvPfH+MKLCrxlXSYec5DaPYP1fe6Iyhf0/fSmJ0bFiZ1F8BT6cGXl2LpltQptzjXKWEkKA== + dependencies: + bintrees "1.0.2" + text-encoding-utf-8@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/text-encoding-utf-8/-/text-encoding-utf-8-1.0.2.tgz#585b62197b0ae437e3c7b5d0af27ac1021e10d13"