From 0493cb2600f16ae38ab0d0f496358dcd6030ebe0 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 30 Dec 2021 22:02:36 -0600 Subject: database metrics --- src/metrics.ts | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/metrics.ts (limited to 'src/metrics.ts') 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 ], +}) + + -- cgit