Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: standardise logs #371

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 15 additions & 117 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Supabase storage middleend",
"main": "index.js",
"scripts": {
"dev": "ts-node-dev --log-error --files ./src/server.ts --stack_trace_limit=100 -r trace -r clarify",
"dev": "ts-node-dev --log-error --files ./src/server.ts --stack_trace_limit=100 -r trace -r clarify | pino-pretty",
"build": "tsc -p tsconfig.json",
"start": "NODE_ENV=production node dist/server.js",
"migration:run": "ts-node-dev ./src/scripts/migrate-call.ts",
Expand Down Expand Up @@ -58,7 +58,7 @@
"pg-boss": "^8.1.1",
"pg-listen": "^1.7.0",
"pino": "^8.2.0",
"pino-logflare": "^0.3.12",
"pino-logflare": "^0.4.2",
"postgres-migrations": "^5.3.0",
"prom-client": "^14.0.1"
},
Expand Down
4 changes: 0 additions & 4 deletions src/http/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ export const setErrorHandler = (app: FastifyInstance) => {
// it will be logged in the request log plugin
reply.executionError = error

if (process.env.NODE_ENV !== 'production') {
console.error(error)
}

if (isRenderableError(error)) {
const renderableError = error.render()
const statusCode = error.userStatusCode
Expand Down
47 changes: 41 additions & 6 deletions src/http/plugins/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TenantConnection } from '../../database/connection'
import { getServiceKeyUser } from '../../database/tenant'
import { getPostgresConnection } from '../../database'
import { verifyJWT } from '../../auth'
import { logSchema } from '../../monitoring'

declare module 'fastify' {
interface FastifyRequest {
Expand Down Expand Up @@ -33,21 +34,38 @@ export const db = fastifyPlugin(async (fastify) => {
fastify.addHook('onSend', async (request, reply, payload) => {
if (request.db) {
request.db.dispose().catch((e) => {
request.log.error(e, 'Error disposing db connection')
logSchema.error(request.log, 'Error disposing db connection', {
type: 'db-connection',
error: e,
})
})
}
return payload
})

fastify.addHook('onTimeout', async (request) => {
if (request.db) {
await request.db.dispose()
try {
await request.db.dispose()
} catch (e) {
logSchema.error(request.log, 'Error disposing db connection', {
type: 'db-connection',
error: e,
})
}
}
})

fastify.addHook('onRequestAbort', async (request) => {
if (request.db) {
await request.db.dispose()
try {
await request.db.dispose()
} catch (e) {
logSchema.error(request.log, 'Error disposing db connection', {
type: 'db-connection',
error: e,
})
}
}
})
})
Expand All @@ -72,7 +90,10 @@ export const dbSuperUser = fastifyPlugin(async (fastify) => {
fastify.addHook('onSend', async (request, reply, payload) => {
if (request.db) {
request.db.dispose().catch((e) => {
request.log.error(e, 'Error disposing db connection')
logSchema.error(request.log, 'Error disposing db connection', {
type: 'db-connection',
error: e,
})
})
}

Expand All @@ -81,13 +102,27 @@ export const dbSuperUser = fastifyPlugin(async (fastify) => {

fastify.addHook('onTimeout', async (request) => {
if (request.db) {
await request.db.dispose()
try {
await request.db.dispose()
} catch (e) {
logSchema.error(request.log, 'Error disposing db connection', {
type: 'db-connection',
error: e,
})
}
}
})

fastify.addHook('onRequestAbort', async (request) => {
if (request.db) {
await request.db.dispose()
try {
await request.db.dispose()
} catch (e) {
logSchema.error(request.log, 'Error disposing db connection', {
type: 'db-connection',
error: e,
})
}
}
})
})
21 changes: 9 additions & 12 deletions src/http/plugins/log-request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fastifyPlugin from 'fastify-plugin'
import { redactQueryParamFromRequest } from '../../monitoring'
import { normalizeRawError } from '../../storage'
import { logSchema, redactQueryParamFromRequest } from '../../monitoring'

interface RequestLoggerOptions {
excludeUrls?: string[]
Expand Down Expand Up @@ -30,15 +29,13 @@ export const logRequest = (options: RequestLoggerOptions) =>

const buildLogMessage = `${tenantId} | ${rMeth} | ${statusCode} | ${cIP} | ${rId} | ${rUrl} | ${uAgent}`

req.log.info(
{
req,
res: reply,
responseTime: reply.getResponseTime(),
error,
rawError: normalizeRawError(error),
},
buildLogMessage
)
logSchema.request(req.log, buildLogMessage, {
type: 'request',
req,
res: reply,
responseTime: reply.getResponseTime(),
error: error,
owner: req.owner,
})
})
})
1 change: 1 addition & 0 deletions src/http/plugins/log-tenant-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const logTenantId = fastifyPlugin(async (fastify) => {
reply.log = request.log = request.log.child({
tenantId: request.tenantId,
project: request.tenantId,
reqId: request.id,
})
})
})
Loading