1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-04 00:18:01 +01:00

feat: add db metrics for environment-store (#5808)

This will give us prometheus metrics on environment lookups in the
database
This commit is contained in:
Gard Rimestad 2024-01-09 13:17:16 +01:00 committed by GitHub
parent eb7882e2eb
commit adc47fd778
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -145,9 +145,11 @@ export default class EnvironmentStore implements IEnvironmentStore {
} }
async get(key: string): Promise<IEnvironment> { async get(key: string): Promise<IEnvironment> {
const stopTimer = this.timer('get');
const row = await this.db<IEnvironmentsTable>(TABLE) const row = await this.db<IEnvironmentsTable>(TABLE)
.where({ name: key }) .where({ name: key })
.first(); .first();
stopTimer();
if (row) { if (row) {
return mapRow(row); return mapRow(row);
} }
@ -155,6 +157,7 @@ export default class EnvironmentStore implements IEnvironmentStore {
} }
async getAll(query?: Object): Promise<IEnvironment[]> { async getAll(query?: Object): Promise<IEnvironment[]> {
const stopTimer = this.timer('getAll');
let qB = this.db<IEnvironmentsTable>(TABLE) let qB = this.db<IEnvironmentsTable>(TABLE)
.select('*') .select('*')
.orderBy([ .orderBy([
@ -165,10 +168,12 @@ export default class EnvironmentStore implements IEnvironmentStore {
qB = qB.where(query); qB = qB.where(query);
} }
const rows = await qB; const rows = await qB;
stopTimer();
return rows.map(mapRow); return rows.map(mapRow);
} }
async getAllWithCounts(query?: Object): Promise<IEnvironment[]> { async getAllWithCounts(query?: Object): Promise<IEnvironment[]> {
const stopTimer = this.timer('getAllWithCounts');
let qB = this.db<IEnvironmentsWithCountsTable>(TABLE) let qB = this.db<IEnvironmentsWithCountsTable>(TABLE)
.select( .select(
'*', '*',
@ -190,6 +195,7 @@ export default class EnvironmentStore implements IEnvironmentStore {
qB = qB.where(query); qB = qB.where(query);
} }
const rows = await qB; const rows = await qB;
stopTimer();
return rows.map(mapRowWithCounts); return rows.map(mapRowWithCounts);
} }
@ -197,6 +203,7 @@ export default class EnvironmentStore implements IEnvironmentStore {
projectId: string, projectId: string,
query?: Object, query?: Object,
): Promise<IProjectEnvironment[]> { ): Promise<IProjectEnvironment[]> {
const stopTimer = this.timer('getProjectEnvironments');
let qB = this.db<IEnvironmentsWithProjectCountsTable>(TABLE) let qB = this.db<IEnvironmentsWithProjectCountsTable>(TABLE)
.select( .select(
'*', '*',
@ -223,23 +230,28 @@ export default class EnvironmentStore implements IEnvironmentStore {
} }
const rows = await qB; const rows = await qB;
stopTimer();
return rows.map(mapRowWithProjectCounts); return rows.map(mapRowWithProjectCounts);
} }
async exists(name: string): Promise<boolean> { async exists(name: string): Promise<boolean> {
const stopTimer = this.timer('exists');
const result = await this.db.raw( const result = await this.db.raw(
`SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE name = ?) AS present`, `SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE name = ?) AS present`,
[name], [name],
); );
stopTimer();
const { present } = result.rows[0]; const { present } = result.rows[0];
return present; return present;
} }
async getByName(name: string): Promise<IEnvironment> { async getByName(name: string): Promise<IEnvironment> {
const stopTimer = this.timer('getByName');
const row = await this.db<IEnvironmentsTable>(TABLE) const row = await this.db<IEnvironmentsTable>(TABLE)
.where({ name }) .where({ name })
.first(); .first();
stopTimer();
if (!row) { if (!row) {
throw new NotFoundError( throw new NotFoundError(
`Could not find environment with name ${name}`, `Could not find environment with name ${name}`,