feat: add db metrics
This commit is contained in:
parent
28ed0f8b40
commit
57d183d652
@ -7,6 +7,7 @@ import { redis } from "./services/redis";
|
||||
import { startMetricsServer } from "@/app/metrics";
|
||||
import { activityCache } from "@/modules/sessionCache";
|
||||
import { auth } from "./modules/auth";
|
||||
import { startDatabaseMetricsUpdater } from "@/modules/metrics";
|
||||
|
||||
async function main() {
|
||||
|
||||
@ -29,6 +30,7 @@ async function main() {
|
||||
|
||||
await startApi();
|
||||
await startMetricsServer();
|
||||
startDatabaseMetricsUpdater();
|
||||
startTimeout();
|
||||
|
||||
//
|
||||
|
@ -1,4 +1,8 @@
|
||||
import { register, Counter, Gauge, Histogram } from 'prom-client';
|
||||
import { db } from '@/storage/db';
|
||||
import { forever } from '@/utils/forever';
|
||||
import { delay } from '@/utils/delay';
|
||||
import { shutdownSignal } from '@/utils/shutdown';
|
||||
|
||||
// Application metrics
|
||||
export const websocketConnectionsGauge = new Gauge({
|
||||
@ -56,6 +60,14 @@ export const httpRequestDurationHistogram = new Histogram({
|
||||
registers: [register]
|
||||
});
|
||||
|
||||
// Database count metrics
|
||||
export const databaseRecordCountGauge = new Gauge({
|
||||
name: 'database_records_total',
|
||||
help: 'Total number of records in database tables',
|
||||
labelNames: ['table'] as const,
|
||||
registers: [register]
|
||||
});
|
||||
|
||||
// WebSocket connection tracking
|
||||
const connectionCounts = {
|
||||
'user-scoped': 0,
|
||||
@ -73,5 +85,31 @@ export function decrementWebSocketConnection(type: 'user-scoped' | 'session-scop
|
||||
websocketConnectionsGauge.set({ type }, connectionCounts[type]);
|
||||
}
|
||||
|
||||
// Database metrics updater
|
||||
export async function updateDatabaseMetrics(): Promise<void> {
|
||||
// Query counts for each table
|
||||
const [accountCount, sessionCount, messageCount, machineCount] = await Promise.all([
|
||||
db.account.count(),
|
||||
db.session.count(),
|
||||
db.sessionMessage.count(),
|
||||
db.machine.count()
|
||||
]);
|
||||
|
||||
// Update metrics
|
||||
databaseRecordCountGauge.set({ table: 'accounts' }, accountCount);
|
||||
databaseRecordCountGauge.set({ table: 'sessions' }, sessionCount);
|
||||
databaseRecordCountGauge.set({ table: 'messages' }, messageCount);
|
||||
databaseRecordCountGauge.set({ table: 'machines' }, machineCount);
|
||||
}
|
||||
|
||||
export function startDatabaseMetricsUpdater(): void {
|
||||
forever('database-metrics-updater', async () => {
|
||||
await updateDatabaseMetrics();
|
||||
|
||||
// Wait 60 seconds before next update
|
||||
await delay(60 * 1000, shutdownSignal);
|
||||
});
|
||||
}
|
||||
|
||||
// Export the register for combining metrics
|
||||
export { register };
|
Loading…
Reference in New Issue
Block a user