Log slow Blink database operations (#11505)

This commit is contained in:
Austin McKinley
2026-08-19 12:38:48 -07:00
committed by GitHub
parent a329899552
commit cc26e5a090
5 changed files with 98 additions and 16 deletions
+2 -2
View File
@@ -8,7 +8,7 @@ import {linkRedirectContents} from '../html/linkRedirectContents.js'
import {linkWarningContents} from '../html/linkWarningContents.js'
import {linkWarningLayout} from '../html/linkWarningLayout.js'
import {redirectLogger} from '../logger.js'
import {handler} from './util.js'
import {observedHandler} from './util.js'
const INTERNAL_IP_REGEX = new RegExp(
'(^127.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$)|(^10.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$)|(^172.1[6-9]{1}[0-9]{0,1}.[0-9]{1,3}.[0-9]{1,3}$)|(^172.2[0-9]{1}[0-9]{0,1}.[0-9]{1,3}.[0-9]{1,3}$)|(^172.3[0-1]{1}[0-9]{0,1}.[0-9]{1,3}.[0-9]{1,3}$)|(^192.168.[0-9]{1,3}.[0-9]{1,3}$)|^localhost',
@@ -18,7 +18,7 @@ const INTERNAL_IP_REGEX = new RegExp(
export default function (ctx: AppContext, app: Express) {
return app.get(
'/redirect',
handler(async (req, res) => {
observedHandler('redirect', async (req, res) => {
let link = req.query.u
assert(
typeof link === 'string',
+9 -7
View File
@@ -4,23 +4,25 @@ import {DAY, SECOND} from '@atproto/common'
import {Express} from 'express'
import {AppContext} from '../context.js'
import {handler} from './util.js'
import {observedHandler} from './util.js'
export default function (ctx: AppContext, app: Express) {
return app.get(
'/:linkId',
handler(async (req, res) => {
observedHandler('short_link', async (req, res) => {
const linkId = req.params.linkId
const contentType = req.accepts(['html', 'json'])
assert(
typeof linkId === 'string',
'express guarantees id parameter is a string',
)
const found = await ctx.db.db
.selectFrom('link')
.selectAll()
.where('id', '=', linkId)
.executeTakeFirst()
const found = await ctx.db.observeQuery('resolve_short_link', () =>
ctx.db.db
.selectFrom('link')
.selectAll()
.where('id', '=', linkId)
.executeTakeFirst(),
)
if (!found) {
// potentially broken or mistyped link
res.setHeader('Cache-Control', 'no-store')
+30
View File
@@ -1,7 +1,11 @@
import {performance} from 'node:perf_hooks'
import {ErrorRequestHandler, Request, RequestHandler, Response} from 'express'
import {httpLogger} from '../logger.js'
const SLOW_REQUEST_THRESHOLD_MS = 1000
export type Handler = (req: Request, res: Response) => Awaited<void>
export const handler = (runHandler: Handler): RequestHandler => {
@@ -14,6 +18,32 @@ export const handler = (runHandler: Handler): RequestHandler => {
}
}
export const observedHandler = (
operation: string,
runHandler: Handler,
): RequestHandler => {
return handler(async (req, res) => {
const startedAt = performance.now()
try {
await runHandler(req, res)
} finally {
const durationMs = Math.round(performance.now() - startedAt)
if (durationMs >= SLOW_REQUEST_THRESHOLD_MS) {
httpLogger.warn(
{
durationMs,
method: req.method,
operation,
requestTraceId: req.get('x-amzn-trace-id'),
statusCode: res.statusCode,
},
'slow request',
)
}
}
})
}
export const errorHandler: ErrorRequestHandler = (err, _req, res, next) => {
httpLogger.error({err}, 'request error')
if (res.headersSent) {