Skip to content

Loading Mongoose v7 with ESM does not work with OpenTelemetry Instrumentation: TypeError: Cannot read properties of undefined (reading 'prototype') #2792

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

Open
JCMais opened this issue Apr 14, 2025 · 0 comments · May be fixed by #2793
Labels
bug Something isn't working pkg:instrumentation-mongoose priority:p1 Bugs which cause problems in end-user applications such as crashes, data inconsistencies

Comments

@JCMais
Copy link

JCMais commented Apr 14, 2025

What version of OpenTelemetry are you using?

@opentelemetry/api 1.9.0
@opentelemetry/auto-instrumentations-node 0.56.0
@opentelemetry/core 1.30.1
@opentelemetry/exporter-metrics-otlp-grpc 0.57.2
@opentelemetry/exporter-metrics-otlp-http 0.57.2
@opentelemetry/exporter-trace-otlp-grpc 0.57.2
@opentelemetry/exporter-trace-otlp-http 0.57.2
@opentelemetry/host-metrics 0.35.5
@opentelemetry/instrumentation 0.57.2
@opentelemetry/resource-detector-gcp 0.33.0
@opentelemetry/resources 1.30.1
@opentelemetry/sdk-metrics 1.30.1
@opentelemetry/sdk-node 0.57.2
@opentelemetry/sdk-trace-base 1.30.1
@opentelemetry/sdk-trace-node 1.30.1
@opentelemetry/semantic-conventions 1.30.0

What version of Node are you using?

Node v20.18

What did you do?

Tried to set up OpenTelemetry with ESM, while using auto-instrumentation and Mongoose v7.

What did you expect to see?

It worked, not errors

What did you see instead?

root@localhost:/usr/src/app# node --no-warnings  --experimental-loader=@opentelemetry/instrumentation/hook.mjs --import ./apps/api/dist/tracing.js ./apps/api/dist/index.js 
/usr/src/app/node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/instrumentation-mongoose/build/src/mongoose.js:95
        this._wrap(moduleExports.Aggregate.prototype, 'exec', this.patchAggregateExec(moduleVersion));
                                           ^

TypeError: Cannot read properties of undefined (reading 'prototype')
    at MongooseInstrumentation.patch (/usr/src/app/node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/instrumentation-mongoose/build/src/mongoose.js:95:44)
    at MongooseInstrumentation._onRequire (/usr/src/app/node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js:164:39)
    at hookFn (/usr/src/app/node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js:227:29)
    at callHookFn (/usr/src/app/node_modules/.pnpm/[email protected]/node_modules/import-in-the-middle/index.js:29:22)
    at Hook._iitmHook (/usr/src/app/node_modules/.pnpm/[email protected]/node_modules/import-in-the-middle/index.js:150:11)
    at /usr/src/app/node_modules/.pnpm/[email protected]/node_modules/import-in-the-middle/lib/register.js:42:31
    at Array.forEach (<anonymous>)
    at register (/usr/src/app/node_modules/.pnpm/[email protected]/node_modules/import-in-the-middle/lib/register.js:42:15)
    at file:///usr/src/app/node_modules/.pnpm/[email protected]_patch_hash=5449370a6dee2c7ac58c04e310a8430620fcac96ad1d4f006889922638cfa_13e07d6321159eadef9120d66a5cb86e/node_modules/mongoose/index.js?iitm=true:428:1
    at ModuleJob.run (node:internal/modules/esm/module_job:234:25)

Node.js v20.18.1

Additional context

tracing.js

import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node' 
import { NodeSDK } from '@opentelemetry/sdk-node'

const sdk = new NodeSDK({
  instrumentations: [getNodeAutoInstrumentations()]
})

sdk.start()

index.js

import 'mongoose'

This is related to #2568, which has been closed by the author.

Looks like some other packages also had similar errors, and I can also reproduce this issue with the dataloader package.

E.g for pg: #1693, which was fixed here: #1701

and here ioredis: #1692, fixed here: #1694

@JCMais JCMais added the bug Something isn't working label Apr 14, 2025
@JCMais JCMais linked a pull request Apr 14, 2025 that will close this issue
@pichlermarc pichlermarc added priority:p1 Bugs which cause problems in end-user applications such as crashes, data inconsistencies pkg:instrumentation-mongoose labels Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pkg:instrumentation-mongoose priority:p1 Bugs which cause problems in end-user applications such as crashes, data inconsistencies
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants