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

How to wrap a handler/pre-handler #4049

Closed
jcchavezs opened this issue Feb 14, 2020 · 3 comments
Closed

How to wrap a handler/pre-handler #4049

jcchavezs opened this issue Feb 14, 2020 · 3 comments
Assignees
Labels
support Questions, discussions, and general support

Comments

@jcchavezs
Copy link

jcchavezs commented Feb 14, 2020

Support plan

  • which support plan is this issue covered by? (e.g. Community, Core, Plus, or Enterprise): Community
  • is this issue currently blocking your project? (yes/no): yes
  • is this issue affecting a production system? (yes/no): yes

Context

  • node version: 12
  • module version: 17+
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): hapi application
  • any other relevant information: this is for supporting a library

How can we help?

Currently we have an issue with hapi instrumentation when using promises. Usual instrumentation is based on middlewares and that allows us to wrap the next handlers with a scope. Hapi has a different model based on listeners.

In express for example, one might do:

  return function zipkinExpressMiddleware(req, res, next) {
     ...
     tracer.scoped(() => {
        ...
        next()
      })
  }

This means that everything in next() happens scoped. In hapi case, we subscribe to events onRequest, onPreHandler, onPreResponse but we can't actually wrap within a scope what happens in the handler (or pre handler). This code won't work for example:

server.route({
    method: 'GET',
    path: '/',
    config: {
      pre: [
        {
          method: () => {
              return doCall()
          },
          assign: 'users',
        },

whereas this one will do the trick:

server.route({
    method: 'GET',
    path: '/',
    config: {
      pre: [
        {
          method: (req) => {
            return tracer.scoped(function() {
              tracer.setId(req._trace_id) // restore the scope
              return doCall()
            })
          },
          assign: 'users',
        },

Is there any way we can wrap handler and pre-handlers without asking the user to specifically wrap the functions? If now, what is the recommended practice, should we extend Hapi server class and override the server.route method to do this wrapping?

For better context, this is the original issue in zipkin: openzipkin/zipkin-js#483 (comment)

@anuraaga
Copy link

@jcchavezs Looks like you're right, I read the comment in hapiMiddleware.js assuming it does something which it doesn't. I guess the comment isn't really valid, sent a PR to remove it to prevent confusion.

@jcchavezs
Copy link
Author

Friendly ping @hueniverse

@hueniverse hueniverse self-assigned this Mar 14, 2020
@hueniverse
Copy link
Contributor

Closing this in favor of the original issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

3 participants