From a4f73d3e71ae5c7fe37e275014cff02108391315 Mon Sep 17 00:00:00 2001 From: Holly Date: Thu, 19 Sep 2024 01:01:46 +0100 Subject: [PATCH] fix web session metrics not working --- server/index.ts | 54 +++++++++++++++++++-------------------- server/objects/Session.ts | 16 ++++++++++++ 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/server/index.ts b/server/index.ts index 49999c5..f3af797 100644 --- a/server/index.ts +++ b/server/index.ts @@ -1,41 +1,13 @@ -import { createReader, createWriter, Endian } from "bufferstuff"; -import { WebSocketServer } from "ws"; -import Fastify from "fastify"; -import FastifyFormBody from "@fastify/formbody"; -import FastifyCookie from "@fastify/cookie"; -import FastifyView from "@fastify/view"; -import EJS from "ejs"; import Config from "./objects/Config"; -import FunkyArray from "./objects/FunkyArray"; -import RemoteUser from "./objects/RemoteUser"; -import { MessageType } from "./enums/MessageType"; -import Database from "./objects/Database"; -import { Console } from "hsconsole"; -import UserService from "./services/UserService"; -import Party from "./entities/Party"; import SimpleProm from "simple-prom"; import Gauge from "simple-prom/lib/objects/Gauge"; import Counter from "simple-prom/lib/objects/Counter"; -import Controller from "./controller/Controller"; -import HomeController from "./controller/HomeController"; -import AccountController from "./controller/AccountController"; -import PartyController from "./controller/PartyController"; -import ApiController from "./controller/ApiController"; - -Console.customHeader(`MultiProbe server started at ${new Date()}`); - -const users = new FunkyArray(); - -new Database(Config.database.address, Config.database.port, Config.database.username, Config.database.password, Config.database.name); const metrics = SimpleProm.init({ selfHost: true, selfHostPort: Config.ports.metrics ?? 9100 }); -const webSessions = metrics.addMetric(new Gauge("multiprobe_web_sessions")); -webSessions.setHelpText("Number of valid web sessions"); - const onlineUsers = metrics.addMetric(new Gauge("multiprobe_open_connections")); onlineUsers.setHelpText("Number of connections to the websocket"); @@ -48,6 +20,32 @@ dataIn.setHelpText("Data received by the server in bytes"); const dataOut = metrics.addMetric(new Counter("multiprobe_data_out")); dataOut.setHelpText("Data sent by the server in bytes"); +import { createReader, createWriter, Endian } from "bufferstuff"; +import { WebSocketServer } from "ws"; +import Fastify from "fastify"; +import FastifyFormBody from "@fastify/formbody"; +import FastifyCookie from "@fastify/cookie"; +import FastifyView from "@fastify/view"; +import EJS from "ejs"; +import FunkyArray from "./objects/FunkyArray"; +import RemoteUser from "./objects/RemoteUser"; +import { MessageType } from "./enums/MessageType"; +import Database from "./objects/Database"; +import { Console } from "hsconsole"; +import UserService from "./services/UserService"; +import Party from "./entities/Party"; +import Controller from "./controller/Controller"; +import HomeController from "./controller/HomeController"; +import AccountController from "./controller/AccountController"; +import PartyController from "./controller/PartyController"; +import ApiController from "./controller/ApiController"; + +Console.customHeader(`MultiProbe server started at ${new Date()}`); + +const users = new FunkyArray(); + +new Database(Config.database.address, Config.database.port, Config.database.username, Config.database.password, Config.database.name); + // Web stuff const fastify = Fastify({ diff --git a/server/objects/Session.ts b/server/objects/Session.ts index 1ad208e..371135c 100644 --- a/server/objects/Session.ts +++ b/server/objects/Session.ts @@ -5,6 +5,8 @@ import SessionUser from "./SessionUser"; import { FastifyReply, FastifyRequest } from "fastify"; import User from "../entities/User"; import { randomBytes } from "crypto"; +import Gauge from "simple-prom/lib/objects/Gauge"; +import SimpleProm from "simple-prom"; type Cookies = { [cookieName: string]: string | undefined } @@ -18,8 +20,18 @@ export default abstract class Session { Session.Sessions.remove(key); } } + Session.WebSessionsGauge.Value = Session.Sessions.length; }, 3600000); + private static WebSessionsGauge = (() => { + if (!SimpleProm.instance) { + throw "SimpleProm not initialised"; + } + const webSessions = SimpleProm.instance.addMetric(new Gauge("multiprobe_web_sessions")); + webSessions?.setHelpText("Number of valid web sessions"); + return webSessions; + })(); + public static AssignUserSession(res:FastifyReply, user:User) { const validPeriod = new Date(); validPeriod.setTime(validPeriod.getTime() + Config.session.validity); @@ -31,6 +43,8 @@ export default abstract class Session { path: "/", signed: true }); + + Session.WebSessionsGauge.Value = Session.Sessions.length; } public static Clear(cookies:Cookies, res:FastifyReply) { @@ -39,6 +53,8 @@ export default abstract class Session { Session.Sessions.remove(key as string); res.clearCookie("MP_SESSION"); + + Session.WebSessionsGauge.Value = Session.Sessions.length; } }