Log slow Blink database queries
This commit is contained in:
Vendored
+4
-2
@@ -98,13 +98,15 @@ export class SafelinkClient {
|
|||||||
url: string,
|
url: string,
|
||||||
pattern: ToolsOzoneSafelinkDefs.PatternType,
|
pattern: ToolsOzoneSafelinkDefs.PatternType,
|
||||||
): Promise<SafelinkRule> {
|
): Promise<SafelinkRule> {
|
||||||
return db.db
|
return db.observeQuery(`resolve_safelink_${pattern}_rule`, () =>
|
||||||
|
db.db
|
||||||
.selectFrom('safelink_rule')
|
.selectFrom('safelink_rule')
|
||||||
.selectAll()
|
.selectAll()
|
||||||
.where('url', '=', url)
|
.where('url', '=', url)
|
||||||
.where('pattern', '=', pattern)
|
.where('pattern', '=', pattern)
|
||||||
.orderBy('createdAt', 'desc')
|
.orderBy('createdAt', 'desc')
|
||||||
.executeTakeFirstOrThrow()
|
.executeTakeFirstOrThrow(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private async addRule(db: Database, rule: SafelinkRule) {
|
private async addRule(db: Database, rule: SafelinkRule) {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import assert from 'assert'
|
import assert from 'assert'
|
||||||
|
import {performance} from 'node:perf_hooks'
|
||||||
import {
|
import {
|
||||||
Kysely,
|
Kysely,
|
||||||
type KyselyPlugin,
|
type KyselyPlugin,
|
||||||
@@ -101,6 +102,27 @@ export class Database {
|
|||||||
return this.db.isTransaction
|
return this.db.isTransaction
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async observeQuery<T>(operation: string, query: () => Promise<T>): Promise<T> {
|
||||||
|
const startedAt = performance.now()
|
||||||
|
try {
|
||||||
|
return await query()
|
||||||
|
} finally {
|
||||||
|
const durationMs = Math.round(performance.now() - startedAt)
|
||||||
|
if (durationMs >= 1000) {
|
||||||
|
log.warn(
|
||||||
|
{
|
||||||
|
durationMs,
|
||||||
|
operation,
|
||||||
|
poolIdleConnections: this.cfg.pool.idleCount,
|
||||||
|
poolTotalConnections: this.cfg.pool.totalCount,
|
||||||
|
poolWaitingRequests: this.cfg.pool.waitingCount,
|
||||||
|
},
|
||||||
|
'slow database query',
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
assertTransaction() {
|
assertTransaction() {
|
||||||
assert(this.isTransaction, 'Transaction required')
|
assert(this.isTransaction, 'Transaction required')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,11 +16,13 @@ export default function (ctx: AppContext, app: Express) {
|
|||||||
typeof linkId === 'string',
|
typeof linkId === 'string',
|
||||||
'express guarantees id parameter is a string',
|
'express guarantees id parameter is a string',
|
||||||
)
|
)
|
||||||
const found = await ctx.db.db
|
const found = await ctx.db.observeQuery('resolve_short_link', () =>
|
||||||
|
ctx.db.db
|
||||||
.selectFrom('link')
|
.selectFrom('link')
|
||||||
.selectAll()
|
.selectAll()
|
||||||
.where('id', '=', linkId)
|
.where('id', '=', linkId)
|
||||||
.executeTakeFirst()
|
.executeTakeFirst(),
|
||||||
|
)
|
||||||
if (!found) {
|
if (!found) {
|
||||||
// potentially broken or mistyped link
|
// potentially broken or mistyped link
|
||||||
res.setHeader('Cache-Control', 'no-store')
|
res.setHeader('Cache-Control', 'no-store')
|
||||||
|
|||||||
Reference in New Issue
Block a user