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!: Replaced esm loader with import-in-the-middle to fix instrumentation firing for both commonjs and esm #1760

Merged
merged 6 commits into from Aug 17, 2023

Conversation

bizob2828
Copy link
Member

@bizob2828 bizob2828 commented Aug 14, 2023

Details

This PR restores support for ESM in Node 20. It also supersedes #1759. It's also worth noting at the time of opening this it layers on top of #1758 . By relying on import-in-the-middle it de-couples are agent from proxying ESM packages to instrument. However this now requires us to do --loader newrelic/esm-loader.mjs -r newrelic. You can see by the PR that it also removes the complexities we created by having a test loader that injects a fake agent before calling the new loader, this is no longer needed. Also since our esm loader now just calls import in the middle, there is no need to test esm files and mock esm with testdouble. I also shifted some supportability metrics around ESM to the agent.

Changelog details

  • Breaking Change: Updated ESM loader that now requires to use both a loader and -r.
    • node --experimental-loader newrelic/esm-loader.mjs -r newrelic path/to/app.js
  • Breaking change: Removed config.esm.custom_instrumentation_entrypoint to register ESM instrumentation.
    • You can now just call the newrelic.instrument* APIs but you must pass in an object and specify isEsm: true.
  • Fixed issue where both CJS and ESM instrumentation tried to instrument the same package, thus causing transaction naming issues, see Transaction name multiplies when upgrading to v10 with ESM #1646
  • Updated agent to only run in the main thread. This is because running in a worker thread does not function out of the box and will just reduce the overhead for customers that are naively trying to load this.
import newrelic from 'newrelic'
newrelic.instrument({ moduleName: 'parse-json', isEsm: true }, function wrap(shim, parseJson, moduleName) {
  shim.wrap(parseJson.default, function wrapParseJson(shim, orig) {
      return function wrappedParseJson() {
          const result = orig.apply(this, arguments)
          result.instrumented = true
          return true
      }
  })
})

Related Issues

Closes #1646
Closes #1720

@codecov
Copy link

codecov bot commented Aug 15, 2023

Codecov Report

Merging #1760 (9a17fd0) into main (6788f9e) will decrease coverage by 0.01%.
Report is 1 commits behind head on main.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #1760      +/-   ##
==========================================
- Coverage   96.88%   96.88%   -0.01%     
==========================================
  Files         200      199       -1     
  Lines       39210    38838     -372     
  Branches       22        0      -22     
==========================================
- Hits        37989    37627     -362     
+ Misses       1221     1211      -10     
Flag Coverage Δ
esm-unit-tests-16.x ?
esm-unit-tests-18.x ?
integration-tests-16.x 79.04% <81.25%> (-0.10%) ⬇️
integration-tests-18.x 79.04% <81.25%> (-0.10%) ⬇️
integration-tests-20.x 79.07% <81.25%> (-0.08%) ⬇️
unit-tests-16.x 91.47% <89.58%> (+0.01%) ⬆️
unit-tests-18.x 91.46% <89.58%> (+0.01%) ⬆️
unit-tests-20.x 91.46% <89.58%> (+0.01%) ⬆️
versioned-tests-16.x 75.93% <100.00%> (-1.38%) ⬇️
versioned-tests-18.x 75.93% <100.00%> (-1.34%) ⬇️
versioned-tests-20.x 75.94% <100.00%> (-0.18%) ⬇️

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

Files Changed Coverage Δ
lib/config/default.js 100.00% <ø> (ø)
lib/instrumentation/amqplib/amqplib.js 90.06% <ø> (ø)
index.js 100.00% <100.00%> (ø)
lib/instrumentation/amqplib/nr-hooks.js 100.00% <100.00%> (ø)
lib/instrumentation/grpc-js/grpc.js 96.42% <100.00%> (-0.07%) ⬇️
lib/instrumentation/grpc-js/nr-hooks.js 100.00% <100.00%> (ø)
lib/instrumentation/pino/nr-hooks.js 100.00% <100.00%> (ø)
lib/instrumentation/pino/pino.js 100.00% <100.00%> (ø)
lib/instrumentations.js 100.00% <100.00%> (ø)
lib/metrics/names.js 100.00% <100.00%> (ø)
... and 1 more

... and 2 files with indirect coverage changes

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

@bizob2828 bizob2828 changed the title DO NOT MERGE: Iitm fix!: Replaced esm loader with import-in-the-middle to fix instrumentation firing for both commonjs and esm Aug 15, 2023
@bizob2828 bizob2828 marked this pull request as ready for review August 15, 2023 20:08
@jmartin4563 jmartin4563 self-assigned this Aug 16, 2023
@@ -272,8 +272,7 @@ const INFINITE_TRACING = {
const FEATURES = {
ESM: {
LOADER: `${SUPPORTABILITY.FEATURES}/ESM/Loader`,
UNSUPPORTED_LOADER: `${SUPPORTABILITY.FEATURES}/ESM/UnsupportedLoader`,
CUSTOM_INSTRUMENTATION: `${SUPPORTABILITY.FEATURES}/ESM/CustomInstrumentation`
Copy link
Contributor

Choose a reason for hiding this comment

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

Same thing about Angler here, should update to remove this metric

Copy link
Member Author

Choose a reason for hiding this comment

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

yep, ill get all these queued up in one PR shortly

Copy link
Member Author

Choose a reason for hiding this comment

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

lib/shimmer.js Show resolved Hide resolved
lib/shimmer.js Show resolved Hide resolved
@@ -13,16 +13,19 @@ module.exports = [
{
type: 'generic',
moduleName: '@grpc/grpc-js/build/src/resolving-call',
isEsm: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Wait, why did this have to get added? It's not an ESM module, and I thought that's what the flag is for?

Copy link
Member Author

Choose a reason for hiding this comment

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

This code is getting transpiled and it has named exports instead of default, that's why

jmartin4563
jmartin4563 previously approved these changes Aug 17, 2023
@bizob2828 bizob2828 changed the base branch from ritm to main August 17, 2023 15:35
@bizob2828 bizob2828 dismissed jmartin4563’s stale review August 17, 2023 15:35

The base branch was changed.

jmartin4563
jmartin4563 previously approved these changes Aug 17, 2023
@bizob2828 bizob2828 merged commit 4452354 into main Aug 17, 2023
40 of 41 checks passed
Node.js Engineering Board automation moved this from Needs PR Review to Done: Issues recently completed Aug 17, 2023
@bizob2828 bizob2828 deleted the iitm branch August 17, 2023 16:58
This was referenced Aug 21, 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
Development

Successfully merging this pull request may close these issues.

Update esm-loader to work with Node 20 Transaction name multiplies when upgrading to v10 with ESM
2 participants