From adc47fd778a05445c8492044ae4b109b26f0fe41 Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Tue, 9 Jan 2024 13:17:16 +0100 Subject: [PATCH] feat: add db metrics for environment-store (#5808) This will give us prometheus metrics on environment lookups in the database --- .../project-environments/environment-store.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/lib/features/project-environments/environment-store.ts b/src/lib/features/project-environments/environment-store.ts index b74a91cf04f5..87c0982a72a5 100644 --- a/src/lib/features/project-environments/environment-store.ts +++ b/src/lib/features/project-environments/environment-store.ts @@ -145,9 +145,11 @@ export default class EnvironmentStore implements IEnvironmentStore { } async get(key: string): Promise { + const stopTimer = this.timer('get'); const row = await this.db(TABLE) .where({ name: key }) .first(); + stopTimer(); if (row) { return mapRow(row); } @@ -155,6 +157,7 @@ export default class EnvironmentStore implements IEnvironmentStore { } async getAll(query?: Object): Promise { + const stopTimer = this.timer('getAll'); let qB = this.db(TABLE) .select('*') .orderBy([ @@ -165,10 +168,12 @@ export default class EnvironmentStore implements IEnvironmentStore { qB = qB.where(query); } const rows = await qB; + stopTimer(); return rows.map(mapRow); } async getAllWithCounts(query?: Object): Promise { + const stopTimer = this.timer('getAllWithCounts'); let qB = this.db(TABLE) .select( '*', @@ -190,6 +195,7 @@ export default class EnvironmentStore implements IEnvironmentStore { qB = qB.where(query); } const rows = await qB; + stopTimer(); return rows.map(mapRowWithCounts); } @@ -197,6 +203,7 @@ export default class EnvironmentStore implements IEnvironmentStore { projectId: string, query?: Object, ): Promise { + const stopTimer = this.timer('getProjectEnvironments'); let qB = this.db(TABLE) .select( '*', @@ -223,23 +230,28 @@ export default class EnvironmentStore implements IEnvironmentStore { } const rows = await qB; + stopTimer(); return rows.map(mapRowWithProjectCounts); } async exists(name: string): Promise { + const stopTimer = this.timer('exists'); const result = await this.db.raw( `SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE name = ?) AS present`, [name], ); + stopTimer(); const { present } = result.rows[0]; return present; } async getByName(name: string): Promise { + const stopTimer = this.timer('getByName'); const row = await this.db(TABLE) .where({ name }) .first(); + stopTimer(); if (!row) { throw new NotFoundError( `Could not find environment with name ${name}`,