From 7da9062916ff260f651d70f7b79cb04472079628 Mon Sep 17 00:00:00 2001 From: Eric Doughty-Papassideris Date: Thu, 12 Dec 2024 04:06:20 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20back:=20tolerate=20postgres=20in?= =?UTF-8?q?dividual=20errors=20in=20statistic=20gathering=20(#762)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../orm/connectors/postgres/postgres.ts | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres.ts b/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres.ts index 12dba80c..6534ca1f 100644 --- a/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres.ts +++ b/tdrive/backend/node/src/core/platform/services/database/services/orm/connectors/postgres/postgres.ts @@ -71,26 +71,41 @@ export class PostgresConnector extends AbstractConnector { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const safeRequest = async (query: string, values?: any[]) => { + try { + return (await this.client.query(query, values)).rows; + } catch (err) { + const logId = "pg-diags-error-" + Math.floor(process.uptime() * 1000); + logger.error( + { err, query, values, logId, errCode: err.code }, + `Error running postgresql statistics at ${depth} ( ${logId} ) `, + ); + return { error: true, logId }; + } + }; switch (depth) { + // This is the only required `ok` case TServiceDiagnosticDepth.alive: return { ok: true, didConnect: await this.ping() }; + + // Statistics can silently fail, and do it granularly if there is + // a permission issue only on some of the stats case TServiceDiagnosticDepth.stats_track: return { ok: true, - db: ( - await this.client.query("select * from pg_stat_database where datname = $1", [ - this.options.database, - ]) - ).rows, + db: await safeRequest("select * from pg_stat_database where datname = $1", [ + this.options.database, + ]), }; case TServiceDiagnosticDepth.stats_basic: - return { ok: true, warn: "pgsql_basic_has_nothing_more_than_track" }; + return { ok: true, warn: "pgsql_basic_has_no_basic_level_stats" }; case TServiceDiagnosticDepth.stats_deep: return { ok: true, - databases: (await this.client.query("select * from pg_stat_database")).rows, - tables: (await this.client.query("select * from pg_stat_user_tables")).rows, - indexes: (await this.client.query("select * from pg_stat_user_indexes")).rows, + databases: await safeRequest("select * from pg_stat_database"), + tables: await safeRequest("select * from pg_stat_user_tables"), + indexes: await safeRequest("select * from pg_stat_user_indexes"), }; default: