count metrics faster

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2024-12-09 16:50:57 +01:00
parent 6701b843c7
commit 1fdb48ef02
2 changed files with 33 additions and 8 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend': patch
---
Use a faster count method on pg when computing some metrics
@@ -98,20 +98,40 @@ export function initDatabaseMetrics(knex: Knex) {
description: 'Total amount of registered locations in the catalog',
})
.addCallback(async gauge => {
const total = await knex<DbLocationsRow>('locations').count({
count: '*',
});
gauge.observe(Number(total[0].count));
if (knex.client.config.client === 'pg') {
// https://stackoverflow.com/questions/7943233/fast-way-to-discover-the-row-count-of-a-table-in-postgresql
const total = await knex.raw(`
SELECT reltuples::bigint AS estimate
FROM pg_class
WHERE oid = 'locations'::regclass;
`);
gauge.observe(Number(total.rows[0].estimate));
} else {
const total = await knex<DbLocationsRow>('locations').count({
count: '*',
});
gauge.observe(Number(total[0].count));
}
}),
relations: meter
.createObservableGauge('catalog_relations_count', {
description: 'Total amount of relations between entities',
})
.addCallback(async gauge => {
const total = await knex<DbRelationsRow>('relations').count({
count: '*',
});
gauge.observe(Number(total[0].count));
if (knex.client.config.client === 'pg') {
// https://stackoverflow.com/questions/7943233/fast-way-to-discover-the-row-count-of-a-table-in-postgresql
const total = await knex.raw(`
SELECT reltuples::bigint AS estimate
FROM pg_class
WHERE oid = 'relations'::regclass;
`);
gauge.observe(Number(total.rows[0].estimate));
} else {
const total = await knex<DbRelationsRow>('relations').count({
count: '*',
});
gauge.observe(Number(total[0].count));
}
}),
};
}