Skip to content

fix: instrumentation of ESM-imported mongoose #2793

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

Merged
merged 4 commits into from
Apr 30, 2025

Conversation

JCMais
Copy link
Contributor

@JCMais JCMais commented Apr 14, 2025

Which problem is this PR solving?

Instrumenting mongoose while using ESM was crashing with:

TypeError: Cannot read properties of undefined (reading 'prototype')
    at MongooseInstrumentation.patch (/home/jcm/projects/opensource/opentelemetry-js-contrib/plugins/node/instrumentation-mongoose/build/src/mongoose.js:95:44)
    at MongooseInstrumentation._onRequire (/home/jcm/projects/opensource/opentelemetry-js-contrib/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js:164:39)
    at hookFn (/home/jcm/projects/opensource/opentelemetry-js-contrib/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js:227:29)
    at callHookFn (/home/jcm/projects/opensource/opentelemetry-js-contrib/node_modules/import-in-the-middle/index.js:29:22)
    at Hook._iitmHook (/home/jcm/projects/opensource/opentelemetry-js-contrib/node_modules/import-in-the-middle/index.js:150:11)
    at /home/jcm/projects/opensource/opentelemetry-js-contrib/node_modules/import-in-the-middle/index.js:18:41
    at Array.forEach (<anonymous>)
    at addHook (/home/jcm/projects/opensource/opentelemetry-js-contrib/node_modules/import-in-the-middle/index.js:18:10)
    at new Hook (/home/jcm/projects/opensource/opentelemetry-js-contrib/node_modules/import-in-the-middle/index.js:158:3)
    at MongooseInstrumentation.enable (/home/jcm/projects/opensource/opentelemetry-js-contrib/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js:239:29)

Closes #2792

And contributes to #1942

Short description of the changes

This fixes it by checking the default export and also adds a test, that was failing before the fix:

image

@JCMais JCMais requested a review from a team as a code owner April 14, 2025 14:44
@github-actions github-actions bot requested a review from blumamir April 14, 2025 14:45
@JCMais JCMais changed the title fix instrumentation of ESM-imported mongoose fix: instrumentation of ESM-imported mongoose Apr 14, 2025
@pichlermarc pichlermarc added priority:p1 Bugs which cause problems in end-user applications such as crashes, data inconsistencies bug Something isn't working labels Apr 14, 2025
Copy link

codecov bot commented Apr 25, 2025

Codecov Report

Attention: Patch coverage is 75.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 89.48%. Comparing base (32f41ee) to head (930196e).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...gins/node/instrumentation-mongoose/src/mongoose.ts 75.00% 2 Missing ⚠️

❌ Your patch status has failed because the patch coverage (75.00%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2793      +/-   ##
==========================================
- Coverage   89.50%   89.48%   -0.02%     
==========================================
  Files         180      180              
  Lines        8719     8725       +6     
  Branches     1767     1771       +4     
==========================================
+ Hits         7804     7808       +4     
- Misses        915      917       +2     
Files with missing lines Coverage Δ
...gins/node/instrumentation-mongoose/src/mongoose.ts 97.03% <75.00%> (-1.42%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@pichlermarc pichlermarc left a comment

Choose a reason for hiding this comment

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

I pushed a lint fix - overall the PR looks good, thank you for adding the test. 🙂
We can ignore the codecov complaint about the added lines not being covered (that's happening as the test that does cover is running out-of-process).

@pichlermarc pichlermarc merged commit 64d358e into open-telemetry:main Apr 30, 2025
22 of 23 checks passed
@dyladan dyladan mentioned this pull request Apr 30, 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 this pull request may close these issues.

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