fix web session metrics not working

This commit is contained in:
Holly Stubbs 2024-09-19 01:01:46 +01:00
parent fecd4ab892
commit a4f73d3e71
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
2 changed files with 42 additions and 28 deletions

View file

@ -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 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 SimpleProm from "simple-prom";
import Gauge from "simple-prom/lib/objects/Gauge"; import Gauge from "simple-prom/lib/objects/Gauge";
import Counter from "simple-prom/lib/objects/Counter"; 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<string, RemoteUser>();
new Database(Config.database.address, Config.database.port, Config.database.username, Config.database.password, Config.database.name);
const metrics = SimpleProm.init({ const metrics = SimpleProm.init({
selfHost: true, selfHost: true,
selfHostPort: Config.ports.metrics ?? 9100 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")); const onlineUsers = metrics.addMetric(new Gauge("multiprobe_open_connections"));
onlineUsers.setHelpText("Number of connections to the websocket"); 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")); const dataOut = metrics.addMetric(new Counter("multiprobe_data_out"));
dataOut.setHelpText("Data sent by the server in bytes"); 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<string, RemoteUser>();
new Database(Config.database.address, Config.database.port, Config.database.username, Config.database.password, Config.database.name);
// Web stuff // Web stuff
const fastify = Fastify({ const fastify = Fastify({

View file

@ -5,6 +5,8 @@ import SessionUser from "./SessionUser";
import { FastifyReply, FastifyRequest } from "fastify"; import { FastifyReply, FastifyRequest } from "fastify";
import User from "../entities/User"; import User from "../entities/User";
import { randomBytes } from "crypto"; import { randomBytes } from "crypto";
import Gauge from "simple-prom/lib/objects/Gauge";
import SimpleProm from "simple-prom";
type Cookies = { [cookieName: string]: string | undefined } type Cookies = { [cookieName: string]: string | undefined }
@ -18,8 +20,18 @@ export default abstract class Session {
Session.Sessions.remove(key); Session.Sessions.remove(key);
} }
} }
Session.WebSessionsGauge.Value = Session.Sessions.length;
}, 3600000); }, 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) { public static AssignUserSession(res:FastifyReply, user:User) {
const validPeriod = new Date(); const validPeriod = new Date();
validPeriod.setTime(validPeriod.getTime() + Config.session.validity); validPeriod.setTime(validPeriod.getTime() + Config.session.validity);
@ -31,6 +43,8 @@ export default abstract class Session {
path: "/", path: "/",
signed: true signed: true
}); });
Session.WebSessionsGauge.Value = Session.Sessions.length;
} }
public static Clear(cookies:Cookies, res:FastifyReply) { public static Clear(cookies:Cookies, res:FastifyReply) {
@ -39,6 +53,8 @@ export default abstract class Session {
Session.Sessions.remove(key as string); Session.Sessions.remove(key as string);
res.clearCookie("MP_SESSION"); res.clearCookie("MP_SESSION");
Session.WebSessionsGauge.Value = Session.Sessions.length;
} }
} }