(sentry) monitor database query performance

This commit is contained in:
Valentyne Stigloher 2024-03-03 13:32:23 +01:00
parent f8156d5204
commit b0a11cf07c

View File

@ -3,6 +3,7 @@ import './setup.ts';
import express from 'express';
import type { Request, Response, NextFunction } from 'express';
import * as Sentry from '@sentry/node';
import type { StartSpanOptions } from '@sentry/types';
import authenticate from '../src/authenticate.ts';
import dbConnection from './db.ts';
import type { Database, SQLQuery } from './db.ts';
@ -87,21 +88,33 @@ export class LazyDatabase implements Database {
}
}
buildSpanOptions(sql: SQLQuery): StartSpanOptions {
return {
name: typeof sql === 'string' ? sql : sql.sql,
op: 'db',
attributes: {
'db.system': 'sqlite',
},
};
}
async get<T = unknown>(sql: SQLQuery, ...args: unknown[]): Promise<T | undefined> {
await this.init();
return this.db!.get(sql, ...args);
return Sentry.startSpan(this.buildSpanOptions(sql), () => this.db!.get(sql, ...args));
}
async all<T = unknown>(sql: SQLQuery, ...args: unknown[]): Promise<T[]> {
await this.init();
return this.db!.all(sql, ...args);
return Sentry.startSpan(this.buildSpanOptions(sql), () => this.db!.all(sql, ...args));
}
async close(): Promise<void> {
if (this.db !== null) {
try {
await this.db.close();
} catch {}
} catch (error) {
Sentry.captureException(error);
}
}
}
}