apm-agent-nodejs icon indicating copy to clipboard operation
apm-agent-nodejs copied to clipboard

APM agent (and so app) dies when using the bodyParser plugin in restify application - unhandled exception prevent route completion

Open waswrongassembled opened this issue 8 years ago • 22 comments

Describe the bug When using ES APM Agent in an restify app and the bodyParser Plugin at the same time, the app dies with the first request/an exception is thrown by APM which cannot be handled. APM agent writes his own error wrappedHandler (node_modules/elastic-apm-node/lib/instrumentation/modules/restify.js) but afterwards the request dies.

To Reproduce Steps to reproduce the behavior:

  1. Create a npm project (elastic-apm-node version doesn't matter - bug exists in 1.x and 2.x)
{
  "name": "apm-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "elastic-apm-node": "^1.14.3",
    "restify": "^7.2.3"
  }
}
  1. Create a simple server
// // Add this to the VERY top of the first file loaded in your app
var apm = require('elastic-apm-node').start({
    serviceName: 'development_apm-test',
  
    // Set custom APM Server URL (default: http://localhost:8200)
    serverUrl: 'http://XXXX:8200',
  })

var restify = require('restify');

function respond(req, res, next) {
  res.send('hello ' + req.params.name);
  next();
}

var server = restify.createServer();

server.use(restify.plugins.jsonBodyParser());

server.get('/hello/:name', respond);
server.head('/hello/:name', respond);

server.listen(8080, function() {
  console.log('%s listening at %s', server.name, server.url);
});
  1. Call the app's endpoint in the browser..
http://localhost:8080/hello/waldi
  1. See in the console logging error XXXX-XXX-XXX with Elastic APM and the request is dead.
  2. Output in Kibana/ES:
{
  "library_frame": true,
  "exclude_from_grouping": false,
  "filename": "node_modules/elastic-apm-node/lib/instrumentation/modules/restify.js",
  "abs_path": "~/apm-test/node_modules/elastic-apm-node/lib/instrumentation/modules/restify.js",
  "line": {
    "number": 37,
    "context": "        return handler.apply(this, arguments)"
  },
  "function": "wrappedHandler",
  "context": {
    "pre": [
      "          }",
      "        })"
    ],
    "post": [
      "      }",
      "    }"
    ]
  }
},
{
  "exclude_from_grouping": false,
  "library_frame": true,
  "filename": "node_modules/restify/lib/chain.js",
  "abs_path": "~/apm-test/node_modules/restify/lib/chain.js",
  "line": {
    "number": 164,
    "context": "        handler(req, res, next);"
  },
  "function": "call",
  "context": {
    "pre": [
      "    } else if (!hasError && arity < 4) {",
      "        // request-handling middleware"
    ],
    "post": [
      "        return;",
      "    }"
    ]
  }
},
{
  "library_frame": true,
  "exclude_from_grouping": false,
  "filename": "node_modules/restify/lib/chain.js",
  "abs_path": "~/apm-test/node_modules/restify/lib/chain.js",
  "line": {
    "number": 120,
    "context": "        call(handler, err, req, res, self.onceNext ? self._once(next) : next);"
  },
  "function": "next",
  "context": {
    "pre": [
      "",
      "        // call the handler"
    ],
    "post": [
      "    }",
      ""
    ]
  }
},
{
  "library_frame": true,
  "exclude_from_grouping": false,
  "filename": "node_modules/restify/lib/chain.js",
  "abs_path": "~/apm-test/node_modules/restify/lib/chain.js",
  "line": {
    "number": 123,
    "context": "    next();"
  },
  "function": "run",
  "context": {
    "pre": [
      "    }",
      ""
    ],
    "post": [
      "    return;",
      "};"
    ]
  }
},
{
  "library_frame": true,
  "exclude_from_grouping": false,
  "filename": "node_modules/restify/lib/server.js",
  "abs_path": "~/apm-test/node_modules/restify/lib/server.js",
  "line": {
    "number": 1002,
    "context": "    self.useChain.run(req, res, function useChainDone(err) {"
  },
  "function": "_runUse",
  "context": {
    "pre": [
      "    req._timeUseStart = process.hrtime();",
      ""
    ],
    "post": [
      "        // Execution time of a handler with error can be significantly lower",
      "        req._timeUseEnd = process.hrtime();"
    ]
  }
},
{
  "library_frame": true,
  "exclude_from_grouping": false,
  "abs_path": "~/apm-test/node_modules/restify/lib/server.js",
  "filename": "node_modules/restify/lib/server.js",
  "line": {
    "number": 916,
    "context": "    self._runUse(req, res, function afterUse() {"
  },
  "function": "_runRoute",
  "context": {
    "pre": [
      "    self.emit('routed', req, res, req.route);",
      ""
    ],
    "post": [
      "        // DTrace",
      "        if (self.dtrace) {"
    ]
  }
},
{
  "library_frame": true,
  "exclude_from_grouping": false,
  "abs_path": "~/apm-test/node_modules/restify/lib/server.js",
  "filename": "node_modules/restify/lib/server.js",
  "line": {
    "number": 886,
    "context": "    self._runRoute(req, res);"
  },
  "function": "_afterPre",
  "context": {
    "pre": [
      "    }",
      ""
    ],
    "post": [
      "};",
      ""
    ]
  }
},
{
  "library_frame": true,
  "exclude_from_grouping": false,
  "filename": "node_modules/restify/lib/server.js",
  "abs_path": "~/apm-test/node_modules/restify/lib/server.js",
  "line": {
    "number": 854,
    "context": "        self._afterPre(err, req, res);"
  },
  "function": "preChainDone",
  "context": {
    "pre": [
      "        // Execution time of a handler with error can be significantly lower",
      "        req._timePreEnd = process.hrtime();"
    ],
    "post": [
      "    });",
      "};"
    ]
  }
},
{
  "library_frame": true,
  "exclude_from_grouping": false,
  "filename": "timers.js",
  "abs_path": "timers.js",
  "line": {
    "number": 634
  },
  "function": "processImmediate"
},
{
  "library_frame": true,
  "exclude_from_grouping": false,
  "filename": "domain.js",
  "abs_path": "domain.js",
  "line": {
    "number": 120
  },
  "function": "topLevelDomainCallback"
}

