Skip to content

feat(instrumentation-mongoose): add instrumentation of static methods #2748

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

Conversation

Vovcharaa
Copy link
Contributor

@Vovcharaa Vovcharaa commented Mar 9, 2025

Which problem is this PR solving?

Currently Mongoose instrumentation does not track static methods like insertMany and bulkWrite

Short description of the changes

Added patchModelStatic to allow instrumentation of static model methods.
Changed tested versions of mongoose because some older versions have weird timeouts with callbacks when suppressTracing is used in tests.

Related issues

Fixes #2488

@Vovcharaa Vovcharaa requested a review from a team as a code owner March 9, 2025 14:36
Copy link

linux-foundation-easycla bot commented Mar 9, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@Vovcharaa
Copy link
Contributor Author

Hi @blumamir,
Is there any chance that you'll be able to review it soon?

@bimmerv
Copy link

bimmerv commented Mar 24, 2025

Hi.
I'm also interested in this feature.

@Vovcharaa
Copy link
Contributor Author

Is there anyone who can review this?
@pichlermarc

@Vovcharaa Vovcharaa force-pushed the mongoose-static-method-tracing branch from 60804e1 to cd106dc Compare April 28, 2025 19:37
@pichlermarc
Copy link
Member

Hi @Vovcharaa - @blumamir owns this component, they are the first contact point for reviews.

Regardless, a few things that stand out from a high-level perspective:

  • I don't think not testing certain versions is the way to go, as we'd not notice if we'd crash the end-user's app on certain versions of mongoose.
  • the PR is marked as breaking (! in the PR title), but the changes actually all seem non-breaking to me, what's the reason for that? 🤔

@Vovcharaa
Copy link
Contributor Author

Hi @pichlermarc
I marked PR as breaking because I removed older versions from tests. I thought it is enough to mark them as not supported anymore.

@Vovcharaa Vovcharaa force-pushed the mongoose-static-method-tracing branch from cd106dc to 62e5b65 Compare May 7, 2025 12:41
Copy link
Member

@blumamir blumamir left a comment

Choose a reason for hiding this comment

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

LGTM

Thank you for contributing this enhancement

@blumamir
Copy link
Member

blumamir commented May 7, 2025

please also update the new version range:

Copy link

codecov bot commented May 7, 2025

Codecov Report

Attention: Patch coverage is 86.48649% with 5 lines in your changes missing coverage. Please review.

Project coverage is 89.68%. Comparing base (11a2999) to head (e1f15c1).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...gins/node/instrumentation-mongoose/src/mongoose.ts 85.71% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2748      +/-   ##
==========================================
- Coverage   89.69%   89.68%   -0.02%     
==========================================
  Files         184      184              
  Lines        8966     9003      +37     
  Branches     1835     1845      +10     
==========================================
+ Hits         8042     8074      +32     
- Misses        924      929       +5     
Files with missing lines Coverage Δ
plugins/node/instrumentation-mongoose/src/utils.ts 76.08% <100.00%> (+1.08%) ⬆️
...gins/node/instrumentation-mongoose/src/mongoose.ts 94.70% <85.71%> (-2.34%) ⬇️
🚀 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.

@Vovcharaa Vovcharaa force-pushed the mongoose-static-method-tracing branch 2 times, most recently from bfac69d to edbb3ff Compare May 8, 2025 13:36
@Vovcharaa
Copy link
Contributor Author

@pichlermarc @blumamir Could you pls allow tests to run?

@Vovcharaa Vovcharaa force-pushed the mongoose-static-method-tracing branch from edbb3ff to 979415c Compare May 17, 2025 14:44
@Vovcharaa
Copy link
Contributor Author

@pichlermarc @blumamir, for some unknown reason, suppressTracing works weird with callback tests. It suppresses what it should not. I fixed it by enabling instrumentation after injecting test data into the database to avoid using suppressTracing in tests.

@Vovcharaa
Copy link
Contributor Author

@pichlermarc As I see @blumamir quite rarely reviews PRs in this repo, could you, please, approve tests and merge this PR in case everything is good? This PR already has component owner approval.

@@ -23,6 +23,8 @@ export interface SerializerPayload {
document?: any;
aggregatePipeline?: any;
fields?: any;
documents?: any;
Copy link
Contributor

Choose a reason for hiding this comment

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

(not for this PR): I guess the payload properties depend on the operation being serialized. This type and DbStatementSerializer could be improved by using a union on the type property and a specific type per opration in payload. This way we provide some hints to the devs who want to implement a serializer

Check this small example in TS playground.

@@ -99,7 +99,7 @@ export class MongooseInstrumentation extends InstrumentationBase<MongooseInstrum
protected init(): InstrumentationModuleDefinition {
const module = new InstrumentationNodeModuleDefinition(
'mongoose',
['>=5.9.7 <9'],
Copy link
Contributor

Choose a reason for hiding this comment

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

the description of the PR states

Changed tested versions of mongoose because some older versions have weird timeouts with callbacks when suppressTracing is used in tests.

But here is actually dropping support for a range of versions (>=6 <6.7.5) :(
What are exactly the test issues you're facing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I fixed one issue with callbacks when there are 3 args on these versions, but the issue with suppressTracing remains on them.
If loadUsers is called after instrumentation is enabled and is wrapped in suppressTracing, spans in callback tests are not created (or ended).

@@ -321,6 +321,107 @@ describe('mongoose instrumentation [common]', () => {
expect(statement.document).toEqual(expect.objectContaining(document));
});

it('instrumenting insertMany operation', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

There is not test with options or callback params. Is it possible to ad one test for these cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added for insertMany.
This helped identify the issue with versions that I dropped support for.

@Vovcharaa Vovcharaa changed the title feat(instrumentation-mongoose)!: add instrumentation of static methods feat(instrumentation-mongoose): add instrumentation of static methods May 19, 2025
@Vovcharaa Vovcharaa force-pushed the mongoose-static-method-tracing branch from 72c0f52 to 167682d Compare May 19, 2025 19:59
Comment on lines 108 to 109
} else if (args.length === 3){
callbackArgumentIndex = 2;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the fix for insertMany/bulkWrite with callback and options.
It also fixed issues with older versions, which I previously dropped from support.

@david-luna david-luna merged commit 55cc256 into open-telemetry:main May 20, 2025
29 of 30 checks passed
@dyladan dyladan mentioned this pull request May 20, 2025
@Vovcharaa Vovcharaa deleted the mongoose-static-method-tracing branch May 20, 2025 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@opentelemetry/instrumentation-mongoose does not instrument insertMany method
5 participants