Skip to content
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

fix(mongodb): mongodb v6.4.0 & 6.5.0 support #2001

Merged
merged 21 commits into from
Mar 27, 2024

Conversation

david-luna
Copy link
Contributor

Which problem is this PR solving?

the new version of mongodb v6.4.0 comes with some internal changes that break the instrumentation. Drtails of the changes are described in #1983 (comment)

Closes: #1983

Short description of the changes

patches of the changed methods have been refactored to accept both implementations

  • handling callbacks for <6.4.0
  • handling returned promises for >=6.4.0

@david-luna david-luna requested a review from a team March 7, 2024 09:44
@github-actions github-actions bot requested a review from osherv March 7, 2024 09:45
@@ -353,7 +353,12 @@ export class MongoDBInstrumentation extends InstrumentationBase {
instrumentation.setPoolName(options);
callback(err, conn);
};
return original.call(this, options, patchedCallback);

const result = original.call(this, options, patchedCallback);
Copy link
Contributor

Choose a reason for hiding this comment

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

It feels slightly messy to be passing in this patchedCallback that isn't going to be used.
Thoughts on a separate patch function for mongodb >=6.4.0 -- this one would just do the promise thing.

Copy link
Contributor Author

@david-luna david-luna Mar 8, 2024

Choose a reason for hiding this comment

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

my intention was to not split the patch logic of this function to 2 separate patches. I do agree is not optimal to create a function that may not be used. I'll try to come with a different approach

@@ -58,7 +58,7 @@
"@types/mongodb": "3.6.20",
"@types/node": "18.6.5",
"mocha": "7.2.0",
"mongodb": "3.6.11",
"mongodb": "^6.4.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is done, then the default "test" script will need to change above, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you're right

}

const result = original.call(this, ns, cmd, options, patchedCallback);
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar Q to above: would separate patch functions (one that uses the callback, one that uses the promise) be clearer? or would they just be a pain?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

here the patchedCallback can be reused because basically the API change has been to "promisify" the function. A separate patch is not complicated, it's almost an identical copy.

Copy link

codecov bot commented Mar 12, 2024

Codecov Report

Merging #2001 (4c1aac9) into main (dfb2dff) will decrease coverage by 0.34%.
Report is 18 commits behind head on main.
The diff coverage is 71.11%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2001      +/-   ##
==========================================
- Coverage   90.97%   90.63%   -0.34%     
==========================================
  Files         146      146              
  Lines        7492     7488       -4     
  Branches     1502     1494       -8     
==========================================
- Hits         6816     6787      -29     
- Misses        676      701      +25     
Files Coverage Δ
...etry-instrumentation-mongodb/src/internal-types.ts 100.00% <ø> (ø)
...try-instrumentation-mongodb/src/instrumentation.ts 47.42% <71.11%> (-5.85%) ⬇️

... and 7 files with indirect coverage changes

@david-luna david-luna requested a review from trentm March 12, 2024 14:41
@david-luna
Copy link
Contributor Author

@trentm I did the split you suggested but partially. I think it was overkill to split the logic of the connect patch.

Now I'm hitting a problem in test-all-versions script. It is failing for [email protected] in the beforeEach method of the tests which is quite odd.

if I log the version under test...

--- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts
+++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts
@@ -56,6 +56,9 @@ describe('MongoDBInstrumentation-Tracing-v4', () => {
   const COLLECTION_NAME = 'test-traces';
   const URL = `mongodb://${HOST}:${PORT}/${DB_NAME}`;

+  const version = require('mongodb/package.json').version;
+  console.log(`testing mongodb@${version}`);
+
   let client: mongodb.MongoClient;
   let collection: mongodb.Collection;

it turns out we get a different version for testing

> @opentelemetry/[email protected] test-v4
> nyc ts-mocha -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/mongodb-v4-v5-v6.metrics.test.ts' 'test/**/mongodb-v4.test.ts'

testing [email protected]

  MongoDBInstrumentation-Metrics
    ✓ Should add connection usage metrics (101ms)
    ✓ Should add disconnection usage metrics (213ms)

  MongoDBInstrumentation-Tracing-v4
    Instrumenting query operations
      1) "before each" hook: mongoBeforeEach for "should create a child span for insert"
      2) "after each" hook for "should create a child span for insert"


  2 passing (4s)
  2 failing

