From 984c6fcda7d31d0514721147fdf5ae5e38618d83 Mon Sep 17 00:00:00 2001 From: Nour Alharithi Date: Tue, 14 Nov 2023 09:54:35 -0800 Subject: [PATCH 1/4] adding prometheus gauge --- package.json | 1 + src/wsConnectionManager.ts | 16 ++++++++++++++++ yarn.lock | 25 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+) 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" From ee2073de08ba3851ef3694c58f96fef5ccbaaca8 Mon Sep 17 00:00:00 2001 From: GitHub Actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 14 Nov 2023 19:06:34 +0000 Subject: [PATCH 2/4] Bumping sdk dependency to 2.43.0-beta.16 --- package.json | 2 +- yarn.lock | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 864aa70..1926253 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "lib/index.js", "license": "Apache-2.0", "dependencies": { - "@drift-labs/sdk": "2.43.0-beta.15", + "@drift-labs/sdk": "2.43.0-beta.16", "@opentelemetry/api": "^1.1.0", "@opentelemetry/auto-instrumentations-node": "^0.31.1", "@opentelemetry/exporter-prometheus": "^0.31.0", diff --git a/yarn.lock b/yarn.lock index 41925b1..3e78291 100644 --- a/yarn.lock +++ b/yarn.lock @@ -80,10 +80,10 @@ enabled "2.0.x" kuler "^2.0.0" -"@drift-labs/sdk@2.43.0-beta.15": - version "2.43.0-beta.15" - resolved "https://registry.yarnpkg.com/@drift-labs/sdk/-/sdk-2.43.0-beta.15.tgz#60dba13d6dffff5a65c1ca807f1f60153e05e2bb" - integrity sha512-0dxf5w3Lga1CXAkVo3AKsvoatEiS7HV6YdVW1bhtyCO4sXNoR1sMVC0wlFIQL8nGpDNC8gpyWSyT6cOuZ3boqA== +"@drift-labs/sdk@2.43.0-beta.16": + version "2.43.0-beta.16" + resolved "https://registry.yarnpkg.com/@drift-labs/sdk/-/sdk-2.43.0-beta.16.tgz#ad2cad68e5bc60edc1f9aefce8d4a81adb3482fd" + integrity sha512-MH0PIbySuaQ1X1NWIrRWniFKnPWSNkSNTl1zgIqUB4c60eUGOMXXe/qVL58Uezx+sIjK/9KrUjVwRd3RLla2Bg== dependencies: "@coral-xyz/anchor" "0.28.1-beta.2" "@ellipsis-labs/phoenix-sdk" "^1.4.2" From 0a5c11713bfe84909116f3a6ef029fbf0e49b732 Mon Sep 17 00:00:00 2001 From: GitHub Actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 15 Nov 2023 17:15:44 +0000 Subject: [PATCH 3/4] Bumping sdk dependency to 2.43.0-beta.17 --- package.json | 2 +- yarn.lock | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 1926253..6759e75 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "lib/index.js", "license": "Apache-2.0", "dependencies": { - "@drift-labs/sdk": "2.43.0-beta.16", + "@drift-labs/sdk": "2.43.0-beta.17", "@opentelemetry/api": "^1.1.0", "@opentelemetry/auto-instrumentations-node": "^0.31.1", "@opentelemetry/exporter-prometheus": "^0.31.0", diff --git a/yarn.lock b/yarn.lock index 3e78291..6da178a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -80,10 +80,10 @@ enabled "2.0.x" kuler "^2.0.0" -"@drift-labs/sdk@2.43.0-beta.16": - version "2.43.0-beta.16" - resolved "https://registry.yarnpkg.com/@drift-labs/sdk/-/sdk-2.43.0-beta.16.tgz#ad2cad68e5bc60edc1f9aefce8d4a81adb3482fd" - integrity sha512-MH0PIbySuaQ1X1NWIrRWniFKnPWSNkSNTl1zgIqUB4c60eUGOMXXe/qVL58Uezx+sIjK/9KrUjVwRd3RLla2Bg== +"@drift-labs/sdk@2.43.0-beta.17": + version "2.43.0-beta.17" + resolved "https://registry.yarnpkg.com/@drift-labs/sdk/-/sdk-2.43.0-beta.17.tgz#179530e5e2324991d10f10a6fa1dde9de15924a2" + integrity sha512-auKQfEIAI1ElrwJ6kZMMOCDbNe8AE1SATtc3qftdn2e5TSDAFM1XkNmTAxnb+CvtRUjmiJkpHD2XN4d0GT1/ew== dependencies: "@coral-xyz/anchor" "0.28.1-beta.2" "@ellipsis-labs/phoenix-sdk" "^1.4.2" From c53f5d66727bcf96c504a2410e257cc4754c9a5c Mon Sep 17 00:00:00 2001 From: Nour Alharithi Date: Wed, 15 Nov 2023 11:05:17 -0800 Subject: [PATCH 4/4] remove double decrement counters --- src/wsConnectionManager.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/wsConnectionManager.ts b/src/wsConnectionManager.ts index e6adf28..7be6d1e 100644 --- a/src/wsConnectionManager.ts +++ b/src/wsConnectionManager.ts @@ -155,8 +155,6 @@ async function main() { // Handle disconnection ws.on('close', () => { - wsConnectionsGauge.dec(); - // Clear any existing intervals and timeouts clearInterval(pingIntervalId); clearTimeout(pongTimeoutId); @@ -167,16 +165,11 @@ async function main() { subscribedChannels.delete(channel); } }); - }); - - ws.on('disconnect', () => { - console.log('Client disconnected'); wsConnectionsGauge.dec(); }); ws.on('error', (error) => { console.error('Socket error:', error); - wsConnectionsGauge.dec(); }); });