From 6582760051fcdc73da27a33d7c401707eae60581 Mon Sep 17 00:00:00 2001 From: RazvanP Date: Thu, 15 Sep 2022 10:42:27 +0300 Subject: [PATCH] Created a Prometheus metrics adapter --- package-lock.json | 66 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + src/index.ts | 6 +++++ 3 files changed, 73 insertions(+) diff --git a/package-lock.json b/package-lock.json index 0182d55..1febe58 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37,6 +37,7 @@ "eslint-plugin-jsdoc": "37.0.3", "express": "4.17.1", "socket.io": "^2.5.0", + "socket.io-prometheus-metrics": "^1.0.6", "ts-node-dev": "^1.1.8", "typescript": "4.2.3" }, @@ -2924,6 +2925,11 @@ "node": ">=8" } }, + "node_modules/bintrees": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/bintrees/-/bintrees-1.0.2.tgz", + "integrity": "sha512-VOMgTMwjAaUG580SXn3LacVgjurrbMme7ZZNYGSSV7mmtY6QQRh0Eg3pwIcntQ77DErK1L0NxkbetjcoXzVwKw==" + }, "node_modules/blob": { "version": "0.0.5", "resolved": "https://registry.npmjs.org/blob/-/blob-0.0.5.tgz", @@ -5710,6 +5716,17 @@ "node": ">=0.4.0" } }, + "node_modules/prom-client": { + "version": "11.5.3", + "resolved": "https://registry.npmjs.org/prom-client/-/prom-client-11.5.3.tgz", + "integrity": "sha512-iz22FmTbtkyL2vt0MdDFY+kWof+S9UB/NACxSn2aJcewtw+EERsen0urSkZ2WrHseNdydsvcxCTAnPcSMZZv4Q==", + "dependencies": { + "tdigest": "^0.1.1" + }, + "engines": { + "node": ">=6.1" + } + }, "node_modules/proxy-addr": { "version": "2.0.7", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", @@ -6197,6 +6214,16 @@ "ms": "^2.1.1" } }, + "node_modules/socket.io-prometheus-metrics": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/socket.io-prometheus-metrics/-/socket.io-prometheus-metrics-1.0.6.tgz", + "integrity": "sha512-rG1XIWuFRysVE/VE2ikJcYbv1ZqcY5IZ5CJOoih2MMiVx7GujwvxF1olcpDlQ/iGGryCXC/FRoBAB00Qnzsntw==", + "dependencies": { + "express": "^4.16.4", + "prom-client": "^11.2.1", + "socket.io": "^2.2.0" + } + }, "node_modules/socket.io/node_modules/debug": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", @@ -6345,6 +6372,14 @@ "node": ">=6" } }, + "node_modules/tdigest": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/tdigest/-/tdigest-0.1.2.tgz", + "integrity": "sha512-+G0LLgjjo9BZX2MfdvPfH+MKLCrxlXSYec5DaPYP1fe6Iyhf0/fSmJ0bFiZ1F8BT6cGXl2LpltQptzjXKWEkKA==", + "dependencies": { + "bintrees": "1.0.2" + } + }, "node_modules/terser": { "version": "5.15.0", "resolved": "https://registry.npmjs.org/terser/-/terser-5.15.0.tgz", @@ -8964,6 +8999,11 @@ "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", "integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==" }, + "bintrees": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/bintrees/-/bintrees-1.0.2.tgz", + "integrity": "sha512-VOMgTMwjAaUG580SXn3LacVgjurrbMme7ZZNYGSSV7mmtY6QQRh0Eg3pwIcntQ77DErK1L0NxkbetjcoXzVwKw==" + }, "blob": { "version": "0.0.5", "resolved": "https://registry.npmjs.org/blob/-/blob-0.0.5.tgz", @@ -11005,6 +11045,14 @@ "resolved": "https://registry.npmjs.org/progress/-/progress-2.0.3.tgz", "integrity": "sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA==" }, + "prom-client": { + "version": "11.5.3", + "resolved": "https://registry.npmjs.org/prom-client/-/prom-client-11.5.3.tgz", + "integrity": "sha512-iz22FmTbtkyL2vt0MdDFY+kWof+S9UB/NACxSn2aJcewtw+EERsen0urSkZ2WrHseNdydsvcxCTAnPcSMZZv4Q==", + "requires": { + "tdigest": "^0.1.1" + } + }, "proxy-addr": { "version": "2.0.7", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", @@ -11391,6 +11439,16 @@ } } }, + "socket.io-prometheus-metrics": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/socket.io-prometheus-metrics/-/socket.io-prometheus-metrics-1.0.6.tgz", + "integrity": "sha512-rG1XIWuFRysVE/VE2ikJcYbv1ZqcY5IZ5CJOoih2MMiVx7GujwvxF1olcpDlQ/iGGryCXC/FRoBAB00Qnzsntw==", + "requires": { + "express": "^4.16.4", + "prom-client": "^11.2.1", + "socket.io": "^2.2.0" + } + }, "source-map": { "version": "0.5.7", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.5.7.tgz", @@ -11493,6 +11551,14 @@ "integrity": "sha512-GNzQvQTOIP6RyTfE2Qxb8ZVlNmw0n88vp1szwWRimP02mnTsx3Wtn5qRdqY9w2XduFNUgvOwhNnQsjwCp+kqaQ==", "peer": true }, + "tdigest": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/tdigest/-/tdigest-0.1.2.tgz", + "integrity": "sha512-+G0LLgjjo9BZX2MfdvPfH+MKLCrxlXSYec5DaPYP1fe6Iyhf0/fSmJ0bFiZ1F8BT6cGXl2LpltQptzjXKWEkKA==", + "requires": { + "bintrees": "1.0.2" + } + }, "terser": { "version": "5.15.0", "resolved": "https://registry.npmjs.org/terser/-/terser-5.15.0.tgz", diff --git a/package.json b/package.json index dbfca21..bcef018 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "eslint-plugin-jsdoc": "37.0.3", "express": "4.17.1", "socket.io": "^2.5.0", + "socket.io-prometheus-metrics": "^1.0.6", "ts-node-dev": "^1.1.8", "typescript": "4.2.3" }, diff --git a/src/index.ts b/src/index.ts index 0134284..998beb4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ import debug from "debug"; import express from "express"; import http from "http"; import socketIO from "socket.io"; +import * as prometheus from "socket.io-prometheus-metrics"; const serverDebug = debug("server"); const ioDebug = debug("io"); @@ -41,6 +42,11 @@ const io = socketIO(server, { }, }); +// listens on host:9090/metrics +prometheus.metrics(io, { + collectDefaultMetrics: true +}); + io.on("connection", (socket) => { ioDebug("connection established!"); io.to(`${socket.id}`).emit("init-room");