diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/database.ts | 4 | ||||
-rw-r--r-- | src/index.ts | 17 | ||||
-rw-r--r-- | src/metrics.ts | 54 |
3 files changed, 60 insertions, 15 deletions
diff --git a/src/database.ts b/src/database.ts index 8a384b1..9ef812d 100644 --- a/src/database.ts +++ b/src/database.ts @@ -803,6 +803,10 @@ export async function updateAccount(discordId: string, schema: AccountSchema) { }, { $set: schema }, { upsert: true }) } +export async function fetchServerStatus() { + return await database.admin().serverStatus() +} + // make sure it's not in a test console.log('global.isTest', globalThis.isTest) if (!globalThis.isTest) { diff --git a/src/index.ts b/src/index.ts index 613d7c8..7332805 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { createSession, fetchAccountFromDiscord, fetchAllLeaderboardsCategorized, fetchLeaderboard, fetchMemberLeaderboardSpots, fetchSession, finishedCachingRawLeaderboards, leaderboardUpdateMemberQueue, leaderboardUpdateProfileQueue, updateAccount } from './database.js' +import { createSession, fetchAccountFromDiscord, fetchAllLeaderboardsCategorized, fetchLeaderboard, fetchMemberLeaderboardSpots, fetchSession, finishedCachingRawLeaderboards, leaderboardUpdateMemberQueue, leaderboardUpdateProfileQueue, updateAccount, fetchServerStatus } from './database.js' import { fetchMemberProfile, fetchUser } from './hypixel.js' import rateLimit from 'express-rate-limit' import * as constants from './constants.js' @@ -6,7 +6,7 @@ import * as discord from './discord.js' import express from 'express' import { getKeyUsage } from './hypixelApi.js' import { basicPlayerCache, basicProfilesCache, playerCache, profileCache, profileNameCache, profilesCache, usernameCache } from './hypixelCached.js' -import { collectDefaultMetrics, Counter, Gauge, register } from 'prom-client' +import { register } from './metrics.js' const app = express() @@ -189,19 +189,6 @@ app.post('/accounts/update', async (req, res) => { }) -// grafana integration -collectDefaultMetrics() - -const apiKeyCounter = new Gauge({ - name: 'hypixel_api_key_usage', - help: 'API requests in the past minute.', - registers: [ register ], - collect() { - let keyUsage = getKeyUsage() - apiKeyCounter.set(keyUsage.usage) - } -}) - app.get('/metrics', async (req, res) => { if (!req.headers.host?.startsWith('0.0.0.0:')) return res.status(403).send('Forbidden') diff --git a/src/metrics.ts b/src/metrics.ts new file mode 100644 index 0000000..9db3ec3 --- /dev/null +++ b/src/metrics.ts @@ -0,0 +1,54 @@ +import { collectDefaultMetrics, Gauge, register } from 'prom-client' +import { fetchServerStatus } from './database.js' +import { getKeyUsage } from './hypixelApi.js' +export { register } from 'prom-client' + + +// grafana integration +collectDefaultMetrics() + +const apiKeyCounter = new Gauge({ + name: 'hypixel_api_key_usage', + help: 'API requests in the past minute.', + registers: [ register ], + collect() { + let keyUsage = getKeyUsage() + apiKeyCounter.set(keyUsage.usage) + } +}) +const connectionsCounter = new Gauge({ + name: 'mongodb_current_connections', + help: 'Number of active connections to the database.', + registers: [ register ], + async collect() { + let status = await fetchServerStatus() + + connectionsCounter.set(status.connections.current) + networkBytesInCounter.set(status.network.bytesIn) + networkBytesOutCounter.set(status.network.bytesOut) + queryOpCounter.set(status.opcounters.query) + updateOpCounter.set(status.opcounters.update) + } +}) +const networkBytesInCounter = new Gauge({ + name: 'mongodb_network_bytes_in', + help: 'Number of bytes received by the database.', + registers: [ register ], +}) +const networkBytesOutCounter = new Gauge({ + name: 'mongodb_network_bytes_out', + help: 'Number of bytes sent by the database.', + registers: [ register ], +}) +const queryOpCounter = new Gauge({ + name: 'mongodb_query_op_count', + help: 'Number of queries executed by the database.', + registers: [ register ], +}) +const updateOpCounter = new Gauge({ + name: 'mongodb_update_op_count', + help: 'Number of updates executed by the database.', + registers: [ register ], +}) + + |