Expected behavior App runs normal without beeing influenced or interrupted by APM.

Environment (please complete the following information)

  • OS: OSX 10.14.1; Docker Image node:alpine latest.
  • Node.js version: v10.4.1
  • APM Server version: 6.2.3
  • Agent version: 1.x or 2.x doesn't matter

How are you starting the agent? (please tick one of the boxes)

  • [x] Calling agent.start() directly (e.g. require('elastic-apm-node').start(...))
  • [ ] Requiring elastic-apm-node/start from within the source code
  • [ ] Starting node with -r elastic-apm-node/start

waswrongassembled avatar Dec 04 '18 12:12 waswrongassembled

@waswrongassembled Thanks for reporting this bug and sorry for the troubles it have caused. I've identified the problem and have a possible solution. Hopefully we can have a patch released for 2.x shortly.

I can see that you're using APM Server v6.2. Would it be possible for you to upgrade to APM Server v6.5? That is the only version compatible with version 2.x of this module, whereas version 1.x is compatible with older versions of the APM Server.

watson avatar Dec 04 '18 21:12 watson

Hi,

that sounds great. Unfortunately we are currently bound to ES 6.2 that’s why we are bound to APM server 6.2 (AFAIK) and so the APM client 1.x.

Is it possible to fix it in the 1.x as well?

Best, Andre

On 4. Dec 2018, at 22:31, Thomas Watson [email protected] wrote:

@waswrongassembled https://github.com/waswrongassembled Thanks for reporting this bug and sorry for the troubles it have caused. I've identified the problem and have a possible solution. Hopefully we can have a patch released shortly.

I can see that you're using APM Server v6.2. Would it be possible for you to upgrade to APM Server v6.5? That is the only version compatible with version 2.x of this module, whereas version 1.x is compatible with older versions of the APM Server.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/elastic/apm-agent-nodejs/issues/706#issuecomment-444266097, or mute the thread https://github.com/notifications/unsubscribe-auth/AA2pJ67YutAkbRs4IgUWGEVMDaXrimibks5u1um0gaJpZM4ZAkWE.

