-
Has anyone attempted to equip functions in a plugin with the request id? // index.js
...
import { db } from './plugins/db'
let fastify = Fastify()
fastify.register(db, {dbOptions}
...
// plugins/db.js
import { routeQuery } from './routes/query.js'
...
export const db async (fastify, options) {
const { db } = options
function dbDecorator (request, reply, done) {
request.db = db
done()
}
fastify.addHook('onRoute', (route) => { route.onRequest = [dbDecorator] })
await fastify.register(routeQuery, routeOpts)
}
// routes/query.js
const route FastifyPluginAsync = async function (fastify, opts) {
fastify.get('/', { schema: { querystring, response }, async (request, reply) => {
//blah blah blah... prep query string for query
await request.db.query(blah blah)
return data
})
}
export default route For example, in the above code base I would potentially pass in the requestId to the query function if I'm using a wrapper that I can manipulate. Then I can add log statements around the actual query. // routes/query.js
...
await request.db.query(request.id, blah blah)
// db/query.js
...
async query (reqId, blah blah) {
const queryStart = process.hrtime()
let data = await client.query(sql`blah blah`)
const queryEnd = process.hrtime(queryStart)
logger.info({ time_in_query: queryEnd blah blah, reqId })
return data
} This feels like the wrong way to do it, because I'm logging several more lines. If I'm using a package like atdatabases it gets even more excessive if I need to add things like onQueryStart, onQueryError, onConnection, etc. At this point I'd be getting 4-5 logs per request. Perhaps overwhelming downstream consumers (either from an ingestion or just from a data overload standpoints) Instead I'd like to be able to add this information to the default configuration s.t. other performance metrics can be added as they are collected. This way I'm reducing the overall footprint of the logs by adding one or two lines to one log. //Request
{
"level":"info",
"time":1697642485460,
"reqId":"req-1",
"req":{
"method":"GET",
"url":"/health",
"hostname":"localhost:8080",
"remoteAddress":"127.0.0.1",
"remotePort":52395
},
"message":"incoming request"
}
//Response
{
"level":"info",
"time":1697642485464,
"reqId":"req-1",
"res":{
"statusCode":200
},
"responseTimes":
{
"totalTime": 4.014708995819092
"timeInQuery":foo,
"timeInFooApi":bar,
...
},
"message":"request completed"
} I'm thinking a hook might be the way to go, but I keep getting stuck on the implementation there and thought I would poll the community. Has anyone tried this approach before? What implementation method did you use? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
You can do it already with open telemetry: |
Beta Was this translation helpful? Give feedback.
You can do it already with open telemetry: