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

[@opentelemetry/instrumentation-fastify] Breaks use of preClose with fastify #1762

Closed
SeanReece opened this issue Oct 30, 2023 · 2 comments · Fixed by #1764
Closed

[@opentelemetry/instrumentation-fastify] Breaks use of preClose with fastify #1762

SeanReece opened this issue Oct 30, 2023 · 2 comments · Fixed by #1764
Assignees
Labels
bug Something isn't working priority:p1 Bugs which cause problems in end-user applications such as crashes, data inconsistencies

Comments

@SeanReece
Copy link

What version of OpenTelemetry are you using?

@opentelemetry/[email protected]
@opentelemetry/[email protected]

What version of Node are you using?

v20.9.0

What did you do?

When adding the hook preClose hook to fastify the application crashes when the preClose hook is called with this error:

TypeError: Cannot read properties of undefined (reading 'Symbol(opentelemetry.instrumentation.fastify.request_active_span)')
    at startSpan (/Users/me/src/test/node_modules/@opentelemetry/instrumentation-fastify/build/src/utils.js:30:24)
    at Object.<anonymous> (/Users/me/src/test/node_modules/@opentelemetry/instrumentation-fastify/build/src/instrumentation.js:74:48)
    at /Users/me/src/test/node_modules/fastify/lib/hooks.js:166:24
    at _encapsulateThreeParam (/Users/me/src/test/node_modules/avvio/boot.js:562:7)
    at Boot.timeoutCall (/Users/me/src/test/node_modules/avvio/boot.js:458:5)
    at Boot.callWithCbOrNextTick (/Users/me/src/test/node_modules/avvio/boot.js:440:19)
    at Object.push (/Users/me/src/test/node_modules/fastq/queue.js:110:14)
    at Boot.ready (/Users/me/src/test/node_modules/avvio/boot.js:336:18)
    at server.<computed> (/Users/me/src/test/node_modules/avvio/boot.js:59:21)
    at next (/Users/me/src/test/node_modules/fastify/lib/hooks.js:145:5)

onClose works as expected, but it appears that preClose needs to be added to the list of applicationHooks here:

Reproducible Example

const { NodeSDK } = require('@opentelemetry/sdk-node')
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http')
const { FastifyInstrumentation } = require('@opentelemetry/instrumentation-fastify')

const sdk = new NodeSDK({
  serviceName: 'testotel',
  instrumentations: [new HttpInstrumentation(), new FastifyInstrumentation()],
})

sdk.start()

const fastify = require('fastify')({
  logger: true,
})

// Declare a route
fastify.get('/', function (request, reply) {
  reply.send({ hello: 'world' })
})

fastify.addHook('preClose', () => {
  // This will never be called
  console.log('Closing')
})

fastify.listen({ port: 3000 }, function (err, address) {
  if (err) {
    fastify.log.error(err)
    process.exit(1)
  }
  // Lets close right away to trigger preClose
  fastify.close()
})

What did you expect to see?

I expect preClose should work as expected.

What did you see instead?

Application crashes when preClose is called

Additional context

@SeanReece SeanReece added the bug Something isn't working label Oct 30, 2023
@dyladan dyladan added the priority:p1 Bugs which cause problems in end-user applications such as crashes, data inconsistencies label Nov 1, 2023
@dyladan
Copy link
Member

dyladan commented Nov 1, 2023

Code owner @pichlermarc is out today but please look when you get back

@trentm
Copy link
Contributor

trentm commented Nov 2, 2023

I can take this one, if you like.

In addition to skipping wrapping preClose, I think the instrumentation should also skip wrapping onRequestAbort (https://fastify.dev/docs/latest/Reference/Hooks#onrequestabort) because it doesn't receive the (request, reply, ...) arguments that _wrapHandler expects.

Initially I didn't notice the existing applicationHookNames guard and implemented my own guard -- but I made an allow-list, rather than the applicationHookNames deny-list. @pichlermarc Would you like a patch that changes to an allow-list of hook names to wrap? We could add onRequestAbort to the deny-list -- and perhaps rename that constant because onRequestAbort isn't an "application" hook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority:p1 Bugs which cause problems in end-user applications such as crashes, data inconsistencies
Projects
None yet
3 participants