waswrongassembled avatar Dec 05 '18 08:12 waswrongassembled

@waswrongassembled yes that should be possible. I'll make sure to backport the fix once it lands in master

watson avatar Dec 05 '18 08:12 watson

Great thx!!

waswrongassembled avatar Dec 05 '18 08:12 waswrongassembled

@watson any way I can get around this until this is fixed and released?

gcsabbagh avatar Dec 05 '18 22:12 gcsabbagh

@watson Hey, when can I expect that the backport will be released?

waswrongassembled avatar Dec 07 '18 07:12 waswrongassembled

Hopefully today. We're just working on backporting the changes to 1.x

watson avatar Dec 07 '18 12:12 watson

I missed your comment about a temporary workaround. What you can do is disable Restify instrumentation. This will mean that errors given to next() are not automatically caught by the agent, but I think the transactions should still be named correctly.

You can do that by using the disableInstrumentations config option and setting it to ['restify']

watson avatar Dec 07 '18 12:12 watson

Hey @watson thank you for the temp workaround. I will use that for now

I tried setting npm to your latest commit with the fix but I still have a similar issue in my app.

TypeError: handler.apply is not a function\n
at Server.wrappedHandler (/code/node_modules/elastic-apm-node/lib/instrumentation/modules/restify.js:37:24)

We use:

  "inversify": "^5.0.1",
  "inversify-restify-utils": "3.4.0",
  "restify": "^7.2.1",
  "typescript": "^3.0.1"

Should I open a separate issue?

gcsabbagh avatar Dec 07 '18 15:12 gcsabbagh

It looks from your stack trace that you tried to use the code in the 1.x branch. This doesn't yet include the fix as the backport PR (#721) hasn't landed yet, so that's why it still doesn't work. You could use my feature branch here to test it out:

https://github.com/watson/apm-agent-nodejs/tree/backport/1.x/pr-709

But it will go away once we merge.

watson avatar Dec 07 '18 15:12 watson

I am using "elastic-apm-node": "elastic/apm-agent-nodejs#0e82351fe8d30f9449d9ecb74a5c90fc2496c43e" actually which poitns to https://github.com/elastic/apm-agent-nodejs/commit/0e82351fe8d30f9449d9ecb74a5c90fc2496c43e if I understand correctly.

I will try again with 2.0.3

gcsabbagh avatar Dec 07 '18 15:12 gcsabbagh

Version 2.x will not work with your version of the APM Server. In that case you need to upgrade your Elastic Stack to v6.5

watson avatar Dec 07 '18 15:12 watson

@watson I think you are confusing me with OP, I am on elastic stack 6.5, I was just encountering the same error (with different packages)

gcsabbagh avatar Dec 07 '18 15:12 gcsabbagh

@gcssabbagh oh, my bad 😅 Yes, then v2.0.3 should work. I just released it and it contains the fix for this bug

watson avatar Dec 07 '18 15:12 watson

@watson yes just tried it, still have the same issue. I think inversify does not play well with the agent based on the stack trace. I will open a new issue once I can reproduce in isolation. Thank you and sorry for the confusion

gcsabbagh avatar Dec 07 '18 15:12 gcsabbagh

@gcssabbagh What's the stack trace? Still the same as the one you posted above? If so it's very strange as that code should no longer exist in 2.0.3 🤔

watson avatar Dec 07 '18 15:12 watson

here it is:

