Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Koa instrumentation does not work with the New Relic ESM module loader #132

Closed
markdb314 opened this issue Dec 14, 2022 · 4 comments · Fixed by #133
Closed

Koa instrumentation does not work with the New Relic ESM module loader #132

markdb314 opened this issue Dec 14, 2022 · 4 comments · Fixed by #133
Assignees
Labels

Comments

@markdb314
Copy link

markdb314 commented Dec 14, 2022

Description

When attempting to load the New Relic Agent (v9.7.3) using the ESM module loader described here, the automatic instrumentation for Koa fails with the following error:

{"v":0,"level":40,"name":"newrelic","hostname":"0bfcea2519cb","pid":82743,"time":"2022-12-14T21:10:08.234Z","msg":"Custom instrumentation for koa failed. Please report this to the maintainers of the custom instrumentation.","component":"shimmer","stack":"TypeError: Cannot convert undefined or null to object\n at Function.keys (<anonymous>)\n at Object.initialize [as onRequire] (/my-system/node_modules/@newrelic/koa/lib/instrumentation.js:10:31)\n at instrumentPostLoad (/my-system/node_modules/newrelic/lib/shimmer.js:534:25)\n at _postLoad (/my-system/node_modules/newrelic/lib/shimmer.js:591:12)\n at Object.instrumentPostLoad (/my-system/node_modules/newrelic/lib/shimmer.js:477:20)\n at wrapModule (file:///my-system/node_modules/newrelic/lib/esm-shim.mjs:35:18)\n at file:///my-system/node_modules/koa/dist/koa.mjs?hasNrInstrumentation=true:4:28\n at ModuleJob.run (node:internal/modules/esm/module_job:193:25)\n at async Promise.all (index 0)\n at async ESMLoader.import (node:internal/modules/esm/loader:526:24)","message":"Cannot convert undefined or null to object"}

The root cause is at line 10 of instrumentation.js
if (!shim || !Koa || Object.keys(Koa.prototype).length > 1) {

When loaded via the ESM module loader, `Koa.prototype' is undefined.

I was able to fix it by simply adding the following line before the if statement above:
Koa.prototype = Koa.default.prototype;

The 'Koa.default' export was added for this purpose in the Koa project. See:

Steps to Reproduce

Run the New Relic agent in a Koa project following the instructions for using the ESM module loader, and look at the agent logs to find the error above.

@workato-integration
Copy link

@newrelic-node-agent-team newrelic-node-agent-team added this to Triage Needed: Unprioritized Features in Node.js Engineering Board Dec 14, 2022
@jmartin4563
Copy link
Contributor

Hiya @markdb314! Thank you for the (awesomely) detailed bug report, I'll be taking a look at this and will reach out if I have any questions :)

@jmartin4563 jmartin4563 self-assigned this Dec 14, 2022
Node.js Engineering Board automation moved this from Triage Needed: Unprioritized Features to Done: Issues recently completed Dec 15, 2022
@jmartin4563
Copy link
Contributor

Hi again @markdb314, v7.1.1 has been released with a fix for this, give it a spin and let me know how it works for you!

@markdb314
Copy link
Author

Works great, thanks for the speedy fix! 👏👏👏

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
Node.js Engineering Board
  
Done: Issues recently completed
Development

Successfully merging a pull request may close this issue.

2 participants