From 01584f03e8159cbb7b411f33bd4f66f7131ce993 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 3 Feb 2023 21:29:23 +0100 Subject: Add metrics endpoint to streaming API (#23388) --- streaming/index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'streaming') diff --git a/streaming/index.js b/streaming/index.js index 0350c488d..e207411f2 100644 --- a/streaming/index.js +++ b/streaming/index.js @@ -850,6 +850,18 @@ const startWorker = async (workerId) => { res.end('OK'); }); + app.get('/metrics', (req, res) => server.getConnections((err, count) => { + res.writeHeader(200, { 'Content-Type': 'application/openmetrics-text; version=1.0.0; charset=utf-8' }); + res.write('# TYPE connected_clients gauge\n'); + res.write('# HELP connected_clients The number of clients connected to the streaming server\n'); + res.write(`connected_clients ${count}.0\n`); + res.write('# TYPE connected_channels gauge\n'); + res.write('# HELP connected_channels The number of Redis channels the streaming server is subscribed to\n'); + res.write(`connected_channels ${Object.keys(subs).length}.0\n`); + res.write('# EOF\n'); + res.end(); + })); + app.use(authenticationMiddleware); app.use(errorMiddleware); -- cgit From 4cb76475167750ef03b747aded962db7f1702f88 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 9 Feb 2023 11:20:59 +0100 Subject: Add database pool configuration over `DB_POOL` to streaming API (#23470) Add connection timeout of 15s --- streaming/index.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'streaming') diff --git a/streaming/index.js b/streaming/index.js index e207411f2..1fbbdea3b 100644 --- a/streaming/index.js +++ b/streaming/index.js @@ -127,7 +127,6 @@ const startWorker = async (workerId) => { database: process.env.DB_NAME || 'mastodon_development', host: process.env.DB_HOST || pg.defaults.host, port: process.env.DB_PORT || pg.defaults.port, - max: 10, }, production: { @@ -136,20 +135,19 @@ const startWorker = async (workerId) => { database: process.env.DB_NAME || 'mastodon_production', host: process.env.DB_HOST || 'localhost', port: process.env.DB_PORT || 5432, - max: 10, }, }; - if (!!process.env.DB_SSLMODE && process.env.DB_SSLMODE !== 'disable') { - pgConfigs.development.ssl = true; - pgConfigs.production.ssl = true; - } - const app = express(); app.set('trust proxy', process.env.TRUSTED_PROXY_IP ? process.env.TRUSTED_PROXY_IP.split(/(?:\s*,\s*|\s+)/) : 'loopback,uniquelocal'); - const pgPool = new pg.Pool(Object.assign(pgConfigs[env], dbUrlToConfig(process.env.DATABASE_URL))); + const pgPool = new pg.Pool(Object.assign(pgConfigs[env], dbUrlToConfig(process.env.DATABASE_URL), { + max: process.env.DB_POOL || 10, + connectionTimeoutMillis: 15000, + ssl: !!process.env.DB_SSLMODE && process.env.DB_SSLMODE !== 'disable', + })); + const server = http.createServer(app); const redisNamespace = process.env.REDIS_NAMESPACE || null; -- cgit From c5a4d8c82ddd5265abc830959a58cdaf1f061a43 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 9 Feb 2023 11:24:16 +0100 Subject: Add information about database pool to metrics endpoint in streaming API (#23469) --- streaming/index.js | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'streaming') diff --git a/streaming/index.js b/streaming/index.js index 1fbbdea3b..a8708ec78 100644 --- a/streaming/index.js +++ b/streaming/index.js @@ -856,6 +856,15 @@ const startWorker = async (workerId) => { res.write('# TYPE connected_channels gauge\n'); res.write('# HELP connected_channels The number of Redis channels the streaming server is subscribed to\n'); res.write(`connected_channels ${Object.keys(subs).length}.0\n`); + res.write('# TYPE pg.pool.total_connections gauge \n'); + res.write('# HELP pg.pool.total_connections The total number of clients existing within the pool\n'); + res.write(`pg.pool.total_connections ${pgPool.totalCount}.0\n`); + res.write('# TYPE pg.pool.idle_connections gauge \n'); + res.write('# HELP pg.pool.idle_connections The number of clients which are not checked out but are currently idle in the pool\n'); + res.write(`pg.pool.idle_connections ${pgPool.idleCount}.0\n`); + res.write('# TYPE pg.pool.waiting_queries gauge \n'); + res.write('# HELP pg.pool.waiting_queries The number of queued requests waiting on a client when all clients are checked out\n'); + res.write(`pg.pool.waiting_queries ${pgPool.waitingCount}.0\n`); res.write('# EOF\n'); res.end(); })); -- cgit