🐛 back: tolerate postgres individual errors in statistic gathering (#762)

This commit is contained in:
Eric Doughty-Papassideris
2024-12-12 04:06:20 +01:00
parent dab4d0e3ff
commit 7da9062916
@@ -71,26 +71,41 @@ export class PostgresConnector extends AbstractConnector<PostgresConnectionOptio
} }
async getDiagnostics(depth: TServiceDiagnosticDepth): Promise<TDiagnosticResult> { async getDiagnostics(depth: TServiceDiagnosticDepth): Promise<TDiagnosticResult> {
// 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) { switch (depth) {
// This is the only required `ok`
case TServiceDiagnosticDepth.alive: case TServiceDiagnosticDepth.alive:
return { ok: true, didConnect: await this.ping() }; 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: case TServiceDiagnosticDepth.stats_track:
return { return {
ok: true, ok: true,
db: ( db: await safeRequest("select * from pg_stat_database where datname = $1", [
await this.client.query("select * from pg_stat_database where datname = $1", [ this.options.database,
this.options.database, ]),
])
).rows,
}; };
case TServiceDiagnosticDepth.stats_basic: 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: case TServiceDiagnosticDepth.stats_deep:
return { return {
ok: true, ok: true,
databases: (await this.client.query("select * from pg_stat_database")).rows, databases: await safeRequest("select * from pg_stat_database"),
tables: (await this.client.query("select * from pg_stat_user_tables")).rows, tables: await safeRequest("select * from pg_stat_user_tables"),
indexes: (await this.client.query("select * from pg_stat_user_indexes")).rows, indexes: await safeRequest("select * from pg_stat_user_indexes"),
}; };
default: default: