fix prometheus metric and add total users and active pad count (#7179)

* fix prometheus metric registration

* add totalUsers and activePads metric to prometheus
This commit is contained in:
Phillip 2025-10-19 17:27:21 +02:00 committed by GitHub
parent 49d431b6b8
commit 06f67b4c01
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 18 deletions

View file

@ -94,14 +94,25 @@ exports.socketio = () => {
const sessioninfos:MapArrayType<any> = {};
exports.sessioninfos = sessioninfos;
stats.gauge('totalUsers', () => socketio ? socketio.engine.clientsCount : 0);
stats.gauge('activePads', () => {
function getTotalActiveUsers() {
return socketio ? socketio.engine.clientsCount : 0;
}
exports.getTotalActiveUsers = getTotalActiveUsers;
function getActivePadCountFromSessionInfos() {
const padIds = new Set();
for (const {padId} of Object.values(sessioninfos)) {
if (!padId) continue;
padIds.add(padId);
}
return padIds.size;
}
exports.getActivePadCountFromSessionInfos = getActivePadCountFromSessionInfos;
stats.gauge('totalUsers', () => getTotalActiveUsers());
stats.gauge('activePads', () => {
return getActivePadCountFromSessionInfos();
});
/**

View file

@ -1,25 +1,38 @@
import client from 'prom-client'
const db = require('./db/DB').db
import client from 'prom-client';
const monitor = function () {
const collectDefaultMetrics = client.collectDefaultMetrics;
const Registry = client.Registry;
const register = new Registry();
collectDefaultMetrics({register});
const gaugeDB = new client.Gauge({
name: "ueberdb_stats",
help: "ueberdb stats",
labelNames: ['type'],
})
const db = require('./db/DB').db;
const PadMessageHandler = require('./handler/PadMessageHandler');
const register = new client.Registry();
const gaugeDB = new client.Gauge({
name: 'ueberdb_stats',
help: 'ueberdb stats',
labelNames: ['type'],
});
register.registerMetric(gaugeDB);
const totalUsersGauge = new client.Gauge({
name: 'etherpad_total_users',
help: 'Total number of users',
});
register.registerMetric(totalUsersGauge);
const activePadsGauge = new client.Gauge({
name: 'etherpad_active_pads',
help: 'Total number of active pads',
});
register.registerMetric(activePadsGauge);
client.collectDefaultMetrics({register});
const monitor = async function () {
for (const [metric, value] of Object.entries(db.metrics)) {
if (typeof value !== 'number') continue;
gaugeDB.set({type: metric}, value);
}
register.registerMetric(gaugeDB);
return register
activePadsGauge.set(PadMessageHandler.getActivePadCountFromSessionInfos());
totalUsersGauge.set(PadMessageHandler.getTotalActiveUsers());
return register;
};
export default monitor;