question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

elastic-apm-node with async hooks enabled leaks Transactions when used with cls-rtracer

See original GitHub issue

elastic-apm-node leaks Transaction objects when it is used in an express app that uses require("cls_rtracer").expressMiddleware().

To Reproduce

Steps to reproduce the behavior:

  1. Unzip cls-rtracer-example.zip
  2. Cd to cls-rtracer-example directory
  3. Run npm ci
  4. Run ELASTIC_APM_SERVICE_NAME=cls-rtracer-example ELASTIC_APM_SERVER_URL=http://elastic-server:8200 node --require elastic-apm-node/start index.js
  5. Hit http://localhost:3000 a bunch of time in a browser or with curl

Expected behavior

Following these steps the activeTransactions map in node_modules/elastic-apm-node/lib/instrumentation/async-hooks.js will continue to grow when the server processes http requests until the node process eventually runs out of memory.

Environment (please complete the following information)

  • OS: Fedora Linux 31 x86_64
  • Node.js version: Reproduced with 12.18.3 and 14.7.0
  • APM Server version: 8.5
  • Agent version: 3.6.1

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

  • 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

Additional context

I believe it is important to use cls-rtracer 2.x to reproduce this bug as it uses the AsyncLocalStorage API that is part of the async_hooks module in recent versions of node.

  • Agent config options

    Click to expand
    ELASTIC_APM_SERVICE_NAME=cls-rtracer-example
    ELASTIC_APM_SERVER_URL=http://elastic-server:8200
    
  • package.json dependencies:

    Click to expand
    "dependencies": {
        "cls-rtracer": "^2.2.0",
        "elastic-apm-node": "^3.6.1",
        "express": "^4.17.1"
    }
    

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10

github_iconTop GitHub Comments

2reactions
puzpuzpuzcommented, Aug 18, 2020

@tsondergaard I’ve checked the reproducer and it seems that current wrapHttpEmitters implementation in cls-rtracer (which monkey-patches EE.emit() method) conflicts with elastic-apm-node CLS related logic. As the result not all Transaction objects are removed from the activeTransactions map. I’ve changed the logic, so that the problem should be gone once this PR passes review: https://github.com/puzpuzpuz/cls-rtracer/pull/39

Thanks for reporting this issue.

1reaction
puzpuzpuzcommented, Aug 17, 2020

@tsondergaard I see now. Going to take a look at the reproducer tomorrow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Investigation about async-hooks #1697 - GitHub
In a script of mine, where I heavily use use callbacks, seems to get an extremely high spike of calls of the function...
Read more >
Configuration options | APM Node.js Agent Reference [master]
"asynchooks" - Use the async_hooks mechanism. (Using this value will restore the agent behavior from before v3.37.0.) "patch" - Use the limited monkey-patching ......
Read more >
Async hooks | Node.js v19.3.0 Documentation
Async hooks. Terminology; Overview; async_hooks.createHook(callbacks). Error handling; Printing in AsyncHook callbacks. Class: AsyncHook. asyncHook.enable ...
Read more >
Distributed Tracing in your Kibana with Node.JS - ITNEXT
Distributed tracing is a method used to profile and monitor applications, especially those built using a microservices architecture.
Read more >
Top 5 elastic-apm-node Code Examples - Snyk
Learn more about how to use elastic-apm-node, based on elastic-apm-node code examples created from the most popular ways it is used in public...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found