# APM Node Agent: Exception is captured by APM, but is not being sent to the APM Server

**URL:** <https://discuss.elastic.co/t/apm-node-agent-exception-is-captured-by-apm-but-is-not-being-sent-to-the-apm-server/160595>\
**Category:** APM\
**Created:** [December 12, 2018, 5:58pm UTC](https://discuss.elastic.co/t/apm-node-agent-exception-is-captured-by-apm-but-is-not-being-sent-to-the-apm-server/160595 "2018-12-12T17:58:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexpereira](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexpereira/32/35962_2.png) [@alexpereira](https://discuss.elastic.co/u/alexpereira)\
**Post date:** [December 12, 2018, 5:58pm UTC](https://discuss.elastic.co/t/apm-node-agent-exception-is-captured-by-apm-but-is-not-being-sent-to-the-apm-server/160595/1 "2018-12-12T17:58:20Z")

</div>

We added the APM Node in our code inside the file server.js, being the first thing to load. However if there is an exception within the server.js file APM will capture the same but will not send it to the server.

The following log shows the catch of the exception. But we do not see her in Kibana. Other exceptions we can see normally.

**Log debug:**

shimming http@6.14.4 module  
shimming http.Server.prototype.emit function  
shimming http.request function  
shimming http.ServerResponse.prototype.writeHead function  
shimming https@6.14.4 module  
shimming https.Server.prototype.emit function  
shimming finalhandler@1.1.1 module  
shimming express@4.16.4 module  
shimming express.Router.process\_params function  
shimming express.Router.use function  
shimming express.static function  
copying property mime from express.static  
shimming bluebird@3.5.3 module  
shimming bluebird.prototype functions: ['\_then', '\_addCallbacks']  
shimming bluebird.config  
skip shimming layer.handle\_request (layer: , path: /)  
Elastic APM caught unhandled exception: aaa is not defined  
Sending error to Elastic APM { id: 'c58a14ce2494c0a7697a56cd2ebc3120' }  
no active transaction found - cannot build new span  
intercepted call to http.request %o { id: null }

**Payload log:**

{"metadata":{"service":{"name":"api-insights-development","runtime":{"name":"node","version":"6.14.4"},"language":{"name":"javascript"},"agent":{"name":"nodejs","version":"2.0.1"}},"process":{"pid":23264,"ppid":23254,"title":"node","argv":["/opt/elasticbeanstalk/node-install/node-v6.14.4-linux-x64/bin/node","/var/app/current"]},"system":{"hostname":"api-insights-development-ip-10-0-3-140","architecture":"x64","platform":"linux"}}}  
{"error":{"exception":{"message":"aaa is not defined","type":"ReferenceError","stacktrace":[{"filename":"server/server.js","lineno":71,"function":"Object.","library\_frame":false,"abs\_path":"/var/app/current/server/server.js","pre\_context":["};",""],"context\_line":"aaa();","post\_context":["","boot(app, bootOptions, function(err) {"]},{"filename":"module.js","lineno":577,"function":"Module.\_compile","library\_frame":true,"abs\_path":"module.js"},{"filename":"module.js","lineno":586,"function":"Module.\_extensions..js","library\_frame":true,"abs\_path":"module.js"},{"filename":"module.js","lineno":494,"function":"Module.load","library\_frame":true,"abs\_path":"module.js"},{"filename":"module.js","lineno":453,"function":"tryModuleLoad","library\_frame":true,"abs\_path":"module.js"},{"filename":"module.js","lineno":445,"function":"Module.\_load","library\_frame":true,"abs\_path":"module.js"},{"filename":"module.js","lineno":611,"function":"Module.runMain","library\_frame":true,"abs\_path":"module.js"},{"filename":"bootstrap\_node.js","lineno":394,"function":"run","library\_frame":true,"abs\_path":"bootstrap\_node.js"},{"filename":"bootstrap\_node.js","lineno":160,"function":"startup","library\_frame":true,"abs\_path":"bootstrap\_node.js"},{"filename":"bootstrap\_node.js","lineno":507,"function":"","library\_frame":true,"abs\_path":"bootstrap\_node.js"}],"handled":false},"culprit":"Object. (server/server.js)","id":"c58a14ce2494c0a7697a56cd2ebc3120","timestamp":1544636558941000,"context":{"user":{},"tags":{},"custom":{}}}}

---

<div class="post-metadata">

**Author:** ![wa7son](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wa7son/32/21259_2.png) [@wa7son](https://discuss.elastic.co/u/wa7son)\
**Post date:** [December 14, 2018, 1:52pm UTC](https://discuss.elastic.co/t/apm-node-agent-exception-is-captured-by-apm-but-is-not-being-sent-to-the-apm-server/160595/2 "2018-12-14T13:52:27Z")

</div>

The agent should normally hold open the process until the error event have been sent to the APM Server. However, if you have another module or code that hooks into the [`uncaughtException`](https://nodejs.org/api/process.html#process_event_uncaughtexception) event and manually shut down your app, that might exit the process before the error has been successfully sent to the APM Server.

If that's not the case, it might be a race-condition where the internals of Node.js or the OS doesn't completely flush the socket before Node.js exits and then the socket is terminated prematurely. I haven't seen this my self, but can't rule it out completely.

You can see if this is the case bu adding this code:

```auto
agent.handleUncaughtException(function () {
  console.error('Done sending error to APM Server - exiting...')
  process.exit(1)
})

```

If the log line in the callback is logged to STDERR, but the error is still not sent to the APM Server, try to add a delay before the `process.exit`, eg:

```auto
agent.handleUncaughtException(function () {
  console.error('Done sending error to APM Server')
  setTimeout(function () {
    console.error('Exiting...')
    process.exit(1)
  }, 100)
})

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 4, 2019, 9:52am UTC](https://discuss.elastic.co/t/apm-node-agent-exception-is-captured-by-apm-but-is-not-being-sent-to-the-apm-server/160595/3 "2019-01-04T09:52:34Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
