Skip to content

Commit

Permalink
feat: include hooks not within route handler when wrapping routes (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
10xLaCroixDrinker authored Mar 5, 2024
1 parent 7a33995 commit 3b7b9e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
32 changes: 12 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ async function openTelemetryPlugin (fastify, opts = {}) {
contextMap.set(request, trace.setSpan(activeContext, span))
}

function onRequestWrapRoutes (request, reply, done) {
if (
!shouldIgnoreRoute(request.url, request.method) &&
(wrapRoutes === true || (Array.isArray(wrapRoutes) && wrapRoutes.includes(request.url)))
) {
context.with(getContext(request), done)
} else {
done()
}
}

async function onResponse (request, reply) {
if (shouldIgnoreRoute(request.url, request.method)) return

Expand All @@ -136,28 +147,9 @@ async function openTelemetryPlugin (fastify, opts = {}) {
}

fastify.addHook('onRequest', onRequest)
if (wrapRoutes) fastify.addHook('onRequest', onRequestWrapRoutes)
fastify.addHook('onResponse', onResponse)
fastify.addHook('onError', onError)

if (wrapRoutes) {
function wrapRoute (routeHandler) {
return function (request, ...args) {
const reqContext = getContext(request)
return context.with(reqContext, routeHandler.bind(this, request, ...args))
}
}

fastify.addHook('onRoute', function (routeOpts) {
const { path, handler, method } = routeOpts
if (!shouldIgnoreRoute(path, method)) {
if (wrapRoutes === true) {
routeOpts.handler = wrapRoute(handler)
} else if (Array.isArray(wrapRoutes) && wrapRoutes.includes(path)) {
routeOpts.handler = wrapRoute(handler)
}
}
})
}
}

module.exports = fp(openTelemetryPlugin, {
Expand Down
11 changes: 10 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,17 @@ test('should wrap all routes when wrapRoutes is true', async ({ equal, same, tea

test('should only wrap routes provided in wrapRoutes array', async ({ same, equal, teardown }) => {
const dummyContext = trace.setSpan(ROOT_CONTEXT, STUB_SPAN)
let next

const fastify = require('fastify')()

await fastify.register(openTelemetryPlugin, { wrapRoutes: ['/testTwo'] })

fastify.addHook('preHandler', (request, reply, done) => {
next = done
done()
})

const testHandlerOne = async () => 'one'
const testHandlerTwo = async () => 'two'

Expand All @@ -257,11 +263,14 @@ test('should only wrap routes provided in wrapRoutes array', async ({ same, equa
})

await fastify.inject({ ...injectArgs, url: '/testOne' })

equal(STUB_CONTEXT_API.with.called, false)

await fastify.inject({ ...injectArgs, url: '/testTwo' })

equal(STUB_CONTEXT_API.with.calledOnce, true)
same(STUB_CONTEXT_API.with.args[0][0], dummyContext)
same(await STUB_CONTEXT_API.with.args[0][1](), await testHandlerTwo())
same(STUB_CONTEXT_API.with.args[0][1], next)
})

test('should ignore routes found in ignoreRoutes array', async ({ equal, same, teardown }) => {
Expand Down

0 comments on commit 3b7b9e2

Please sign in to comment.