mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-06 20:26:28 +00:00
53 lines
1.7 KiB
JavaScript
53 lines
1.7 KiB
JavaScript
const { performance } = require('node:perf_hooks');
|
|
const { Gauge, Histogram } = require('prom-client');
|
|
|
|
const pools = new Map();
|
|
|
|
// Snapshot pool diambil dari memori saat scrape, tanpa query PostgreSQL.
|
|
const connections = new Gauge({
|
|
name: 'topuponline_db_pool_connections',
|
|
help: 'Connections by pool and state in the serving Node process',
|
|
labelNames: ['pool', 'state'],
|
|
registers: [],
|
|
collect() {
|
|
for (const [name, pool] of pools) {
|
|
this.set({ pool: name, state: 'used' }, pool.numUsed());
|
|
this.set({ pool: name, state: 'idle' }, pool.numFree());
|
|
this.set({ pool: name, state: 'max' }, pool.max);
|
|
}
|
|
},
|
|
});
|
|
|
|
const pending = new Gauge({
|
|
name: 'topuponline_db_pool_pending_acquires',
|
|
help: 'Requests waiting for a database pool connection',
|
|
labelNames: ['pool'],
|
|
registers: [],
|
|
collect() {
|
|
for (const [name, pool] of pools) this.set({ pool: name }, pool.numPendingAcquires());
|
|
},
|
|
});
|
|
|
|
const acquire = new Histogram({
|
|
name: 'topuponline_db_pool_acquire_seconds',
|
|
help: 'Time to acquire a database connection by outcome',
|
|
labelNames: ['pool', 'outcome'],
|
|
buckets: [0.001, 0.005, 0.025, 0.1, 0.25, 0.5, 1, 2.5, 5, 10],
|
|
registers: [],
|
|
});
|
|
|
|
function observePool(name, pool) {
|
|
pools.set(name, pool);
|
|
const started = new Map();
|
|
pool.on('acquireRequest', (id) => started.set(id, performance.now()));
|
|
const finish = (id, outcome) => {
|
|
const start = started.get(id);
|
|
started.delete(id);
|
|
if (start !== undefined) acquire.observe({ pool: name, outcome }, (performance.now() - start) / 1000);
|
|
};
|
|
pool.on('acquireSuccess', (id) => finish(id, 'success'));
|
|
pool.on('acquireFail', (id) => finish(id, 'failure'));
|
|
}
|
|
|
|
module.exports = { observePool, poolMetrics: [connections, pending, acquire] };
|