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

fix: prevent ESM loader from loading instrumentation multiple times #1729

Merged

Conversation

jmartin4563
Copy link
Contributor

@jmartin4563 jmartin4563 commented Jul 18, 2023

Description

Version 10 of the Node agent added support for instrumenting a module more than once. This had a knock-on effect in our ESM loader in that we were naively calling registerInstrumentation every time a CJS module was imported, which in turn, would cause our instrumentation to be registered multiple times if an application import-ed a module more than once. This exhibited as route name duplication for transactions in the Express instrumentation / GH issue, but would have caused strangeness with all instrumentation.

To fix, I've added CJS modules to the registeredSpecifiers Map after performing instrumentation registration, and added a check to the beginning of the resolve hook logic to not register again if the loader has already performed on a module.

How to Test

Related Issues

Closes #1646 and Closes #1693

@codecov
Copy link

codecov bot commented Jul 18, 2023

Codecov Report

Merging #1729 (df47f2d) into main (0b96de3) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main    #1729   +/-   ##
=======================================
  Coverage   96.87%   96.87%           
=======================================
  Files         200      200           
  Lines       39181    39191   +10     
  Branches       24       25    +1     
=======================================
+ Hits        37956    37966   +10     
  Misses       1225     1225           
Flag Coverage Δ
esm-unit-tests-14.x 45.58% <0.00%> (-2.23%) ⬇️
esm-unit-tests-16.x 92.43% <100.00%> (+0.31%) ⬆️
esm-unit-tests-18.x 92.43% <100.00%> (+0.31%) ⬆️
integration-tests-14.x 79.03% <ø> (ø)
integration-tests-16.x 79.12% <ø> (-0.01%) ⬇️
integration-tests-18.x 79.11% <ø> (-0.02%) ⬇️
unit-tests-14.x 91.41% <ø> (ø)
unit-tests-16.x 91.47% <ø> (ø)
unit-tests-18.x 91.45% <ø> (ø)
versioned-tests-14.x 75.70% <ø> (?)
versioned-tests-16.x 77.03% <ø> (-0.05%) ⬇️
versioned-tests-18.x 77.03% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
esm-loader.mjs 97.67% <100.00%> (+0.11%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@bizob2828 bizob2828 self-assigned this Jul 18, 2023
Copy link
Member

@bizob2828 bizob2828 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified the new test will fail if you remove line 92 from esm-loader.mjs. Nice work tracking this down

@bizob2828 bizob2828 merged commit 962d172 into newrelic:main Jul 18, 2023
24 checks passed
Node.js Engineering Board automation moved this from Needs PR Review to Done: Issues recently completed Jul 18, 2023
@jmartin4563 jmartin4563 deleted the fix-esm-instrumentation-duplication branch July 18, 2023 19:20
@github-actions github-actions bot mentioned this pull request Jul 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Node.js Engineering Board
  
Done: Issues recently completed
2 participants