"TypeError: handler.apply is not a function\n
at Server.wrappedHandler (/code/node_modules/elastic-apm-node/lib/instrumentation/modules/restify.js:37:24)\n
at next (/code/node_modules/inversify-restify-utils/node_modules/restify/lib/server.js:1123:29)\n
at f (/code/node_modules/once/once.js:36:25)\n
at wrappedNext (/code/node_modules/elastic-apm-node/lib/instrumentation/modules/restify.js:34:25)\n
at Server.parseQueryString (/code/node_modules/restify/lib/plugins/query.js:67:16)\n
at Server.wrappedHandler (/code/node_modules/elastic-apm-node/lib/instrumentation/modules/restify.js:37:24)\n
at next (/code/node_modules/inversify-restify-utils/node_modules/restify/lib/server.js:1123:29)\n
at f (/code/node_modules/once/once.js:36:25)\n
at Server._run (/code/node_modules/inversify-restify-utils/node_modules/restify/lib/server.js:1177:16)\n
at _route (/code/node_modules/inversify-restify-utils/node_modules/restify/lib/server.js:825:14)\n
at onRoute (/code/node_modules/inversify-restify-utils/node_modules/restify/lib/server.js:937:24)\n
at Router.find (/code/node_modules/inversify-restify-utils/node_modules/restify/lib/router.js:551:9)\n
at Server._route (/code/node_modules/inversify-restify-utils/node_modules/restify/lib/server.js:920:28)\n
at Server._routeAndRun (/code/node_modules/inversify-restify-utils/node_modules/restify/lib/server.js:816:10)\n
at _run (/code/node_modules/inversify-restify-utils/node_modules/restify/lib/server.js:860:18)\n
at f (/code/node_modules/once/once.js:36:25)\n
at next (/code/node_modules/inversify-restify-utils/node_modules/restify/lib/server.js:1153:20)\n
at f (/code/node_modules/once/once.js:36:25)\n
at wrappedNext (/code/node_modules/elastic-apm-node/lib/instrumentation/modules/restify.js:34:25)\n
at Server.corsHelper (/code/app/Registry.ts:110:6)\n
at Server.wrappedHandler (/code/node_modules/elastic-apm-node/lib/instrumentation/modules/restify.js:37:24)\n
at next (/code/node_modules/inversify-restify-utils/node_modules/restify/lib/server.js:1123:29)\n
at f (/code/node_modules/once/once.js:36:25)\n
at wrappedNext (/code/node_modules/elastic-apm-node/lib/instrumentation/modules/restify.js:34:25)\n
at Server._sanitizePath (/code/node_modules/restify/lib/plugins/pre/prePath.js:48:9)\n
at Server.wrappedHandler (/code/node_modules/elastic-apm-node/lib/instrumentation/modules/restify.js:37:24)\n
at next (/code/node_modules/inversify-restify-utils/node_modules/restify/lib/server.js:1123:29)\n
at f (/code/node_modules/once/once.js:36:25)\n
at wrappedNext (/code/node_modules/elastic-apm-node/lib/instrumentation/modules/restify.js:34:25)\n
at Server.server.pre (/code/app/Registry.ts:96:6)\n
at Server.wrappedHandler (/code/node_modules/elastic-apm-node/lib/instrumentation/modules/restify.js:37:24)\n
at next (/code/node_modules/inversify-restify-utils/node_modules/restify/lib/server.js:1123:29)\n
at f (/code/node_modules/once/once.js:36:25)\n
at Server._run (/code/node_modules/inversify-restify-utils/node_modules/restify/lib/server.js:1177:16)\n
at Server._handle (/code/node_modules/inversify-restify-utils/node_modules/restify/lib/server.js:852:14)\n
at Server.onRequest (/code/node_modules/inversify-restify-utils/node_modules/restify/lib/server.js:214:14)\n
at Server.emit (events.js:182:13)\n
at Server.EventEmitter.emit (domain.js:442:20)\n
at Server.emit (/code/node_modules/elastic-apm-node/lib/instrumentation/http-shared.js:55:19)\n
at parserOnIncoming (_http_server.js:672:12)\n
at HTTPParser.parserOnHeadersComplete (_http_common.js:109:17)"

gcsabbagh avatar Dec 07 '18 15:12 gcsabbagh

@gcssabbagh it sounds like that you're running an older version and not version 2.0.3. This stack trace doesn't match that release.

Line 37 is an empty line in that version: https://github.com/elastic/apm-agent-nodejs/blob/v2.0.3/lib/instrumentation/modules/restify.js#L37

And handler.apply isn't used in that file at all.

watson avatar Dec 07 '18 16:12 watson

@watson you are correct, it was docker volumes shenanigans at play. it works as expected now, thank you!

gcsabbagh avatar Dec 07 '18 17:12 gcsabbagh

@waswrongassembled The fix have now been backported and released in version 1.14.4 a well

watson avatar Dec 07 '18 18:12 watson