Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prometheus scaling gauge #18

Merged
merged 5 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.17",
"@opentelemetry/api": "^1.1.0",
"@opentelemetry/auto-instrumentations-node": "^0.31.1",
"@opentelemetry/exporter-prometheus": "^0.31.0",
Expand All @@ -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",
Expand Down
17 changes: 13 additions & 4 deletions src/wsConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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' });

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -158,10 +165,7 @@ async function main() {
subscribedChannels.delete(channel);
}
});
});

ws.on('disconnect', () => {
console.log('Client disconnected');
wsConnectionsGauge.dec();
});

ws.on('error', (error) => {
Expand All @@ -173,6 +177,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);
});
Expand Down
33 changes: 29 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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.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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
Loading