Open mydea opened 2 months ago
If anyone needs any more info, I'm here!
I could reproduce it with my own example (repo: https://github.com/s1gr1d/example.astro-sentry-netlify).
Some things I saw in the function log on Netlify:
Sentry Logger [log]: Could not load NodeFetch instrumentation.
on Netlify (does not happen locally)/test-success/
is missing -> this one is a static page with no fetch call
a7364239 WARN Sentry Logger [warn]: Failed to register ESM hook Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/var/task/node_modules/@opentelemetry/instrumentation/hook.mjs' imported from /var/task/node_modules/@sentry/node/esm/sdk/initOtel.js
at new NodeError (node:internal/errors:405:5)
at finalizeResolution (node:internal/modules/esm/resolve:332:11)
at moduleResolve (node:internal/modules/esm/resolve:1029:10)
at moduleResolveWithNodePath (node:internal/modules/esm/resolve:876:12)
at defaultResolve (node:internal/modules/esm/resolve:1242:79)
at nextResolve (node:internal/modules/esm/hooks:864:28)
at Hooks.resolve (node:internal/modules/esm/hooks:302:30)
at ModuleLoader.resolve (node:internal/modules/esm/loader:366:35)
at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:250:38)
at ModuleLoader.import (node:internal/modules/esm/loader:336:34) {
url: 'file:///var/task/node_modules/@opentelemetry/instrumentation/hook.mjs',
code: 'ERR_MODULE_NOT_FOUND'
}
Without trying the code out yet myself, the ESM hook error suggests the app is bundled in some way or the full node_modules
isn't getting deployed.
It's trying to load /var/task/node_modules/@opentelemetry/instrumentation/hook.mjs
but it can't be found.
I still need to test this locally to confirm but I think this issue is caused by the Netlify tooling not including all the required files in production builds. Netlify uses @netlify/zip-it-and-ship-it
to collect all the files required for a production deploy, which in turn uses @vercel/nft
to resolve all the required source files.
My best guess so far is that this does not pick up that the otel hook should be included in the production build.
I suspect this is caused by https://github.com/vercel/nft/issues/428
You can probably work around this for now by specifying included_files
in your config and forcing it to include the missing files.
I attempted this.
Include the node_modules/@opentelemetry/instrumentation/hook.mjs
in my netlify.toml file as follows:
[functions]
included_files = ["node_modules/@opentelemetry/instrumentation/hook.mjs"]
The initial error disappeared, and now the logs were showing a different error:
Jun 25, 11:31:19 AM: 518d1b15 WARN Sentry Logger [warn]: Failed to register ESM hook Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/var/task/node_modules/import-in-the-middle/hook.js' imported from /var/task/node_modules/import-in-the-middle/hook.mjs
at finalizeResolution (node:internal/modules/esm/resolve:269:11)
at moduleResolve (node:internal/modules/esm/resolve:937:10)
at moduleResolveWithNodePath (node:internal/modules/esm/resolve:1161:14)
at defaultResolve (node:internal/modules/esm/resolve:1204:79)
at nextResolve (node:internal/modules/esm/hooks:866:28)
at Hooks.resolve (node:internal/modules/esm/hooks:304:30)
at ModuleLoader.resolve (node:internal/modules/esm/loader:345:35)
at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:227:38)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:87:39)
at link (node:internal/modules/esm/module_job:86:36) {
code: 'ERR_MODULE_NOT_FOUND',
url: 'file:///var/task/node_modules/import-in-the-middle/hook.js'
}
The file that is now missing is the file mentioned in https://github.com/vercel/nft/issues/428.
Let's see if including the second missing file solves things.
New netlify.toml configuration:
[functions]
included_files = ["node_modules/@opentelemetry/instrumentation/hook.mjs", "node_modules/import-in-the-middle/hook.mjs"]
This did not fix it. I saw the same error where the import-in-the-middle
hook file was reported missing.
I also saw this error in the logs a little time after, presumably because of the previous errors.
Jun 25, 11:34:53 AM: b82c400a ERROR Sentry Logger [error]: Error while sending event: Error: Client network socket disconnected before secure TLS connection was established
at TLSSocket.onConnectEnd (node:_tls_wrap:1727:19)
at TLSSocket.emit (node:events:531:35)
at endReadableNT (node:internal/streams/readable:1696:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
code: 'ECONNRESET',
path: null,
host: 'o4505635661873152.ingest.us.sentry.io',
port: 443,
localAddress: undefined
}
I suspect you might need to include more so that not only the missing file is included, but also all the files it references too:
[functions]
included_files = ["node_modules/@opentelemetry/instrumentation/*", "node_modules/import-in-the-middle/*"]
However, both of these dependencies have their own dependencies too so you might need to go as far as including all of node_modules
:
[functions]
included_files = ["node_modules/*"]
This is far from ideal though as I suspect this increases the deployment bundle size considerably!
Oof, yeah I'm not going to do that 😅
What I found out so far: Netlify is automatically creating a Netlify Function for the Astro server-side rendering (source) and this function is throwing the error. However, I found no way to test this locally with the CLI, as I have no function defined in the code and Netlify just generates the function automatically. Locally, there seems to be no issue.
It looks like this is not a problem with Astro, but rather one with Netlify and how they get their dependencies.
I attempted something similar as @whitep4nth3r (the changes to the netlify.toml
can be seen in the history of the file here)
I tried different variants (can be seen in the history of the file here). But those led to the same outcome.
Those are all the variants:
[functions]
node_bundler = "esbuild"
external_node_modules = ["@opentelemetry/instrumentation/hook.mjs"]
[functions]
external_node_modules = ["/var/task/node_modules/@opentelemetry/instrumentation/hook.mjs"]
[functions]
node_bundler = "esbuild"
external_node_modules = ["@opentelemetry/instrumentation/hook.mjs"]
[functions]
node_bundler = "esbuild"
included_files = ["node_modules/@opentelemetry/instrumentation/hook.mjs"]
[functions]
node_bundler = "esbuild"
external_node_modules = ["@opentelemetry/instrumentation"]
[functions]
node_bundler = "esbuild"
external_node_modules = ["@opentelemetry/instrumentation"]
included_files = ["node_modules/@opentelemetry/instrumentation/**"]
[functions]
node_bundler = "esbuild"
external_node_modules = ["@opentelemetry/instrumentation", "import-in-the-middle"]
included_files = ["node_modules/@opentelemetry/instrumentation/**", "node_modules/import-in-the-middle/**"]
--> no change in the error message
[functions]
node_bundler = "esbuild"
external_node_modules = ["@opentelemetry/instrumentation", "import-in-the-middle"]
included_files = ["node_modules/*"]
also this:
[functions]
included_files = ["node_modules/*"]
--> no change in the error message
[functions]
included_files = ["node_modules/@opentelemetry/instrumentation/*", "node_modules/import-in-the-middle/*", "node_modules/acorn-import-attributes/*"]
This gave the following error message:
Error: Cannot find module './lib/get-exports.js'
Require stack:
- /var/task/node_modules/import-in-the-middle/hook.js
at Module._resolveFilename (node:internal/modules/cjs/loader:1140:15)
at Module._load (node:internal/modules/cjs/loader:981:27)
at Module.require (node:internal/modules/cjs/loader:1231:19)
at require (node:internal/modules/helpers:177:18)
at Object.<anonymous> (/var/task/node_modules/import-in-the-middle/hook.js:20:16)
at Module._compile (node:internal/modules/cjs/loader:1364:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1422:10)
at Module.load (node:internal/modules/cjs/loader:1203:32)
at Module._load (node:internal/modules/cjs/loader:1019:12)
at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:203:29) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/var/task/node_modules/import-in-the-middle/hook.js' ]
}
"@opentelemetry/instrumentation": "^0.52.1",
New error message:
Failed to register ESM hook Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/var/task/node_modules/import-in-the-middle/hook.mjs' imported from /var/task/node_modules/@opentelemetry/instrumentation/hook.mjs
"import-in-the-middle": "1.8.1"
New error message:
rror: Cannot find module 'acorn-import-attributes'
Require stack:
- /var/task/node_modules/import-in-the-middle/lib/get-esm-exports.js
- /var/task/node_modules/import-in-the-middle/lib/get-exports.js
- /var/task/node_modules/import-in-the-middle/hook.js
at Module._resolveFilename (node:internal/modules/cjs/loader:1140:15)
at Module._load (node:internal/modules/cjs/loader:981:27)
at Module.require (node:internal/modules/cjs/loader:1231:19)
at require (node:internal/modules/helpers:177:18)
at Object.<anonymous> (/var/task/node_modules/import-in-the-middle/lib/get-esm-exports.js:4:30)
at Module._compile (node:internal/modules/cjs/loader:1364:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1422:10)
at Module.load (node:internal/modules/cjs/loader:1203:32)
at Module._load (node:internal/modules/cjs/loader:1019:12)
at Module.require (node:internal/modules/cjs/loader:1231:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/var/task/node_modules/import-in-the-middle/lib/get-esm-exports.js',
'/var/task/node_modules/import-in-the-middle/lib/get-exports.js',
'/var/task/node_modules/import-in-the-middle/hook.js'
]
}
I changed the build command in the Netlify configuration, so it prints the content of the file. This will happen after "Initializing" which installs the packages.
[build]
command = "cd node_modules/@opentelemetry/instrumentation && ls"
In the output we can see that the hook.mjs
file is there. But at some stage (maybe after the build?), netlify is deleting those files.
11:23:14 AM: $ cd node_modules/@opentelemetry/instrumentation && ls
11:23:14 AM: build
11:23:14 AM: hook.mjs
11:23:14 AM: LICENSE
11:23:14 AM: node_modules
11:23:14 AM: package.json
11:23:14 AM: README.md
I've opened an issue in the Netlify organisation and have referenced this issue.
Raised by @whitep4nth3r , there seem to be some issues with an astro app deployed to netfliy.
Repro: https://github.com/whitep4nth3r/p4nth3rworld
When running locally, stuff seems to work as far as I can tell. But when deployed to production (on Netlify), you can see that often no server transaction is sent: https://the-claw.sentry.io/performance/trace/ffcc30d02ff374907a3bef54c998f338/ (only a pageload).
It is sent sometimes but not always, leading to broken traces etc.
While looking more into this, when enabling debug logs, we found some things:
This log:
These logs:
something seems to be off there 🤔