....

@trentm I think you got into a similar situation but I can't find the issue or PR. Do you recall having a similar issue? 🤔

@david-luna david-luna changed the title fix(mongodb): mongodb v6.4.0 support fix(mongodb): mongodb v6.4.0 & 6.5.0 support Mar 12, 2024
@trentm
Copy link
Contributor

trentm commented Mar 12, 2024

@trentm I think you got into a similar situation but I can't find the issue or PR. Do you recall having a similar issue? 🤔

Yup, it was described here: #1938 (comment)
We get this behaviour because of a pinned "mongodb" transitive dep from instrumentation-mongoose:

[~/tm/opentelemetry-js-contrib2/plugins/node/opentelemetry-instrumentation-mongodb (git:1983-mongodb-6-4-support)]
% npm ls mongodb
[email protected] /Users/trentm/tm/opentelemetry-js-contrib2
└─┬ @opentelemetry/[email protected] -> ./plugins/node/opentelemetry-instrumentation-mongodb
  └── [email protected]

[~/tm/opentelemetry-js-contrib2/plugins/node/opentelemetry-instrumentation-mongodb (git:1983-mongodb-6-4-support)]
% npm install --no-save [email protected]
...

[~/tm/opentelemetry-js-contrib2/plugins/node/opentelemetry-instrumentation-mongodb (git:1983-mongodb-6-4-support)]
% npm ls mongodb
[email protected] /Users/trentm/tm/opentelemetry-js-contrib2
└─┬ @opentelemetry/[email protected] -> ./plugins/node/opentelemetry-instrumentation-mongodb
  └── [email protected]

The looking for any npm install felt it didn't need to install v4.17.1 locally:

[~/tm/opentelemetry-js-contrib2 (git:1983-mongodb-6-4-support)]
% npm ls mongodb
[email protected] /Users/trentm/tm/opentelemetry-js-contrib2
├─┬ @opentelemetry/[email protected] -> ./plugins/node/opentelemetry-instrumentation-mongodb
│ └── [email protected]
└─┬ @opentelemetry/[email protected] -> ./plugins/node/instrumentation-mongoose
  └─┬ [email protected]
    └── [email protected]

Yuck. This has to be an npm bug. I'm not sure how to cope with this in general.
We discussed (in DMs) explicitly skipping v4.17.1 in the .tav.yml file. That's a hack, but I can't think of anything better.

Copy link
Contributor

@trentm trentm left a comment

Choose a reason for hiding this comment

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

Thanks for humouring me on the splitting the callback vs. promise patching. That looks like it a was a fair amount of extra work.

LGTM, though I think the .tav.yml update should be added before merge.

@@ -11,7 +11,7 @@ jobs:
matrix:
node: ["14", "16", "18.18.2"]
include:
- node: 14
- node: 16
Copy link
Contributor

Choose a reason for hiding this comment

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

Note to reviewers: This was discussion in the OTel JS SIG this week as a fix for the code-coverage check failures. Testing coverage only with node v14 artificially results in coverage looking poor for a unit-test run that skips all or most tests for the (now old) node v14.

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.

Thanks for taking care of this 🙂

@david-luna
Copy link
Contributor Author

david-luna commented Mar 26, 2024

So the split added new code that isn't covered by the tests since is meant to run in earlier versions. I'll see what I can do about it

@david-luna
Copy link
Contributor Author

david-luna commented Mar 26, 2024

By looking at the docs there is no way to tell codecov that a specific code path is expected to not to be covered. I guess the solution would be to make use of merge reports but IMO this is out of scope of this PR (the impact is probably high)

@pichlermarc do you think is okay to move forward and have a follow-up PR about merging coverage reports from unit testing?

@trentm
Copy link
Contributor

trentm commented Mar 27, 2024

I agree dealing with merging of coverage reports is out of scope for this PR.

@trentm trentm merged commit 20328d4 into open-telemetry:main Mar 27, 2024
16 of 17 checks passed
@dyladan dyladan mentioned this pull request Mar 25, 2024
@david-luna david-luna deleted the 1983-mongodb-6-4-support branch March 27, 2024 16:19
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.

[instrumentation-mongodb] test failing for [email protected]
4 participants